From 22007ef38e9c545fab9e392715d47b023666e64f Mon Sep 17 00:00:00 2001 From: qowevisa Date: Mon, 11 Nov 2024 22:26:47 +0200 Subject: [PATCH] Add SelfReferencing check in Category database BeforeSave hook --- db/category.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/category.go b/db/category.go index 6a7e463..d29eac8 100644 --- a/db/category.go +++ b/db/category.go @@ -35,9 +35,13 @@ var ( ERROR_CATEGORY_PARENT_NOT_FOUND = errors.New("Can't find Category with ParentID 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") + ERROR_CATEGORY_SELF_REFERENCING = errors.New("Category can't set itself as a parent") ) func (c *Category) BeforeSave(tx *gorm.DB) error { + if c.ParentID == c.ID { + return ERROR_CATEGORY_SELF_REFERENCING + } if c.ParentID != 0 { var parent Category if err := tx.Find(&parent, c.ParentID).Error; err != nil {