Add structs and corresponding API routes
This commit is contained in:
parent
16d2285ff3
commit
977a75819d
12 changed files with 942 additions and 1 deletions
76
api/check.go
Normal file
76
api/check.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"git.nemunai.re/checkhome/struct"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/checks/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.GetChecks()
|
||||
}))
|
||||
|
||||
router.GET("/api/rooms/:rid/items/:iid/checks/", apiHandler(itemHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
||||
return item.GetChecks()
|
||||
})))
|
||||
router.POST("/api/rooms/:rid/items/:iid/checks/", apiHandler(itemHandler(newCheck)))
|
||||
|
||||
router.GET("/api/rooms/:rid/items/:iid/checks/:cid", apiHandler(checkHandler(func (check ckh.Check, _ ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
||||
return check, nil
|
||||
})))
|
||||
router.PUT("/api/rooms/:rid/items/:iid/checks/:cid", apiHandler(checkHandler(updateCheck)))
|
||||
router.DELETE("/api/rooms/:rid/items/:iid/checks/:cid", apiHandler(checkHandler(func (check ckh.Check, _ ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
||||
return check.Delete()
|
||||
})))
|
||||
}
|
||||
|
||||
func checkHandler(f func(ckh.Check, ckh.Item, ckh.Room, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
return itemHandler(func (item ckh.Item, room ckh.Room, _ []byte) (interface{}, error) {
|
||||
if cid, err := strconv.ParseInt(string(ps.ByName("cid")), 10, 64); err != nil {
|
||||
return nil, err
|
||||
} else if check, err := item.GetCheck(cid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(check, item, room, body)
|
||||
}
|
||||
})(ps, body)
|
||||
}
|
||||
}
|
||||
|
||||
func newCheck(item ckh.Item, _ ckh.Room, body []byte) (interface{}, error) {
|
||||
var uc ckh.Check
|
||||
if err := json.Unmarshal(body, &uc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(uc.Passed) == 0 {
|
||||
return nil, errors.New("Check's state cannot be empty")
|
||||
}
|
||||
|
||||
return item.NewCheck(ckh.User{}, uc.Passed, uc.Comment)
|
||||
}
|
||||
|
||||
func updateCheck(check ckh.Check, _ ckh.Item, _ ckh.Room, body []byte) (interface{}, error) {
|
||||
var uc ckh.Check
|
||||
if err := json.Unmarshal(body, &uc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(uc.Passed) == 0 {
|
||||
return nil, errors.New("Check's state cannot be empty")
|
||||
}
|
||||
|
||||
uc.Id = check.Id
|
||||
|
||||
if _, err := uc.Update(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return uc, nil
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
73
api/room.go
Normal file
73
api/room.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"git.nemunai.re/checkhome/struct"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/rooms/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.GetRooms()
|
||||
}))
|
||||
router.POST("/api/rooms/", apiHandler(newRoom))
|
||||
router.DELETE("/api/rooms/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.ClearRooms()
|
||||
}))
|
||||
|
||||
router.GET("/api/rooms/:rid", apiHandler(roomHandler(func (room ckh.Room, _ []byte) (interface{}, error) {
|
||||
return room, nil
|
||||
})))
|
||||
router.PUT("/api/rooms/:rid", apiHandler(roomHandler(updateRoom)))
|
||||
router.DELETE("/api/rooms/:rid", apiHandler(roomHandler(func (room ckh.Room, _ []byte) (interface{}, error) {
|
||||
return room.Delete()
|
||||
})))
|
||||
}
|
||||
|
||||
func roomHandler(f func(ckh.Room, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if rid, err := strconv.ParseInt(string(ps.ByName("rid")), 10, 64); err != nil {
|
||||
return nil, err
|
||||
} else if room, err := ckh.GetRoom(rid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(room, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newRoom(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ur ckh.Room
|
||||
if err := json.Unmarshal(body, &ur); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ur.Label) == 0 {
|
||||
return nil, errors.New("Room's label cannot be empty")
|
||||
}
|
||||
|
||||
return ckh.NewRoom(ur.Label)
|
||||
}
|
||||
|
||||
func updateRoom(room ckh.Room, body []byte) (interface{}, error) {
|
||||
var ur ckh.Room
|
||||
if err := json.Unmarshal(body, &ur); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ur.Label) == 0 {
|
||||
return nil, errors.New("Room's label cannot be empty")
|
||||
}
|
||||
|
||||
ur.Id = room.Id
|
||||
|
||||
if _, err := ur.Update(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return ur, nil
|
||||
}
|
||||
}
|
||||
73
api/tag.go
Normal file
73
api/tag.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"git.nemunai.re/checkhome/struct"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/tags/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.GetTags()
|
||||
}))
|
||||
router.POST("/api/tags/", apiHandler(newTag))
|
||||
router.DELETE("/api/tags/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.ClearTags()
|
||||
}))
|
||||
|
||||
router.GET("/api/tags/:tid", apiHandler(tagHandler(func (tag ckh.Tag, _ []byte) (interface{}, error) {
|
||||
return tag, nil
|
||||
})))
|
||||
router.PUT("/api/tags/:tid", apiHandler(tagHandler(updateTag)))
|
||||
router.DELETE("/api/tags/:tid", apiHandler(tagHandler(func (tag ckh.Tag, _ []byte) (interface{}, error) {
|
||||
return tag.Delete()
|
||||
})))
|
||||
}
|
||||
|
||||
func tagHandler(f func(ckh.Tag, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if tid, err := strconv.ParseInt(string(ps.ByName("tid")), 10, 64); err != nil {
|
||||
return nil, err
|
||||
} else if tag, err := ckh.GetTag(tid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(tag, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newTag(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ut ckh.Tag
|
||||
if err := json.Unmarshal(body, &ut); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ut.Label) == 0 {
|
||||
return nil, errors.New("Tag's label cannot be empty")
|
||||
}
|
||||
|
||||
return ckh.NewTag(ut.Label)
|
||||
}
|
||||
|
||||
func updateTag(tag ckh.Tag, body []byte) (interface{}, error) {
|
||||
var ut ckh.Tag
|
||||
if err := json.Unmarshal(body, &ut); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ut.Label) == 0 {
|
||||
return nil, errors.New("Tag's label cannot be empty")
|
||||
}
|
||||
|
||||
ut.Id = tag.Id
|
||||
|
||||
if _, err := ut.Update(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return ut, nil
|
||||
}
|
||||
}
|
||||
87
api/user.go
Normal file
87
api/user.go
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"git.nemunai.re/checkhome/struct"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/users/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.GetUsers()
|
||||
}))
|
||||
router.POST("/api/users/", apiHandler(newUser))
|
||||
router.DELETE("/api/users/", apiHandler(func (_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return ckh.ClearUsers()
|
||||
}))
|
||||
|
||||
router.GET("/api/users/:uid", apiHandler(userHandler(func (user ckh.User, _ []byte) (interface{}, error) {
|
||||
return user, nil
|
||||
})))
|
||||
router.PUT("/api/users/:uid", apiHandler(userHandler(updateUser)))
|
||||
router.DELETE("/api/users/:uid", apiHandler(userHandler(func (user ckh.User, _ []byte) (interface{}, error) {
|
||||
return user.Delete()
|
||||
})))
|
||||
}
|
||||
|
||||
func userHandler(f func(ckh.User, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if uid, err := strconv.ParseInt(string(ps.ByName("uid")), 10, 64); err != nil {
|
||||
return nil, err
|
||||
} else if user, err := ckh.GetUser(uid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(user, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type uploadedUser struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func newUser(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var uu uploadedUser
|
||||
if err := json.Unmarshal(body, &uu); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(uu.Username) == 0 {
|
||||
return nil, errors.New("Username cannot be empty")
|
||||
}
|
||||
|
||||
if len(uu.Password) == 0 {
|
||||
return nil, errors.New("Password cannot be empty")
|
||||
}
|
||||
|
||||
return ckh.NewUser(uu.Username, uu.Password)
|
||||
}
|
||||
|
||||
func updateUser(user ckh.User, body []byte) (interface{}, error) {
|
||||
var uu uploadedUser
|
||||
if err := json.Unmarshal(body, &uu); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(uu.Username) == 0 {
|
||||
return nil, errors.New("Username cannot be empty")
|
||||
}
|
||||
|
||||
if len(uu.Password) == 0 {
|
||||
return nil, errors.New("Password cannot be empty")
|
||||
}
|
||||
|
||||
user.Username = uu.Username
|
||||
user.ChangePassword(uu.Password)
|
||||
|
||||
if _, err := user.Update(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return user, nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue