enabled more crud operations

This commit is contained in:
2024-03-10 05:48:26 +00:00
parent 95171706da
commit 1ce9a659ed
5 changed files with 128 additions and 10 deletions

View File

@@ -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: &notes,
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)