diff --git a/server/ajax/storage.go b/server/ajax/storage.go index e43013c..34a2605 100755 --- a/server/ajax/storage.go +++ b/server/ajax/storage.go @@ -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) { diff --git a/server/ajax/task.go b/server/ajax/task.go index 2f86d3b..5ac92e6 100755 --- a/server/ajax/task.go +++ b/server/ajax/task.go @@ -6,7 +6,6 @@ import ( "fmt" "local/todo-server/server/ajax/form" "local/todo-server/server/ajax/task" - "log" "net/http" "strconv" "strings" @@ -47,10 +46,6 @@ func (a *Ajax) loadTasks(w http.ResponseWriter, r *http.Request) error { if err != nil { return err } - log.Println(listID) - for _, task := range tasks { - log.Println(task.Index, task.Title) - } return json.NewEncoder(w).Encode(map[string]interface{}{"list": tasks}) } @@ -97,6 +92,9 @@ func (a *Ajax) completeTask(w http.ResponseWriter, r *http.Request) error { if err != nil { return err } + if err := a.storageDelTask(completedTask.UUID); err != nil { + return err + } completedTask.SetComplete(form.Get(r, "compl") == "1") if err := a.storageSetTask(listID, completedTask); err != nil { return err diff --git a/server/ajax/task/task.go b/server/ajax/task/task.go index a2f9213..8028486 100755 --- a/server/ajax/task/task.go +++ b/server/ajax/task/task.go @@ -119,14 +119,16 @@ func (t *Task) AppendTags(tags []string) { t.Tags = append(t.Tags, tags...) } -func (t *Task) SetComplete(state bool) { +func (t *Task) SetComplete(state bool) bool { t.touch() + changed := t.Complete != state t.Complete = state if t.Complete { t.Completed = time.Now() } else { t.Completed = time.Time{} } + return changed } func (t *Task) SetPrio(prio int) { diff --git a/source_to_run_loaded.sh b/source_to_run_loaded.sh new file mode 100644 index 0000000..b0878d6 --- /dev/null +++ b/source_to_run_loaded.sh @@ -0,0 +1,39 @@ +#! /bin/bash + +go run main.go & +until curl localhost:39909 > /dev/null 2>&1; do + sleep 1 +done + +listid="" +for ((i=0; i<3; i++)); do + nlistid="$( \ + curl \ + -X POST \ + localhost:39909/ajax.php?addList \ + -d "name=Todo$i" \ + 2> /dev/null \ + | jq -r '.list[0].id' \ + )" + listid=${listid:-$nlistid} + curl \ + -X POST \ + localhost:39909/ajax.php?newTask \ + -d "list=$listid&title=abc${i}&tag=" +done + +taskid=$( \ + curl \ + -X POST \ + localhost:39909/ajax.php?newTask \ + -d "list=$listid&title=abc${i}&tag=" \ + 2> /dev/null \ + | jq -r '.list[0].id' \ +) +curl \ + -X POST \ + localhost:39909/ajax.php?completeTask=$taskid \ + -d "id=$taskid&compl=1" + +jobs +fg