From 5ce7223b92fc01e5b9de74ba7abadcda033c0a04 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 3 Aug 2024 10:52:38 +0300 Subject: [PATCH] Fix for category when searching parents when ParentID is zero --- db/category.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/db/category.go b/db/category.go index 87a341f..76435a2 100644 --- a/db/category.go +++ b/db/category.go @@ -22,12 +22,14 @@ var ( ) func (c *Category) BeforeSave(tx *gorm.DB) error { - var parent Category - if err := tx.Find(&parent, Category{ParentID: c.ParentID, UserID: c.UserID}).Error; err != nil { - return err - } - if parent.ID == 0 { - return ERROR_CATEGORY_PARENT_NOT_FOUND + if c.ParentID != 0 { + var parent Category + if err := tx.Find(&parent, Category{ParentID: c.ParentID, UserID: c.UserID}).Error; err != nil { + return err + } + if parent.ID == 0 { + return ERROR_CATEGORY_PARENT_NOT_FOUND + } } var dup Category if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil {