fix for box item parsing issue
This commit is contained in:
145
db/sql.go
145
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user