From 95ccdeb1b206c68d856b06078cd9e765025e06fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20devkcud=20Albanese=20Junior?= Date: Sat, 26 Oct 2024 21:46:49 -0300 Subject: [PATCH] fix When project gets deleted remove forks --- internal/service/repository/project.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/service/repository/project.go b/internal/service/repository/project.go index 8df4d29..af30843 100644 --- a/internal/service/repository/project.go +++ b/internal/service/repository/project.go @@ -296,8 +296,8 @@ func (pr *projectRepository) Update(projectID uint, updateModel *dto.ProjectUpda var upstreamPublication model.ProjectPublication if err := tx.Where("project_id = ?", upstreamProject.ID).First(&upstreamPublication).Error; err != nil { + tx.Rollback() if errors.Is(err, gorm.ErrRecordNotFound) { - tx.Rollback() return ErrUpstreamNotPublic } return err @@ -813,6 +813,11 @@ func (pr *projectRepository) UnsafeDelete(id uint) error { return err } + if err := tx.Model(&model.Project{}).Where("fork = ?", id).Update("fork", nil).Error; err != nil { + tx.Rollback() + return err + } + if err := tx.Commit().Error; err != nil { tx.Rollback() return err @@ -924,6 +929,18 @@ func (pr *projectRepository) ClearTrash(userID uint) error { return err } + deletedProjectIDs := make([]uint, len(projects)) + for i, project := range projects { + deletedProjectIDs[i] = project.ID + } + + if len(deletedProjectIDs) > 0 { + if err := tx.Model(&model.Project{}).Where("fork IN ?", deletedProjectIDs).Update("fork", nil).Error; err != nil { + tx.Rollback() + return err + } + } + if err := tx.Commit().Error; err != nil { tx.Rollback() return err