fix for box item parsing issue

This commit is contained in:
2024-03-12 19:38:59 +00:00
parent a601a8308d
commit 4fd0e72a86
8 changed files with 280 additions and 24 deletions

View File

@@ -39,9 +39,16 @@ type Box struct {
Category Category `json:"category"`
}
// joins
// joining tables and derivative data types
type BoxItem struct {
ID int
BoxID int
ItemID int
}
type BoxItemWithItemInfo struct {
ID int
Name string
Stage PackingStage
Category Category
}

145
db/sql.go
View File

@@ -68,6 +68,38 @@ func GetAllBoxes() (result []Box, err error) {
return
}
func GetAllBoxItems() ([]BoxItem, error) {
db, err := CreateClient()
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("SELECT * FROM box_items")
if err != nil {
return nil, err
}
defer rows.Close()
var result []BoxItem
for rows.Next() {
boxItem := BoxItem{}
err = rows.Scan(&boxItem.ID, &boxItem.BoxID, &boxItem.ItemID)
if err != nil {
return nil, err
}
result = append(result, boxItem)
}
return result, nil
}
func GetItemByID(id int) (item Item, err error) {
db, err := CreateClient()
if err != nil {
@@ -96,6 +128,57 @@ func GetBoxByID(id int) (box Box, err error) {
return
}
func GetBoxItemByID(id int) (BoxItem, error) {
db, err := CreateClient()
if err != nil {
return BoxItem{}, err
}
defer db.Close()
row := db.QueryRow("SELECT * FROM box_items WHERE id = ?", id)
boxItem := BoxItem{}
err = row.Scan(&boxItem.ID, &boxItem.BoxID, &boxItem.ItemID)
return boxItem, err
}
func GetBoxItemsByBoxID(boxID int) ([]BoxItemWithItemInfo, error) {
db, err := CreateClient()
if err != nil {
return nil, err
}
defer db.Close()
// get all rows from box_items where boxid = boxID
// also get the item info for each item
rows, err := db.Query(
"SELECT id, items.name, items.stage, items.category FROM boxitems JOIN items ON itemid=items.id WHERE boxid = ?",
boxID)
if err != nil {
return nil, err
}
defer rows.Close()
result := []BoxItemWithItemInfo{}
for rows.Next() {
boxItem := BoxItemWithItemInfo{}
if err = rows.Scan(&boxItem.ID, &boxItem.Name, &boxItem.Stage, &boxItem.Category); err != nil {
return nil, err
}
result = append(result, boxItem)
}
return result, nil
}
func PostItem(item Item) (int64, error) {
db, err := CreateClient()
if err != nil {
@@ -135,6 +218,25 @@ func PostBox(box Box) (int64, error) {
return result.LastInsertId()
}
func PostBoxItem(boxItem BoxItem) (int64, error) {
db, err := CreateClient()
if err != nil {
return -1, err
}
defer db.Close()
query := `INSERT INTO box_items (boxid, item_id) VALUES (?, ?)`
result, err := db.Exec(query, boxItem.BoxID, boxItem.ItemID)
if err != nil {
return -1, err
}
return result.LastInsertId()
}
func PutItem(item Item) (int64, error) {
db, err := CreateClient()
if err != nil {
@@ -173,6 +275,25 @@ func PutBox(box Box) (int64, error) {
return result.RowsAffected()
}
func PutBoxItem(boxItem BoxItem) (int64, error) {
db, err := CreateClient()
if err != nil {
return -1, err
}
defer db.Close()
query := `UPDATE box_items SET boxid = ?, item_id = ? WHERE id = ?`
result, err := db.Exec(query, boxItem.BoxID, boxItem.ItemID, boxItem.ID)
if err != nil {
return -1, err
}
return result.RowsAffected()
}
func DeleteItem(id int) (int64, error) {
db, err := CreateClient()
if err != nil {
@@ -211,13 +332,21 @@ func DeleteBox(id int) (int64, error) {
return result.RowsAffected()
}
func DeleteBoxItem(id int) (int64, error) {
db, err := CreateClient()
if err != nil {
return -1, err
}
// func PostBoxItem(itemid int, boxid int) (int64, error) {
// db, err := CreateClient()
// if err != nil {
// return -1, err
// }
defer db.Close()
// defer db.Close()
// // query :=
// }
query := `DELETE FROM box_items WHERE id = ?`
result, err := db.Exec(query, id)
if err != nil {
return -1, err
}
return result.RowsAffected()
}

View File

@@ -45,6 +45,7 @@ func main() {
itemActions := routes.Items(html)
boxActions := routes.Boxes(html)
boxItemActions := routes.BoxItems(html)
router.Handle("/items/edit", web.Action(itemActions.Edit))
router.Handle("/items/delete", web.Action(itemActions.Delete))
@@ -58,6 +59,10 @@ func main() {
router.Handle("/items/add", web.Action(itemActions.Post))
router.Handle("/items/add/", web.Action(itemActions.Post))
router.Handle("/box-items", web.Action(boxItemActions.Get))
router.Handle("/box-items/", web.Action(boxItemActions.Get))
router.Handle("/box-items/:id", web.Action(boxItemActions.Get))
router.Handle("/items", web.Action(itemActions.Get))
router.Handle("/boxes", web.Action(boxActions.GetAll))
router.Handle("/items/", web.Action(itemActions.Get))

100
routes/boxItems.go Normal file
View File

@@ -0,0 +1,100 @@
package routes
import (
"html/template"
"net/http"
"strconv"
"github.com/innocuous-symmetry/moving-mgmt/db"
"github.com/jritsema/gotoolbox/web"
)
type BoxItemActions struct {
Get func(r *http.Request) *web.Response
GetAll func(r *http.Request) *web.Response
GetByID func(r *http.Request) *web.Response
GetByBoxID func(r *http.Request) *web.Response
}
func BoxItems(_html *template.Template) *BoxItemActions {
html = _html
return &BoxItemActions{
Get: BoxItemsHandler,
GetAll: nil,
GetByID: nil,
}
}
func BoxItemsHandler(r *http.Request) *web.Response {
switch r.Method {
case http.MethodGet:
if r.URL.Query().Has("boxid") {
return GetBoxItemsByBoxID(r)
}
_, count := web.PathLast(r)
if count == 1 {
return GetAllBoxItems(r)
} else {
return GetBoxItemByID(r)
}
default:
return nil
}
}
func GetAllBoxItems(_ *http.Request) *web.Response {
items, err := db.GetAllBoxItems()
if err != nil {
return web.Error(
http.StatusBadRequest,
err,
nil,
)
}
return web.HTML(
http.StatusOK,
html,
"box-items/box-item-list.html",
items,
nil,
)
}
func GetBoxItemsByBoxID(r *http.Request) *web.Response {
boxID := r.URL.Query().Get("boxid")
if id, err := strconv.ParseInt(boxID, 10, 64); err != nil {
return web.Error(
http.StatusBadRequest,
err,
nil,
)
} else {
items, err := db.GetBoxItemsByBoxID(int(id))
if err != nil {
return web.Error(
http.StatusNotFound,
err,
nil,
)
}
return web.HTML(
http.StatusOK,
html,
"box-items/box-item-list.html",
items,
nil,
)
}
}
func GetBoxItemByID(_ *http.Request) *web.Response {
return nil
}

View File

@@ -26,7 +26,7 @@ func Items(_html *template.Template) *ItemActions {
Get: Get,
GetAll: GetAllItems,
Edit: EditItem,
Delete: nil,
Delete: Delete,
Save: Put,
Post: Post,
Add: Add,

View File

@@ -0,0 +1,3 @@
<tr id="box-item-list">
<td>Hooray!</td>
</tr>

View File

@@ -1,11 +1,12 @@
<tr id="boxrow-{{.ID}}" class="border-b dark:border-neutral-500 hover:bg-slate-300 hover:bg-opacity-20">
<td
hx-get="/box-items?box_id={{ .ID }}"
<td class="whitespace-nowrap px-6 py-4">
<button
hx-get="/box-items?boxid={{ .ID }}"
hx-target="#boxrow-{{ .ID }}"
hx-swap="afterend"
class="whitespace-nowrap px-6 py-4 relative top-0 -left-12 inline-flex items-center h-8 m-2 text-sm text-stone-100 transition-colors duration-150 bg-stone-700 rounded-lg focus:shadow-outline hover:bg-stone-800"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-stone-100 transition-colors duration-150 bg-stone-700 rounded-lg focus:shadow-outline hover:bg-stone-800"
>
&lt;
&gt;
</button>
</td>
<td class="whitespace-nowrap px-6 py-4">{{.ID}}</td>
<td class="whitespace-nowrap px-6 py-4">{{.Name}}</td>

View File

@@ -18,7 +18,7 @@
<td class="whitespace-nowrap px-6 py-4">...</td>
{{ end }}
<td class="whitespace-nowrap px-6 py-4">
<td class="flex whitespace-nowrap px-6 py-4">
<button
hx-get="/items/edit/{{ .ID }}"
hx-target="#datarow-{{ .ID }}"
@@ -27,4 +27,15 @@
>
Edit
</button>
<button
hx-delete="/items/delete/{{ .ID }}"
hx-confirm="Are you sure?"
hx-target="#datarow-{{ .ID }}"
hx-swap="delete"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-red-100 transition-colors duration-150 bg-red-700 rounded-lg focus:shadow-outline hover:bg-red-800"
>
Delete
</button>
</td>
</tr>