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

@@ -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))

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)

58
templates/entity-add.html Normal file
View File

@@ -0,0 +1,58 @@
<tr id="datarow-NEW_ITEM_ROW" class="datarow border-b dark:border-neutral-500">
<td class="whitespace-nowrap px-6 py-4">
<!-- <input hidden disabled type="text" name="id" value="NEW_ITEM_ROW" data-include-edit="NEW_ITEM_ROW" />
<span>NEW_ITEM_ROW</span> -->
</td>
<td class="whitespace-nowrap px-6 py-4">
<input
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-edit="NEW_ITEM_ROW"
name="name"
/>
</td>
<td class="whitespace-nowrap px-6 py-4">
<input
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-edit="NEW_ITEM_ROW"
name="stage"
/>
</td>
<td class="whitespace-nowrap px-6 py-4">
<input
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-edit="NEW_ITEM_ROW"
name="category"
/>
</td>
<td class="whitespace-nowrap px-6 py-4">
<input
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-edit="NEW_ITEM_ROW"
name="description"
/>
</td>
<td class="whitespace-nowrap px-6 py-4">
<input
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-edit="NEW_ITEM_ROW"
name="notes"
/>
<td class="whitespace-nowrap px-1 py-1">
<a
hx-post="/items/add"
hx-target="#datarow-NEW_ITEM_ROW"
hx-swap="outerHTML"
hx-indicator="#processing"
hx-include="input[data-include-edit='NEW_ITEM_ROW']"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-blue-100 transition-colors duration-150 bg-blue-700 rounded-lg focus:shadow-outline hover:bg-blue-800"
href=""
>Add</a
>
</td>
</tr>

View File

@@ -48,8 +48,6 @@
value="{{.Notes}}"
/>
<td class="whitespace-nowrap px-1 py-1">
<a
hx-put="/items/save/{{.ID}}"

View File

@@ -6,9 +6,9 @@
<div class="overflow-hidden">
<!-- <div class="flex justify-end">
<button
hx-get="/company/add"
hx-get="/item/add"
hx-target="#table-body"
hx-swap="outerHTML"
hx-swap="beforeend"
hx-indicator="#processing"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-blue-100 transition-colors duration-150 bg-blue-700 rounded-lg focus:shadow-outline hover:bg-blue-800"
href=""
@@ -30,8 +30,10 @@
</thead>
<tbody id="table-body">
{{range .}}
{{ template "entity-row.html". }}
{{ template "entity-row.html" . }}
{{end}}
{{ template "entity-add.html" . }}
</tbody>
</table>
</div>