Skip to content

Commit

Permalink
feat: add cascade on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
oae committed Oct 15, 2022
1 parent e6b6f64 commit e09b656
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
Binary file removed prisma/kaizoku.db-journal
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CREATE TABLE "Manga" (
"interval" TEXT NOT NULL,
"source" TEXT NOT NULL,
"libraryId" INTEGER NOT NULL,
CONSTRAINT "Manga_libraryId_fkey" FOREIGN KEY ("libraryId") REFERENCES "Library" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
CONSTRAINT "Manga_libraryId_fkey" FOREIGN KEY ("libraryId") REFERENCES "Library" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
Expand All @@ -22,7 +22,7 @@ CREATE TABLE "Chapter" (
"fileName" TEXT NOT NULL,
"size" INTEGER NOT NULL,
"mangaId" INTEGER NOT NULL,
CONSTRAINT "Chapter_mangaId_fkey" FOREIGN KEY ("mangaId") REFERENCES "Manga" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
CONSTRAINT "Chapter_mangaId_fkey" FOREIGN KEY ("mangaId") REFERENCES "Manga" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
Expand Down
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ model Manga {
cover String
interval String
source String
library Library @relation(fields: [libraryId], references: [id])
library Library @relation(fields: [libraryId], references: [id], onDelete: Cascade)
libraryId Int
chapter Chapter[]
}
Expand All @@ -33,6 +33,6 @@ model Chapter {
index Int
fileName String
size Int
manga Manga @relation(fields: [mangaId], references: [id])
manga Manga @relation(fields: [mangaId], references: [id], onDelete: Cascade)
mangaId Int
}
12 changes: 6 additions & 6 deletions src/components/mangaCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ const useStyles = createStyles((theme, _params, getRef) => ({
marginTop: theme.spacing.xs,
},

interval: {
textTransform: 'uppercase',
badge: {
cursor: 'pointer',
},
}));

interface ArticleCardImageProps {
cover: string;
title: string;
interval: string;
badge: string;
onRemove: () => void;
onClick: () => void;
}
Expand All @@ -76,7 +76,7 @@ const createRemoveModal = (title: string, onRemove: () => void) => {
return openRemoveModal;
};

export function MangaCard({ cover, title, interval, onRemove, onClick }: ArticleCardImageProps) {
export function MangaCard({ cover, title, badge, onRemove, onClick }: ArticleCardImageProps) {
const { classes } = useStyles();
const removeModal = createRemoveModal(title, onRemove);

Expand Down Expand Up @@ -104,8 +104,8 @@ export function MangaCard({ cover, title, interval, onRemove, onClick }: Article
<IconX size={16} />
</ActionIcon>
<div>
<Badge color="teal" variant="filled" className={classes.interval} size="md">
{interval}
<Badge color="teal" variant="filled" className={classes.badge} size="xs">
{badge}
</Badge>
<Title order={3} className={classes.title}>
{title}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function IndexPage() {
return (
<Grid.Col span="content" key={manga.id}>
<MangaCard
interval={manga.interval}
badge={manga.source}
title={manga.title}
cover={manga.cover}
onRemove={() => handleRemove(manga.id, manga.title)}
Expand Down
27 changes: 14 additions & 13 deletions src/server/queue/checkChapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ const checkChapters = async (manga: MangaWithLibrary) => {
logger.info(`There are no missing chapter files for ${manga.title}`);

const localChapters = await getChaptersFromLocal(mangaDir);

await prisma.chapter.deleteMany({
where: {
mangaId: manga.id,
index: {
notIn: localChapters.map((chapter) => chapter.index),
},
fileName: {
notIn: localChapters.map((chapter) => chapter.fileName),
},
},
});

const dbChapters = await prisma.chapter.findMany({
where: {
mangaId: manga.id,
},
});

const dbOnlyChapters = dbChapters.filter(
(dbChapter) => localChapters.findIndex((localChapter) => localChapter.index === dbChapter.index) < 0,
);

await Promise.all(
dbOnlyChapters.map(async (chapter) => {
await prisma.chapter.delete({
where: {
id: chapter.id,
},
});
}),
);

const missingDbChapters = localChapters.filter(
(localChapter) => dbChapters.findIndex((dbChapter) => dbChapter.index === localChapter.index) < 0,
);
Expand Down

0 comments on commit e09b656

Please sign in to comment.