fix(task): cyclomatic complexity
(cherry picked from commit 20724f6fb57128fd212544cb3a6b55b90f38c316)
This commit is contained in:
parent
dfda2e5500
commit
b94802169c
@ -681,7 +681,7 @@ func calculateDefaultPosition(entityID int64, position float64) float64 {
|
|||||||
return position
|
return position
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNextTaskIndex(s *xorm.Session, projectID int64) (nextIndex int64, err error) {
|
func calculateNextTaskIndex(s *xorm.Session, projectID int64) (nextIndex int64, err error) {
|
||||||
latestTask := &Task{}
|
latestTask := &Task{}
|
||||||
_, err = s.
|
_, err = s.
|
||||||
Where("project_id = ?", projectID).
|
Where("project_id = ?", projectID).
|
||||||
@ -694,6 +694,29 @@ func getNextTaskIndex(s *xorm.Session, projectID int64) (nextIndex int64, err er
|
|||||||
return latestTask.Index + 1, nil
|
return latestTask.Index + 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setNewTaskIndex(s *xorm.Session, t *Task) (err error) {
|
||||||
|
// Check if an index was provided, otherwise calculate a new one
|
||||||
|
if t.Index == 0 {
|
||||||
|
t.Index, err = calculateNextTaskIndex(s, t.ProjectID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the provided index is already taken
|
||||||
|
exists, err := s.Where("project_id = ? AND `index` = ?", t.ProjectID, t.Index).Exist(&Task{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
// If the index is taken, calculate a new one
|
||||||
|
t.Index, err = calculateNextTaskIndex(s, t.ProjectID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Create is the implementation to create a project task
|
// Create is the implementation to create a project task
|
||||||
// @Summary Create a task
|
// @Summary Create a task
|
||||||
// @Description Inserts a task into a project.
|
// @Description Inserts a task into a project.
|
||||||
@ -738,25 +761,9 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool, setB
|
|||||||
t.UID = uuid.NewString()
|
t.UID = uuid.NewString()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if an index was provided, otherwise calculate a new one
|
err = setNewTaskIndex(s, t)
|
||||||
if t.Index == 0 {
|
if err != nil {
|
||||||
t.Index, err = getNextTaskIndex(s, t.ProjectID)
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Check if the provided index is already taken
|
|
||||||
exists, err := s.Where("project_id = ? AND `index` = ?", t.ProjectID, t.Index).Exist(&Task{})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if exists {
|
|
||||||
// If the index is taken, calculate a new one
|
|
||||||
t.Index, err = getNextTaskIndex(s, t.ProjectID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.HexColor = utils.NormalizeHex(t.HexColor)
|
t.HexColor = utils.NormalizeHex(t.HexColor)
|
||||||
@ -924,7 +931,7 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
|
|||||||
|
|
||||||
// If the task is being moved between projects, make sure to move the bucket + index as well
|
// If the task is being moved between projects, make sure to move the bucket + index as well
|
||||||
if t.ProjectID != 0 && ot.ProjectID != t.ProjectID {
|
if t.ProjectID != 0 && ot.ProjectID != t.ProjectID {
|
||||||
t.Index, err = getNextTaskIndex(s, t.ProjectID)
|
t.Index, err = calculateNextTaskIndex(s, t.ProjectID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user