diff --git a/main.go b/main.go index 6d1b452..ca61146 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,9 @@ func main() { router.Handle("/items/save/", web.Action(itemActions.Save)) router.Handle("/items/save/:id", web.Action(itemActions.Save)) + router.Handle("/items/add", web.Action(itemActions.Post)) + router.Handle("/items/add/", web.Action(itemActions.Post)) + router.Handle("/items", web.Action(itemActions.Get)) router.Handle("/boxes", web.Action(boxActions.GetAll)) router.Handle("/items/", web.Action(itemActions.Get)) diff --git a/routes/items.go b/routes/items.go index 9672d9d..38b7152 100644 --- a/routes/items.go +++ b/routes/items.go @@ -10,11 +10,13 @@ import ( ) type ItemActions struct { - Get func(r *http.Request) *web.Response - GetAll func(r *http.Request) *web.Response - Edit func(r *http.Request) *web.Response - Delete func(r *http.Request) *web.Response - Save func(r *http.Request) *web.Response + Get func(r *http.Request) *web.Response + GetAll func(r *http.Request) *web.Response + Edit func(r *http.Request) *web.Response + Delete func(r *http.Request) *web.Response + Save func(r *http.Request) *web.Response + Post func(r *http.Request) *web.Response + Add func(r *http.Request) *web.Response } func Items(_html *template.Template) *ItemActions { @@ -26,6 +28,8 @@ func Items(_html *template.Template) *ItemActions { Edit: EditItem, Delete: nil, Save: Put, + Post: Post, + Add: Add, } } @@ -156,6 +160,59 @@ func Put(r *http.Request) *web.Response { ) } +func Post(r *http.Request) *web.Response { + err := r.ParseForm() + if err != nil { + return web.Error(http.StatusBadRequest, err, nil) + } + + name := r.Form.Get("name") + stage := r.Form.Get("stage") + category := r.Form.Get("category") + description := r.Form.Get("description") + notes := r.Form.Get("notes") + + item := db.Item{ + Name: name, + Description: &description, + Notes: ¬es, + + Stage: func() db.PackingStage { + stageInt, _ := strconv.Atoi(stage) + return db.PackingStage(stageInt) + }(), + + Category: func() db.Category { + categoryInt, _ := strconv.Atoi(category) + return db.Category(categoryInt) + }(), + } + + _, err = db.PostItem(item) + + if err != nil { + return web.Error(http.StatusInternalServerError, err, nil) + } + + return web.HTML( + http.StatusOK, + html, + "entity-row.html", + item, + nil, + ) +} + +func Add(r *http.Request) *web.Response { + return web.HTML( + http.StatusOK, + html, + "entity-add.html", + nil, + nil, + ) +} + func Delete(r *http.Request) *web.Response { idFromPath, _ := web.PathLast(r) id, err := strconv.ParseInt(idFromPath, 10, 64) diff --git a/templates/entity-add.html b/templates/entity-add.html new file mode 100644 index 0000000..916ed23 --- /dev/null +++ b/templates/entity-add.html @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + Add + + diff --git a/templates/entity-edit.html b/templates/entity-edit.html index 87daa2c..37847f0 100644 --- a/templates/entity-edit.html +++ b/templates/entity-edit.html @@ -48,8 +48,6 @@ value="{{.Notes}}" /> - -