diff --git a/src/app/blog/write/action.ts b/src/app/blog/write/action.ts index 2a9ee67..17cb0b7 100644 --- a/src/app/blog/write/action.ts +++ b/src/app/blog/write/action.ts @@ -39,6 +39,21 @@ export async function savePostServer( const slug = slugify(title, { lower: true, strict: true }); if (existingSlug) { + const post = await prisma.post.findUnique({ + where: { slug: existingSlug }, + include: { tags: true }, + }); + if (!post) { + throw new Error("Post not found"); + } + for (const tag of post.tags) { + const postsWithTag = await prisma.post.count({ + where: { tags: { some: { id: tag.id } } }, + }); + if (postsWithTag == 0) { + await prisma.tag.delete({ where: { id: tag.id } }); + } + } await prisma.post.delete({ where: { slug: existingSlug } }); }