Refactor bucket limit check into seperate function
This commit is contained in:
parent
9d17ec4cb3
commit
848b11ce52
|
@ -545,6 +545,32 @@ func checkBucketAndTaskBelongToSameList(s *xorm.Session, fullTask *Task, bucketI
|
||||||
return
|
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
|
// Create is the implementation to create a list task
|
||||||
// @Summary Create a task
|
// @Summary Create a task
|
||||||
// @Description Inserts a task into a list.
|
// @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
|
return err
|
||||||
}
|
}
|
||||||
t.BucketID = bucket.ID
|
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
|
// Bucket Limit
|
||||||
if bucket.Limit > 0 {
|
if err := checkBucketLimit(s, t, bucket); err != nil {
|
||||||
taskCount, err := s.
|
return err
|
||||||
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}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the index for this task
|
// Get the index for this task
|
||||||
|
|
Loading…
Reference in New Issue