wip1
This commit is contained in:
parent
977a75819d
commit
f25068075c
52
api/check.go
52
api/check.go
@ -15,35 +15,63 @@ func init() {
|
|||||||
return ckh.GetChecks()
|
return ckh.GetChecks()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
router.GET("/api/rooms/:rid/items/:iid/checks/", apiHandler(itemHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
router.GET("/api/items/:iid/checks/", apiHandler(itemHandler(func (item ckh.Item, _ []byte) (interface{}, error) {
|
||||||
return item.GetChecks()
|
return item.GetChecks()
|
||||||
})))
|
})))
|
||||||
router.POST("/api/rooms/:rid/items/:iid/checks/", apiHandler(itemHandler(newCheck)))
|
router.POST("/api/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) {
|
router.GET("/api/items/:iid/checks/:cid", apiHandler(checkHandler(func (check ckh.Check, _ ckh.Item, _ []byte) (interface{}, error) {
|
||||||
return check, nil
|
return check, nil
|
||||||
})))
|
})))
|
||||||
router.PUT("/api/rooms/:rid/items/:iid/checks/:cid", apiHandler(checkHandler(updateCheck)))
|
router.PUT("/api/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) {
|
router.DELETE("/api/items/:iid/checks/:cid", apiHandler(checkHandler(func (check ckh.Check, _ ckh.Item, _ []byte) (interface{}, error) {
|
||||||
|
return check.Delete()
|
||||||
|
})))
|
||||||
|
|
||||||
|
// in room
|
||||||
|
router.GET("/api/rooms/:rid/items/:iid/checks/", apiHandler(itemInRoomHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
||||||
|
return item.GetChecks()
|
||||||
|
})))
|
||||||
|
router.POST("/api/rooms/:rid/items/:iid/checks/", apiHandler(itemInRoomHandler(newCheckInRoom)))
|
||||||
|
|
||||||
|
router.GET("/api/rooms/:rid/items/:iid/checks/:cid", apiHandler(checkInRoomHandler(func (check ckh.Check, _ ckh.Item, _ []byte) (interface{}, error) {
|
||||||
|
return check, nil
|
||||||
|
})))
|
||||||
|
router.PUT("/api/rooms/:rid/items/:iid/checks/:cid", apiHandler(checkInRoomHandler(updateCheck)))
|
||||||
|
router.DELETE("/api/rooms/:rid/items/:iid/checks/:cid", apiHandler(checkInRoomHandler(func (check ckh.Check, _ ckh.Item, _ []byte) (interface{}, error) {
|
||||||
return check.Delete()
|
return check.Delete()
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkHandler(f func(ckh.Check, ckh.Item, ckh.Room, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
func checkHandler(f func(ckh.Check, ckh.Item, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||||
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||||
return itemHandler(func (item ckh.Item, room ckh.Room, _ []byte) (interface{}, error) {
|
return itemHandler(func (item ckh.Item, _ []byte) (interface{}, error) {
|
||||||
if cid, err := strconv.ParseInt(string(ps.ByName("cid")), 10, 64); err != nil {
|
if cid, err := strconv.ParseInt(string(ps.ByName("cid")), 10, 64); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if check, err := item.GetCheck(cid); err != nil {
|
} else if check, err := item.GetCheck(cid); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return f(check, item, room, body)
|
return f(check, item, body)
|
||||||
}
|
}
|
||||||
})(ps, body)
|
})(ps, body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCheck(item ckh.Item, _ ckh.Room, body []byte) (interface{}, error) {
|
func checkInRoomHandler(f func(ckh.Check, ckh.Item, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||||
|
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||||
|
return itemInRoomHandler(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, body)
|
||||||
|
}
|
||||||
|
})(ps, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newCheck(item ckh.Item, body []byte) (interface{}, error) {
|
||||||
var uc ckh.Check
|
var uc ckh.Check
|
||||||
if err := json.Unmarshal(body, &uc); err != nil {
|
if err := json.Unmarshal(body, &uc); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -56,7 +84,11 @@ func newCheck(item ckh.Item, _ ckh.Room, body []byte) (interface{}, error) {
|
|||||||
return item.NewCheck(ckh.User{}, uc.Passed, uc.Comment)
|
return item.NewCheck(ckh.User{}, uc.Passed, uc.Comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateCheck(check ckh.Check, _ ckh.Item, _ ckh.Room, body []byte) (interface{}, error) {
|
func newCheckInRoom(item ckh.Item, _ ckh.Room, body []byte) (interface{}, error) {
|
||||||
|
return newCheck(item, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateCheck(check ckh.Check, _ ckh.Item, body []byte) (interface{}, error) {
|
||||||
var uc ckh.Check
|
var uc ckh.Check
|
||||||
if err := json.Unmarshal(body, &uc); err != nil {
|
if err := json.Unmarshal(body, &uc); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
20
api/item.go
20
api/item.go
@ -26,16 +26,28 @@ func init() {
|
|||||||
return room.ClearItems()
|
return room.ClearItems()
|
||||||
})))
|
})))
|
||||||
|
|
||||||
router.GET("/api/rooms/:rid/items/:iid/", apiHandler(itemHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
router.GET("/api/rooms/:rid/items/:iid/", apiHandler(itemInRoomHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
||||||
return item, nil
|
return item, nil
|
||||||
})))
|
})))
|
||||||
router.PUT("/api/rooms/:rid/items/:iid/", apiHandler(itemHandler(updateItem)))
|
router.PUT("/api/rooms/:rid/items/:iid/", apiHandler(itemInRoomHandler(updateItem)))
|
||||||
router.DELETE("/api/rooms/:rid/items/:iid/", apiHandler(itemHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
router.DELETE("/api/rooms/:rid/items/:iid/", apiHandler(itemInRoomHandler(func (item ckh.Item, _ ckh.Room, _ []byte) (interface{}, error) {
|
||||||
return item.Delete()
|
return item.Delete()
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
func itemHandler(f func(ckh.Item, ckh.Room, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
func itemHandler(f func(ckh.Item, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||||
|
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||||
|
if iid, err := strconv.ParseInt(string(ps.ByName("iid")), 10, 64); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if item, err := ckh.GetItem(iid); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return f(item, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func itemInRoomHandler(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 func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||||
return roomHandler(func (room ckh.Room, _ []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 {
|
if iid, err := strconv.ParseInt(string(ps.ByName("iid")), 10, 64); err != nil {
|
||||||
|
28
api/tag.go
28
api/tag.go
@ -24,7 +24,23 @@ func init() {
|
|||||||
})))
|
})))
|
||||||
router.PUT("/api/tags/:tid", apiHandler(tagHandler(updateTag)))
|
router.PUT("/api/tags/:tid", apiHandler(tagHandler(updateTag)))
|
||||||
router.DELETE("/api/tags/:tid", apiHandler(tagHandler(func (tag ckh.Tag, _ []byte) (interface{}, error) {
|
router.DELETE("/api/tags/:tid", apiHandler(tagHandler(func (tag ckh.Tag, _ []byte) (interface{}, error) {
|
||||||
return tag.Delete()
|
if _, err := tag.Delete(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return map[string]int{}, nil
|
||||||
|
}
|
||||||
|
})))
|
||||||
|
|
||||||
|
// Item's tags
|
||||||
|
router.GET("/api/items/:iid/tags", apiHandler(itemHandler(func (item ckh.Item, _ []byte) (interface{}, error) {
|
||||||
|
return item.GetTags()
|
||||||
|
})))
|
||||||
|
router.PUT("/api/items/:iid/tags/:tid", apiHandler(itemTagHandler(func (item ckh.Item, tag ckh.Tag, _ []byte) (interface{}, error) {
|
||||||
|
_, err := tag.AddItem(item)
|
||||||
|
return tag, err
|
||||||
|
})))
|
||||||
|
router.DELETE("/api/items/:iid/tags/:tid", apiHandler(itemTagHandler(func (item ckh.Item, tag ckh.Tag, _ []byte) (interface{}, error) {
|
||||||
|
return tag.DeleteItem(item)
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +56,16 @@ func tagHandler(f func(ckh.Tag, []byte) (interface{}, error)) func(httprouter.Pa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func itemTagHandler(f func(ckh.Item, ckh.Tag, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||||
|
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||||
|
return itemHandler(func (item ckh.Item, _ []byte) (interface{}, error) {
|
||||||
|
return tagHandler(func (tag ckh.Tag, _ []byte) (interface{}, error) {
|
||||||
|
return f(item, tag, body)
|
||||||
|
})(ps, body)
|
||||||
|
})(ps, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func newTag(_ httprouter.Params, body []byte) (interface{}, error) {
|
func newTag(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||||
var ut ckh.Tag
|
var ut ckh.Tag
|
||||||
if err := json.Unmarshal(body, &ut); err != nil {
|
if err := json.Unmarshal(body, &ut); err != nil {
|
||||||
|
@ -72,12 +72,11 @@ func updateUser(user ckh.User, body []byte) (interface{}, error) {
|
|||||||
return nil, errors.New("Username cannot be empty")
|
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.Username = uu.Username
|
||||||
user.ChangePassword(uu.Password)
|
|
||||||
|
if len(uu.Password) != 0 {
|
||||||
|
user.ChangePassword(uu.Password)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := user.Update(); err != nil {
|
if _, err := user.Update(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
2
main.go
2
main.go
@ -18,7 +18,7 @@ import (
|
|||||||
"git.nemunai.re/checkhome/struct"
|
"git.nemunai.re/checkhome/struct"
|
||||||
)
|
)
|
||||||
|
|
||||||
var StaticDir string
|
var StaticDir string = "./static/"
|
||||||
|
|
||||||
type ResponseWriterPrefix struct {
|
type ResponseWriterPrefix struct {
|
||||||
real http.ResponseWriter
|
real http.ResponseWriter
|
||||||
|
@ -65,7 +65,7 @@ func (i Item) GetCheck(id int64) (c Check, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i Item) NewCheck(user User, state string, comment string) (Check, error) {
|
func (i Item) NewCheck(user User, state string, comment string) (Check, error) {
|
||||||
if res, err := DBExec("INSERT INTO checks (id_item, id_user, passed, comment) VALUES (?, ?, ?)", i.Id, user.Id, state, comment); err != nil {
|
if res, err := DBExec("INSERT INTO checks (id_item, id_user, passed, comment) VALUES (?, ?, ?, ?)", i.Id, user.Id, state, comment); err != nil {
|
||||||
return Check{}, err
|
return Check{}, err
|
||||||
} else if cid, err := res.LastInsertId(); err != nil {
|
} else if cid, err := res.LastInsertId(); err != nil {
|
||||||
return Check{}, err
|
return Check{}, err
|
||||||
|
@ -53,6 +53,28 @@ func ClearItemTags() (int64, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i Item) GetTags() (tags []Tag, err error) {
|
||||||
|
if rows, errr := DBQuery("SELECT T.id_tag, T.label FROM item_tag IT INNER JOIN tags T ON T.id_tag = IT.id_tag WHERE id_item = ?", i.Id); errr != nil {
|
||||||
|
return nil, errr
|
||||||
|
} else {
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
tags = make([]Tag, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
var t Tag
|
||||||
|
if err = rows.Scan(&t.Id, &t.Label); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tags = append(tags, t)
|
||||||
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (i Item) ClearItemTags() (int64, error) {
|
func (i Item) ClearItemTags() (int64, error) {
|
||||||
if res, err := DBExec("DELETE FROM item_tag WHERE id_item = ?", i.Id); err != nil {
|
if res, err := DBExec("DELETE FROM item_tag WHERE id_item = ?", i.Id); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
Loading…
Reference in New Issue
Block a user