112 lines
2.5 KiB
Go
Executable File
112 lines
2.5 KiB
Go
Executable File
package ajax
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"local/todo-server/server/ajax/form"
|
|
"local/todo-server/server/ajax/list"
|
|
"net/http"
|
|
"sort"
|
|
)
|
|
|
|
type List struct{}
|
|
|
|
func (a *Ajax) loadLists(w http.ResponseWriter, r *http.Request) error {
|
|
lists, err := a.storageListLists()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.ListCnt = len(lists)
|
|
sort.Slice(lists, func(i, j int) bool {
|
|
return lists[i].Index < lists[j].Index
|
|
})
|
|
return json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"total": len(lists),
|
|
"list": lists,
|
|
})
|
|
}
|
|
|
|
func (a *Ajax) addList(w http.ResponseWriter, r *http.Request) error {
|
|
newList, err := list.New(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.ListCnt += 1
|
|
newList.Index = a.ListCnt
|
|
if err := a.storageSetList(newList); err != nil {
|
|
return err
|
|
}
|
|
return json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"total": 1,
|
|
"list": []*list.List{newList},
|
|
})
|
|
}
|
|
|
|
func (a *Ajax) renameList(w http.ResponseWriter, r *http.Request) error {
|
|
uuid := form.Get(r, "list")
|
|
renameList, err := a.storageGetList(uuid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
renameList.SetName(form.Get(r, "name"))
|
|
if err := a.storageSetList(renameList); err != nil {
|
|
return err
|
|
}
|
|
return json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"total": 1,
|
|
"list": []*list.List{renameList},
|
|
})
|
|
}
|
|
|
|
func (a *Ajax) deleteList(w http.ResponseWriter, r *http.Request) error {
|
|
uuid := form.Get(r, "list")
|
|
if err := a.storageDelList(uuid); err != nil {
|
|
return err
|
|
}
|
|
return json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"total": 1,
|
|
})
|
|
}
|
|
|
|
func (a *Ajax) setSort(w http.ResponseWriter, r *http.Request) error {
|
|
return errors.New("not impl")
|
|
}
|
|
|
|
func (a *Ajax) publishList(w http.ResponseWriter, r *http.Request) error {
|
|
return errors.New("not impl")
|
|
}
|
|
|
|
func (a *Ajax) changeListOrder(w http.ResponseWriter, r *http.Request) error {
|
|
order := form.GetAll(r, "order[]")
|
|
lists, err := a.storageListLists()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i := range order {
|
|
listUUID := order[i]
|
|
for j := range lists {
|
|
if lists[j].UUID == listUUID {
|
|
lists[j].Index = i
|
|
if err := a.storageSetList(lists[j]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"total": len(order),
|
|
})
|
|
}
|
|
|
|
func (a *Ajax) clearCompletedInList(w http.ResponseWriter, r *http.Request) error {
|
|
return errors.New("not impl")
|
|
}
|
|
|
|
func (a *Ajax) setShowNotesInList(w http.ResponseWriter, r *http.Request) error {
|
|
return errors.New("not impl")
|
|
}
|
|
|
|
func (a *Ajax) setHideList(w http.ResponseWriter, r *http.Request) error {
|
|
return errors.New("not impl")
|
|
}
|