fix: update tags and categories not updated
This commit is contained in:
parent
c410e651ce
commit
c48a4b944b
@ -2,7 +2,9 @@ package newsrepository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
newsdomain "legalgo-BE-go/internal/domain/news"
|
||||
tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
@ -34,26 +36,27 @@ func (a *accessor) Update(spec newsdomain.News) error {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to update news: %v", err)
|
||||
}
|
||||
if len(spec.Tags) < 1 {
|
||||
if err := tx.Model(&spec).Association("Tags").Clear(); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to clear tags: %v", err)
|
||||
}
|
||||
|
||||
tagsDeleted := make([]tagdomain.Tag, len(spec.Tags))
|
||||
copy(tagsDeleted, spec.Tags)
|
||||
if err := tx.Model(&spec).Association("Tags").Clear(); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to remove previous tags: %v", err)
|
||||
}
|
||||
|
||||
if len(spec.Categories) < 1 {
|
||||
if err := tx.Model(&spec).Association("Categories").Clear(); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to clear categories: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Model(&spec).Association("Tags").Append(spec.Tags); err != nil {
|
||||
if err := tx.Model(&spec).Association("Tags").Append(tagsDeleted); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to add tags: %v", err)
|
||||
}
|
||||
|
||||
if err := tx.Model(&spec).Association("Categories").Append(spec.Categories); err != nil {
|
||||
categoriesDeleted := make([]categorydomain.Category, len(spec.Categories))
|
||||
copy(categoriesDeleted, spec.Categories)
|
||||
if err := tx.Model(&spec).Association("Categories").Clear(); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to remove previous categories: %v", err)
|
||||
}
|
||||
|
||||
if err := tx.Model(&spec).Association("Categories").Append(categoriesDeleted); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to add categories: %v", err)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user