Add structs and corresponding API routes
This commit is contained in:
parent
16d2285ff3
commit
977a75819d
12 changed files with 942 additions and 1 deletions
83
api/item.go
Normal file
83
api/item.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"git.nemunai.re/checkhome/struct"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/items/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.GetItems()
|
||||
}))
|
||||
router.DELETE("/api/items/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.ClearItems()
|
||||
}))
|
||||
|
||||
router.GET("/api/rooms/:rid/items/", apiHandler(roomHandler(func (room ckh.Room, _ []byte) (interface{}, error) {
|
||||
return room.GetItems()
|
||||
})))
|
||||
router.POST("/api/rooms/:rid/items/", apiHandler(roomHandler(newItem)))
|
||||
router.DELETE("/api/rooms/:rid/items/", apiHandler(roomHandler(func (room ckh.Room, _ []byte) (interface{}, error) {
|
||||
return room.ClearItems()
|
||||
})))
|
||||
|
||||
router.GET("/api/rooms/:rid/items/:iid/", apiHandler(itemHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
||||
return item, nil
|
||||
})))
|
||||
router.PUT("/api/rooms/:rid/items/:iid/", apiHandler(itemHandler(updateItem)))
|
||||
router.DELETE("/api/rooms/:rid/items/:iid/", apiHandler(itemHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
||||
return item.Delete()
|
||||
})))
|
||||
}
|
||||
|
||||
func itemHandler(f func(ckh.Item, ckh.Room, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
return roomHandler(func (room ckh.Room, _ []byte) (interface{}, error) {
|
||||
if iid, err := strconv.ParseInt(string(ps.ByName("iid")), 10, 64); err != nil {
|
||||
return nil, err
|
||||
} else if item, err := room.GetItem(iid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(item, room, body)
|
||||
}
|
||||
})(ps, body)
|
||||
}
|
||||
}
|
||||
|
||||
func newItem(room ckh.Room, body []byte) (interface{}, error) {
|
||||
var ui ckh.Item
|
||||
if err := json.Unmarshal(body, &ui); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ui.Label) == 0 {
|
||||
return nil, errors.New("Item's label cannot be empty")
|
||||
}
|
||||
|
||||
return ckh.NewItem(ui.Label, ui.Description, room)
|
||||
}
|
||||
|
||||
func updateItem(item ckh.Item, room ckh.Room, body []byte) (interface{}, error) {
|
||||
var ui ckh.Item
|
||||
if err := json.Unmarshal(body, &ui); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ui.Label) == 0 {
|
||||
return nil, errors.New("Item's label cannot be empty")
|
||||
}
|
||||
|
||||
ui.Id = item.Id
|
||||
ui.IdRoom = room.Id
|
||||
|
||||
if _, err := ui.Update(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return ui, nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue