Fix Parent finding issue with BeforeSave hook and duplicate finding error for category

This commit is contained in:
qowevisa 2024-11-11 22:26:21 +02:00
parent 9b77237d50
commit 8a7d52efda

View File

@ -40,7 +40,7 @@ var (
func (c *Category) BeforeSave(tx *gorm.DB) error { func (c *Category) BeforeSave(tx *gorm.DB) error {
if c.ParentID != 0 { if c.ParentID != 0 {
var parent Category var parent Category
if err := tx.Find(&parent, c.ID).Error; err != nil { if err := tx.Find(&parent, c.ParentID).Error; err != nil {
return err return err
} }
if parent.ID == 0 { if parent.ID == 0 {
@ -54,7 +54,7 @@ func (c *Category) BeforeSave(tx *gorm.DB) error {
if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil { if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil {
return err return err
} }
if dup.ID != 0 { if c.ID != dup.ID && dup.ID != 0 {
return ERROR_CATEGORY_NAME_NOT_UNIQUE return ERROR_CATEGORY_NAME_NOT_UNIQUE
} }
return nil return nil