cleanup tags on edit
All checks were successful
Build / build (push) Successful in 3m11s

This commit is contained in:
Nareshkumar Rao 2025-04-22 12:48:23 +02:00
parent 8b46bf354e
commit d9e9824146

View File

@ -39,6 +39,21 @@ export async function savePostServer(
const slug = slugify(title, { lower: true, strict: true }); const slug = slugify(title, { lower: true, strict: true });
if (existingSlug) { 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 } }); await prisma.post.delete({ where: { slug: existingSlug } });
} }