fix: make sure task indexes are calculated correctly when moving tasks between lists
Resolves https://github.com/go-vikunja/api/issues/52
This commit is contained in:
parent
649d1e3e6f
commit
c495096444
@ -869,6 +869,19 @@ func calculateDefaultPosition(entityID int64, position float64) float64 {
|
|||||||
return position
|
return position
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getNextTaskIndex(s *xorm.Session, listID int64) (nextIndex int64, err error) {
|
||||||
|
latestTask := &Task{}
|
||||||
|
_, err = s.
|
||||||
|
Where("list_id = ?", listID).
|
||||||
|
OrderBy("`index` desc").
|
||||||
|
Get(latestTask)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return latestTask.Index + 1, 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.
|
||||||
@ -920,16 +933,14 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the index for this task
|
// Get the index for this task
|
||||||
latestTask := &Task{}
|
t.Index, err = getNextTaskIndex(s, t.ListID)
|
||||||
_, err = s.Where("list_id = ?", t.ListID).OrderBy("id desc").Get(latestTask)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Index = latestTask.Index + 1
|
|
||||||
// If no position was supplied, set a default one
|
// If no position was supplied, set a default one
|
||||||
t.Position = calculateDefaultPosition(latestTask.ID+1, t.Position)
|
t.Position = calculateDefaultPosition(t.Index, t.Position)
|
||||||
t.KanbanPosition = calculateDefaultPosition(latestTask.ID+1, t.KanbanPosition)
|
t.KanbanPosition = calculateDefaultPosition(t.Index, t.KanbanPosition)
|
||||||
if _, err = s.Insert(t); err != nil {
|
if _, err = s.Insert(t); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -1047,13 +1058,10 @@ func (t *Task) Update(s *xorm.Session, a web.Auth) (err error) {
|
|||||||
|
|
||||||
// If the task is being moved between lists, make sure to move the bucket + index as well
|
// If the task is being moved between lists, make sure to move the bucket + index as well
|
||||||
if t.ListID != 0 && ot.ListID != t.ListID {
|
if t.ListID != 0 && ot.ListID != t.ListID {
|
||||||
latestTask := &Task{}
|
t.Index, err = getNextTaskIndex(s, t.ListID)
|
||||||
_, err = s.Where("list_id = ?", t.ListID).OrderBy("id desc").Get(latestTask)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Index = latestTask.Index + 1
|
|
||||||
colsToUpdate = append(colsToUpdate, "index")
|
colsToUpdate = append(colsToUpdate, "index")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,10 +20,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/events"
|
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
|
"code.vikunja.io/api/pkg/events"
|
||||||
"code.vikunja.io/api/pkg/user"
|
"code.vikunja.io/api/pkg/user"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -324,6 +324,21 @@ func TestTask_Update(t *testing.T) {
|
|||||||
"bucket_id": 1,
|
"bucket_id": 1,
|
||||||
}, false)
|
}, false)
|
||||||
})
|
})
|
||||||
|
t.Run("moving a task between lists should give it a correct index", func(t *testing.T) {
|
||||||
|
db.LoadAndAssertFixtures(t)
|
||||||
|
s := db.NewSession()
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
task := &Task{
|
||||||
|
ID: 12,
|
||||||
|
ListID: 2, // From list 1
|
||||||
|
}
|
||||||
|
err := task.Update(s, u)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
err = s.Commit()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, int64(3), task.Index)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTask_Delete(t *testing.T) {
|
func TestTask_Delete(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user