From 186f8b8aff8b2b444c780612b9bb9786722078d3 Mon Sep 17 00:00:00 2001 From: TZGyn Date: Fri, 15 Sep 2023 06:43:29 +0800 Subject: [PATCH] Update delete bookmark category route to check if category has 0 bookmarks --- app/api/bookmark_category/route.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/api/bookmark_category/route.ts b/app/api/bookmark_category/route.ts index 3920fd1..a660c70 100644 --- a/app/api/bookmark_category/route.ts +++ b/app/api/bookmark_category/route.ts @@ -51,6 +51,23 @@ export const DELETE = async (request: NextRequest) => { const bookmarkId = body.bookmarkCategoryId + const selectedBookmarkCategory = await db.query.bookmarkCategory.findFirst({ + where: (bookmarkCategory, { eq }) => + eq(bookmarkCategory.id, bookmarkId), + with: { + bookmark: true, + }, + }) + + if (!selectedBookmarkCategory) + return NextResponse.json({ message: 'Invalid Category' }) + + if (selectedBookmarkCategory.bookmark.length) { + return NextResponse.json({ + message: 'Category Must Have No Bookmarks To Be Deleted', + }) + } + await db .delete(bookmarkCategory) .where(