fix for box item parsing issue
This commit is contained in:
@@ -39,9 +39,16 @@ type Box struct {
|
|||||||
Category Category `json:"category"`
|
Category Category `json:"category"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// joins
|
// joining tables and derivative data types
|
||||||
type BoxItem struct {
|
type BoxItem struct {
|
||||||
ID int
|
ID int
|
||||||
BoxID int
|
BoxID int
|
||||||
ItemID int
|
ItemID int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BoxItemWithItemInfo struct {
|
||||||
|
ID int
|
||||||
|
Name string
|
||||||
|
Stage PackingStage
|
||||||
|
Category Category
|
||||||
|
}
|
||||||
|
|||||||
145
db/sql.go
145
db/sql.go
@@ -68,6 +68,38 @@ func GetAllBoxes() (result []Box, err error) {
|
|||||||
return
|
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) {
|
func GetItemByID(id int) (item Item, err error) {
|
||||||
db, err := CreateClient()
|
db, err := CreateClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -96,6 +128,57 @@ func GetBoxByID(id int) (box Box, err error) {
|
|||||||
return
|
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) {
|
func PostItem(item Item) (int64, error) {
|
||||||
db, err := CreateClient()
|
db, err := CreateClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -135,6 +218,25 @@ func PostBox(box Box) (int64, error) {
|
|||||||
return result.LastInsertId()
|
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) {
|
func PutItem(item Item) (int64, error) {
|
||||||
db, err := CreateClient()
|
db, err := CreateClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -173,6 +275,25 @@ func PutBox(box Box) (int64, error) {
|
|||||||
return result.RowsAffected()
|
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) {
|
func DeleteItem(id int) (int64, error) {
|
||||||
db, err := CreateClient()
|
db, err := CreateClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -211,13 +332,21 @@ func DeleteBox(id int) (int64, error) {
|
|||||||
return result.RowsAffected()
|
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) {
|
defer db.Close()
|
||||||
// db, err := CreateClient()
|
|
||||||
// if err != nil {
|
|
||||||
// return -1, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// defer db.Close()
|
query := `DELETE FROM box_items WHERE id = ?`
|
||||||
// // query :=
|
|
||||||
// }
|
result, err := db.Exec(query, id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.RowsAffected()
|
||||||
|
}
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -45,6 +45,7 @@ func main() {
|
|||||||
|
|
||||||
itemActions := routes.Items(html)
|
itemActions := routes.Items(html)
|
||||||
boxActions := routes.Boxes(html)
|
boxActions := routes.Boxes(html)
|
||||||
|
boxItemActions := routes.BoxItems(html)
|
||||||
|
|
||||||
router.Handle("/items/edit", web.Action(itemActions.Edit))
|
router.Handle("/items/edit", web.Action(itemActions.Edit))
|
||||||
router.Handle("/items/delete", web.Action(itemActions.Delete))
|
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("/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("/items", web.Action(itemActions.Get))
|
||||||
router.Handle("/boxes", web.Action(boxActions.GetAll))
|
router.Handle("/boxes", web.Action(boxActions.GetAll))
|
||||||
router.Handle("/items/", web.Action(itemActions.Get))
|
router.Handle("/items/", web.Action(itemActions.Get))
|
||||||
|
|||||||
100
routes/boxItems.go
Normal file
100
routes/boxItems.go
Normal 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
|
||||||
|
}
|
||||||
@@ -26,7 +26,7 @@ func Items(_html *template.Template) *ItemActions {
|
|||||||
Get: Get,
|
Get: Get,
|
||||||
GetAll: GetAllItems,
|
GetAll: GetAllItems,
|
||||||
Edit: EditItem,
|
Edit: EditItem,
|
||||||
Delete: nil,
|
Delete: Delete,
|
||||||
Save: Put,
|
Save: Put,
|
||||||
Post: Post,
|
Post: Post,
|
||||||
Add: Add,
|
Add: Add,
|
||||||
|
|||||||
3
templates/box-items/box-item-list.html
Normal file
3
templates/box-items/box-item-list.html
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<tr id="box-item-list">
|
||||||
|
<td>Hooray!</td>
|
||||||
|
</tr>
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
<tr id="boxrow-{{.ID}}" class="border-b dark:border-neutral-500 hover:bg-slate-300 hover:bg-opacity-20">
|
<tr id="boxrow-{{.ID}}" class="border-b dark:border-neutral-500 hover:bg-slate-300 hover:bg-opacity-20">
|
||||||
<td
|
<td class="whitespace-nowrap px-6 py-4">
|
||||||
hx-get="/box-items?box_id={{ .ID }}"
|
<button
|
||||||
|
hx-get="/box-items?boxid={{ .ID }}"
|
||||||
hx-target="#boxrow-{{ .ID }}"
|
hx-target="#boxrow-{{ .ID }}"
|
||||||
hx-swap="afterend"
|
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"
|
||||||
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"
|
|
||||||
>
|
>
|
||||||
<
|
>
|
||||||
|
</button>
|
||||||
</td>
|
</td>
|
||||||
<td class="whitespace-nowrap px-6 py-4">{{.ID}}</td>
|
<td class="whitespace-nowrap px-6 py-4">{{.ID}}</td>
|
||||||
<td class="whitespace-nowrap px-6 py-4">{{.Name}}</td>
|
<td class="whitespace-nowrap px-6 py-4">{{.Name}}</td>
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
<td class="whitespace-nowrap px-6 py-4">...</td>
|
<td class="whitespace-nowrap px-6 py-4">...</td>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
<td class="whitespace-nowrap px-6 py-4">
|
<td class="flex whitespace-nowrap px-6 py-4">
|
||||||
<button
|
<button
|
||||||
hx-get="/items/edit/{{ .ID }}"
|
hx-get="/items/edit/{{ .ID }}"
|
||||||
hx-target="#datarow-{{ .ID }}"
|
hx-target="#datarow-{{ .ID }}"
|
||||||
@@ -27,4 +27,15 @@
|
|||||||
>
|
>
|
||||||
Edit
|
Edit
|
||||||
</button>
|
</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>
|
</tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user