item put functionality

This commit is contained in:
2024-03-10 05:17:46 +00:00
parent 01f25d4f71
commit 95171706da
7 changed files with 275 additions and 78 deletions

View File

@@ -10,22 +10,22 @@ 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
}
func Items(_html *template.Template) *ItemActions {
html = _html
return &ItemActions{
Get: Get,
Get: Get,
GetAll: GetAllItems,
Edit: EditItem,
Delete: nil,
Save: nil,
Save: Put,
}
}
@@ -99,3 +99,82 @@ func GetItemByID(r *http.Request) *web.Response {
nil,
)
}
func Put(r *http.Request) *web.Response {
id, _ := web.PathLast(r)
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")
// id := r.Form.Get("id")
item := db.Item{
ID: func() int {
idInt, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return -1
}
return int(idInt)
}(),
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.PutItem(item)
if err != nil {
return web.Error(http.StatusInternalServerError, err, nil)
}
return web.HTML(
http.StatusOK,
html,
"entity-row.html",
item,
nil,
)
}
func Delete(r *http.Request) *web.Response {
idFromPath, _ := web.PathLast(r)
id, err := strconv.ParseInt(idFromPath, 10, 64)
if err != nil {
return web.Error(http.StatusBadRequest, err, nil)
}
_, err = db.DeleteItem(int(id))
if err != nil {
return web.Error(http.StatusInternalServerError, err, nil)
}
return web.HTML(
http.StatusOK,
html,
"entity-row.html",
nil,
nil,
)
}