Change task naming for faster listing in future

This commit is contained in:
bel
2019-12-07 12:19:04 -07:00
parent 9752acbd76
commit e119d1d867
4 changed files with 78 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ package ajax
import (
"bytes"
"encoding/gob"
"local/storage"
"local/todo-server/server/ajax/form"
"local/todo-server/server/ajax/list"
"local/todo-server/server/ajax/task"
@@ -57,10 +58,24 @@ func (a *Ajax) storageListLists(filters ...func(t *list.List) bool) ([]*list.Lis
}
func (a *Ajax) storageListTasks(listID string, filters ...func(t *task.Task) bool) ([]*task.Task, error) {
results, err := a.DB.List(nil, path.Join("list", listID, "task"), path.Join("list", listID, "task", "}"))
results, err := a.DB.List(nil, path.Join("list", listID, "incomplete", "task"), path.Join("list", listID, "incomplete", "task", "}"))
if err != nil {
return nil, err
}
includeComplete := true
for _, f := range filters {
t := &task.Task{Complete: true}
if !f(t) {
includeComplete = false
}
}
if includeComplete {
completeResults, err := a.DB.List(nil, path.Join("list", listID, "complete", "task"), path.Join("list", listID, "complete", "task", "}"))
if err != nil {
return nil, err
}
results = append(results, completeResults...)
}
tasks := []*task.Task{}
for _, result := range results {
taskID := path.Base(result)
@@ -83,6 +98,9 @@ func (a *Ajax) storageListTasks(listID string, filters ...func(t *task.Task) boo
}
}
sort.SliceStable(tasks, func(i, j int) bool {
if tasks[i].Complete != tasks[j].Complete {
return tasks[j].Complete
}
if tasks[i].Index == tasks[j].Index {
return tasks[i].Created.Before(tasks[j].Created)
}
@@ -97,7 +115,10 @@ func (a *Ajax) storageGetTask(taskID string) (*task.Task, error) {
return nil, err
}
var task task.Task
err := a.storageGet(path.Join("list", listID, "task", taskID), &task)
err := a.storageGet(path.Join("list", listID, "incomplete", "task", taskID), &task)
if err == storage.ErrNotFound {
err = a.storageGet(path.Join("list", listID, "complete", "task", taskID), &task)
}
return &task, err
}
@@ -110,7 +131,11 @@ func (a *Ajax) storageSetTask(listID string, newTask *task.Task) error {
if err := a.storageSet(path.Join("task", newTask.UUID, "map"), listID); err != nil {
return err
}
return a.storageSet(path.Join("list", listID, "task", newTask.UUID), *newTask)
scope := "incomplete"
if newTask.Complete {
scope = "complete"
}
return a.storageSet(path.Join("list", listID, scope, "task", newTask.UUID), *newTask)
}
func (a *Ajax) storageDelTask(taskID string) error {
@@ -121,7 +146,11 @@ func (a *Ajax) storageDelTask(taskID string) error {
if err := a.storageDel(path.Join("task", taskID, "map")); err != nil {
return err
}
return a.storageDel(path.Join("list", listID, "task", taskID))
err := a.storageDel(path.Join("list", listID, "incomplete", "task", taskID))
if err == nil {
err = a.storageDel(path.Join("list", listID, "complete", "task", taskID))
}
return err
}
func (a *Ajax) storageGetList(listID string) (*list.List, error) {