From 218ef615374c7b5fbfd9b5801756ec9f8751d274 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 3 Aug 2024 10:57:13 +0300 Subject: [PATCH] Fixes --- db/category.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/db/category.go b/db/category.go index 76435a2..67f4d24 100644 --- a/db/category.go +++ b/db/category.go @@ -17,19 +17,23 @@ type Category struct { } var ( - ERROR_CATEGORY_PARENT_NOT_FOUND = errors.New("ParentID is invalid for user") - ERROR_CATEGORY_NAME_NOT_UNIQUE = errors.New("Name for Category have to be unique for user") + ERROR_CATEGORY_PARENT_NOT_FOUND = errors.New("ParentID is invalid for user") + ERROR_CATEGORY_NAME_NOT_UNIQUE = errors.New("Name for Category have to be unique for user") + ERROR_CATEGORY_USER_ID_NOT_EQUAL = errors.New("ParentID is invalid for user") ) func (c *Category) BeforeSave(tx *gorm.DB) error { if c.ParentID != 0 { var parent Category - if err := tx.Find(&parent, Category{ParentID: c.ParentID, UserID: c.UserID}).Error; err != nil { + if err := tx.Find(&parent, c.ID).Error; err != nil { return err } if parent.ID == 0 { return ERROR_CATEGORY_PARENT_NOT_FOUND } + if parent.UserID != c.UserID { + return ERROR_CATEGORY_USER_ID_NOT_EQUAL + } } var dup Category if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil {