diff --git a/db/entities.go b/db/entities.go index 214b15b..3c97446 100644 --- a/db/entities.go +++ b/db/entities.go @@ -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 +} diff --git a/db/sql.go b/db/sql.go index 03f6dec..5f16eb0 100644 --- a/db/sql.go +++ b/db/sql.go @@ -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() +} diff --git a/main.go b/main.go index ca61146..68c4709 100644 --- a/main.go +++ b/main.go @@ -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)) diff --git a/routes/boxItems.go b/routes/boxItems.go new file mode 100644 index 0000000..a91329e --- /dev/null +++ b/routes/boxItems.go @@ -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 +} diff --git a/routes/items.go b/routes/items.go index 0f201c0..ab35795 100644 --- a/routes/items.go +++ b/routes/items.go @@ -23,13 +23,13 @@ func Items(_html *template.Template) *ItemActions { html = _html return &ItemActions{ - Get: Get, - GetAll: GetAllItems, - Edit: EditItem, - Delete: nil, - Save: Put, - Post: Post, - Add: Add, + Get: Get, + GetAll: GetAllItems, + Edit: EditItem, + Delete: Delete, + Save: Put, + Post: Post, + Add: Add, } } diff --git a/templates/box-items/box-item-list.html b/templates/box-items/box-item-list.html new file mode 100644 index 0000000..3070add --- /dev/null +++ b/templates/box-items/box-item-list.html @@ -0,0 +1,3 @@ + + Hooray! + diff --git a/templates/boxes/box-row.html b/templates/boxes/box-row.html index 60526e7..089c07f 100644 --- a/templates/boxes/box-row.html +++ b/templates/boxes/box-row.html @@ -1,11 +1,12 @@ - - < + + {{.ID}} {{.Name}} diff --git a/templates/items/entity-row.html b/templates/items/entity-row.html index 97ceac6..77f68ed 100644 --- a/templates/items/entity-row.html +++ b/templates/items/entity-row.html @@ -18,7 +18,7 @@ ... {{ end }} - + + + +