todo-server/server/ajax/ajax.go

84 lines
2.1 KiB
Go
Executable File

package ajax
import (
"local/storage"
"local/todo-server/config"
"net/http"
"net/url"
)
type Ajax struct {
DB storage.DB
}
func New() (*Ajax, error) {
db, err := storage.New(storage.TypeFromString(config.StoreType), config.StoreAddr, config.StoreUser, config.StorePass)
return &Ajax{
DB: db,
}, err
}
func (a *Ajax) HandleAjax(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
var foo func(http.ResponseWriter, *http.Request) error
if has(params, "loadLists") {
foo = a.loadLists
} else if has(params, "loadTasks") {
foo = a.loadTasks
} else if has(params, "newTask") {
foo = a.newTask
} else if has(params, "fullNewTask") {
foo = a.newTask
} else if has(params, "deleteTask") {
foo = a.deleteTask
} else if has(params, "completeTask") {
foo = a.completeTask
} else if has(params, "editNote") {
foo = a.editNote
} else if has(params, "editTask") {
foo = a.editTask
} else if has(params, "changeOrder") {
foo = a.changeOrder
} else if has(params, "suggestTags") {
foo = a.suggestTags
} else if has(params, "setPrio") {
foo = a.setPrio
} else if has(params, "tagCloud") {
foo = a.tagCloud
} else if has(params, "addList") {
foo = a.addList
} else if has(params, "renameList") {
foo = a.renameList
} else if has(params, "deleteList") {
foo = a.deleteList
} else if has(params, "setSort") {
foo = a.setSort
} else if has(params, "publishList") {
foo = a.publishList
} else if has(params, "moveTask") {
foo = a.moveTask
} else if has(params, "changeListOrder") {
foo = a.changeListOrder
} else if has(params, "parseTaskStr") {
foo = a.parseTaskStr
} else if has(params, "clearCompletedInList") {
foo = a.clearCompletedInList
} else if has(params, "setShowNotesInList") {
foo = a.setShowNotesInList
} else if has(params, "setHideList") {
foo = a.setHideList
}
if foo == nil {
http.NotFound(w, r)
} else if err := foo(w, r); err == storage.ErrNotFound {
http.Error(w, err.Error(), http.StatusNotFound)
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func has(params url.Values, k string) bool {
_, ok := params[k]
return ok
}