From 238b9e4c8594b8846b3cd9a24702a2299581a0a1 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Tue, 21 Nov 2017 14:57:27 -0800 Subject: Check for category uniqueness before saving --- storage/category.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'storage/category.go') diff --git a/storage/category.go b/storage/category.go index 3d08c4d..e1986b7 100644 --- a/storage/category.go +++ b/storage/category.go @@ -8,11 +8,21 @@ import ( "database/sql" "errors" "fmt" + "time" + "github.com/miniflux/miniflux2/helper" "github.com/miniflux/miniflux2/model" - "time" ) +func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool { + defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:AnotherCategoryExists] userID=%d, categoryID=%d, title=%s", userID, categoryID, title)) + + var result int + query := `SELECT count(*) as c FROM categories WHERE user_id=$1 AND id != $2 AND title=$3` + s.db.QueryRow(query, userID, categoryID, title).Scan(&result) + return result >= 1 +} + func (s *Storage) CategoryExists(userID, categoryID int64) bool { defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CategoryExists] userID=%d, categoryID=%d", userID, categoryID)) -- cgit v1.2.3