From 848b11ce526f5c62dca58956528874ef71b74a3a Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 4 Sep 2020 13:41:41 +0200 Subject: [PATCH] Refactor bucket limit check into seperate function --- pkg/models/tasks.go | 46 ++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index d34e15b23..7dd15daf8 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -545,6 +545,32 @@ func checkBucketAndTaskBelongToSameList(s *xorm.Session, fullTask *Task, bucketI return } +// Checks if adding a new task would exceed the bucket limit +func checkBucketLimit(s *xorm.Session, t *Task, bucket *Bucket) (err error) { + + // We need the bucket to check if it has more tasks than the limit allows + if bucket == nil { + bucket, err = getBucketByID(s, t.BucketID) + if err != nil { + return err + } + } + + // Check the limit + if bucket.Limit > 0 { + taskCount, err := s. + Where("bucket_id = ?", bucket.ID). + Count(&Task{}) + if err != nil { + return err + } + if taskCount >= bucket.Limit { + return ErrBucketLimitExceeded{TaskID: t.ID, BucketID: bucket.ID, Limit: bucket.Limit} + } + } + return nil +} + // Create is the implementation to create a list task // @Summary Create a task // @Description Inserts a task into a list. @@ -615,25 +641,11 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err return err } t.BucketID = bucket.ID - } else { - // We need the bucket to check if it has more tasks than the limit allows - bucket, err = getBucketByID(s, t.BucketID) - if err != nil { - return err - } } - // Check the limit - if bucket.Limit > 0 { - taskCount, err := s. - Where("bucket_id = ?", bucket.ID). - Count(&Task{}) - if err != nil { - return err - } - if taskCount >= bucket.Limit { - return ErrBucketLimitExceeded{TaskID: t.ID, BucketID: bucket.ID, Limit: bucket.Limit} - } + // Bucket Limit + if err := checkBucketLimit(s, t, bucket); err != nil { + return err } // Get the index for this task