148 lines
3.2 KiB
Go
148 lines
3.2 KiB
Go
package ajax
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"local/todo-server/server/ajax/form"
|
|
"local/todo-server/server/ajax/list"
|
|
"local/todo-server/server/ajax/task"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
func (a *Ajax) Cur(r *http.Request) (string, string, []string) {
|
|
listID := form.Get(r, "list")
|
|
taskID := form.Get(r, "id")
|
|
tags, _ := r.URL.Query()["t"]
|
|
return listID, taskID, tags
|
|
}
|
|
|
|
func (a *Ajax) storageListLists(filters ...func(t *list.List) bool) ([]*list.List, error) {
|
|
results, err := a.DB.List(nil, "", "}")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
lists := []*list.List{}
|
|
for _, result := range results {
|
|
if d, f := path.Split(result); d == "" || f == "" {
|
|
continue
|
|
}
|
|
list := &list.List{
|
|
ID: result,
|
|
UUID: result,
|
|
}
|
|
filtered := true
|
|
for _, f := range filters {
|
|
if !f(list) {
|
|
filtered = false
|
|
break
|
|
}
|
|
}
|
|
if filtered {
|
|
lists = append(lists, list)
|
|
}
|
|
}
|
|
return lists, nil
|
|
}
|
|
|
|
func (a *Ajax) storageListTasks(listID string, filters ...func(t *task.Task) bool) ([]*task.Task, error) {
|
|
results, err := a.DB.List(nil, listID+"/", listID+"/}")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tasks := []*task.Task{}
|
|
for _, result := range results {
|
|
taskID := strings.TrimPrefix(result, listID+"/")
|
|
task, err := a.storageGetTask(listID, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
filtered := true
|
|
for _, f := range filters {
|
|
if !f(task) {
|
|
filtered = false
|
|
break
|
|
}
|
|
}
|
|
if filtered {
|
|
tasks = append(tasks, task)
|
|
}
|
|
}
|
|
return tasks, nil
|
|
}
|
|
|
|
func (a *Ajax) storageGetTask(listID, taskID string) (*task.Task, error) {
|
|
var task task.Task
|
|
err := a.storageGet(path.Join(listID, taskID), &task)
|
|
return &task, err
|
|
}
|
|
|
|
func (a *Ajax) storageSetTask(listID, taskID string, newTask *task.Task) error {
|
|
if newTask == nil {
|
|
newTask = &task.Task{}
|
|
}
|
|
return a.storageSet(path.Join(listID, taskID), *newTask)
|
|
}
|
|
|
|
func (a *Ajax) storageDelTask(listID, taskID string) error {
|
|
return a.storageDel(path.Join(listID, taskID))
|
|
}
|
|
|
|
func (a *Ajax) storageGetList(listID string) (*list.List, error) {
|
|
var list list.List
|
|
err := a.storageGet(listID, &list)
|
|
return &list, err
|
|
}
|
|
|
|
func (a *Ajax) storageSetList(listID string, list *list.List) error {
|
|
return a.storageSet(listID, *list)
|
|
}
|
|
|
|
func (a *Ajax) storageDelList(listID string) error {
|
|
return a.storageDel(listID)
|
|
}
|
|
|
|
func (a *Ajax) storageSetCurTags(tags []string) error {
|
|
return a.storageSet("currentTags", tags)
|
|
}
|
|
|
|
func (a *Ajax) storageGetCurTags() ([]string, error) {
|
|
var tags []string
|
|
err := a.storageGet("currentTags", &tags)
|
|
return tags, err
|
|
}
|
|
|
|
func (a *Ajax) storageSetCur(listID string) error {
|
|
return a.storageSet("currentList", listID)
|
|
}
|
|
|
|
func (a *Ajax) storageGetCur() (string, error) {
|
|
var listID string
|
|
err := a.storageGet("currentList", &listID)
|
|
return listID, err
|
|
}
|
|
|
|
func (a *Ajax) storageSet(key string, value interface{}) error {
|
|
buff := bytes.NewBuffer(nil)
|
|
encoder := gob.NewEncoder(buff)
|
|
if err := encoder.Encode(value); err != nil {
|
|
return err
|
|
}
|
|
return a.DB.Set(key, buff.Bytes())
|
|
}
|
|
|
|
func (a *Ajax) storageGet(key string, value interface{}) error {
|
|
b, err := a.DB.Get(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
buff := bytes.NewBuffer(b)
|
|
decoder := gob.NewDecoder(buff)
|
|
return decoder.Decode(value)
|
|
}
|
|
|
|
func (a *Ajax) storageDel(key string) error {
|
|
return a.DB.Set(key, nil)
|
|
}
|