From a16c4c07645052972c65893776fa83847b4cc5f3 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 16 Feb 2025 17:11:36 +0800 Subject: [PATCH] :bug: Fix delete publisher will auto delete the posts posted by it --- pkg/internal/services/publishers.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/internal/services/publishers.go b/pkg/internal/services/publishers.go index 1ed8563..dd81a97 100644 --- a/pkg/internal/services/publishers.go +++ b/pkg/internal/services/publishers.go @@ -82,5 +82,16 @@ func EditPublisher(user authm.Account, publisher models.Publisher) (models.Publi } func DeletePublisher(publisher models.Publisher) error { - return database.C.Delete(&publisher).Error + tx := database.C.Begin() + + if err := tx.Where("publisher_id = ?", publisher.ID).Delete(&models.Post{}).Error; err != nil { + tx.Rollback() + return err + } + if err := tx.Delete(&publisher).Error; err != nil { + tx.Rollback() + return err + } + + return tx.Commit().Error }