Refactor bucket limit check into seperate function

This commit is contained in:
kolaente 2020-09-04 13:41:41 +02:00
parent 9d17ec4cb3
commit 848b11ce52
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 29 additions and 17 deletions

View File

@ -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