Add order to tasks and lists

master
Bel LaPointe 2019-11-25 12:57:47 -07:00
parent f2e3a71348
commit 48e8b91039
6 changed files with 29 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package ajax
import (
"local/todo-server/config"
"local/todo-server/server/ajax/list"
"testing"
)
@ -15,5 +16,12 @@ func mockAjax() *Ajax {
if err != nil {
panic(err)
}
list := &list.List{
Name: "list",
UUID: "list",
}
if err := ajax.storageSetList(list); err != nil {
panic(err)
}
return ajax
}

View File

@ -14,6 +14,7 @@ type List struct {
ShowCompl int `json:"showCompl"`
ShowNotes int `json:"showNotes"`
Hidden int `json:"hidden"`
Max int `json:"max"`
}
func New(r *http.Request) (*List, error) {
@ -47,6 +48,11 @@ func (l *List) SetHideList(state bool) {
set(state, &l.Hidden)
}
func (l *List) NextIndex() int {
l.Max += 1
return l.Max
}
func set(b bool, i *int) {
*i = 1
if !b {

View File

@ -77,7 +77,10 @@ func (a *Ajax) storageListTasks(listID string, filters ...func(t *task.Task) boo
}
}
sort.SliceStable(tasks, func(i, j int) bool {
if tasks[i].Index == tasks[j].Index {
return tasks[i].Created.Before(tasks[j].Created)
}
return tasks[i].Index < tasks[j].Index
})
return tasks, nil
}

View File

@ -33,7 +33,7 @@ func TestAjaxStorageListLists(t *testing.T) {
if results, err := ajax.storageListLists(); err != nil {
t.Error(err)
} else if len(results) != 3 {
} else if len(results) < 3 {
t.Error(results)
}

View File

@ -68,6 +68,14 @@ func (a *Ajax) makeTask(r *http.Request) (string, *task.Task, error) {
if err != nil {
return "", nil, err
}
list, err := a.storageGetList(listID)
if err != nil {
return "", nil, err
}
task.Index = list.NextIndex()
if err := a.storageSetList(list); err != nil {
return "", nil, err
}
task.AppendTags(tags)
return listID, task, nil
}

View File

@ -23,6 +23,8 @@ type Task struct {
Complete bool
Note []string
Due time.Time
Index int
}
type StrList []string