item put functionality
This commit is contained in:
@@ -22,21 +22,21 @@ const (
|
||||
|
||||
// entities
|
||||
type Item struct {
|
||||
ID int
|
||||
Name string
|
||||
Notes *string
|
||||
Description *string
|
||||
Stage PackingStage
|
||||
Category Category
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Notes *string `json:"notes"`
|
||||
Description *string `json:"description"`
|
||||
Stage PackingStage `json:"stage"`
|
||||
Category Category `json:"category"`
|
||||
}
|
||||
|
||||
type Box struct {
|
||||
ID int
|
||||
Name string
|
||||
Notes *string
|
||||
Description *string
|
||||
Stage PackingStage
|
||||
Category Category
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Notes *string `json:"notes"`
|
||||
Description *string `json:"description"`
|
||||
Stage PackingStage `json:"stage"`
|
||||
Category Category `json:"category"`
|
||||
}
|
||||
|
||||
// joins
|
||||
|
||||
199
db/sql.go
199
db/sql.go
@@ -38,61 +38,6 @@ func GetAllItems() (result []Item, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func GetItemByID(id int) (item Item, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return Item{}, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
row := db.QueryRow("SELECT * FROM items WHERE id = ?", id)
|
||||
err = row.Scan(&item.ID, &item.Name, &item.Notes, &item.Description, &item.Stage, &item.Category)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func PostItem(item Item) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO items (name, notes, description, stage, category)
|
||||
VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, item.Name, item.Notes, item.Description, item.Stage, item.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func PostBox(box Box) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO boxes (name, notes, description, stage, category) VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, box.Name, box.Notes, box.Description, box.Stage, box.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func GetAllBoxes() (result []Box, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
@@ -123,6 +68,150 @@ func GetAllBoxes() (result []Box, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func GetItemByID(id int) (item Item, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return Item{}, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
row := db.QueryRow("SELECT * FROM items WHERE id = ?", id)
|
||||
err = row.Scan(&item.ID, &item.Name, &item.Notes, &item.Description, &item.Stage, &item.Category)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetBoxByID(id int) (box Box, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return Box{}, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
row := db.QueryRow("SELECT * FROM boxes WHERE id = ?", id)
|
||||
err = row.Scan(&box.ID, &box.Name, &box.Notes, &box.Description, &box.Stage, &box.Category)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func PostItem(item Item) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO items (name, notes, description, stage, category)
|
||||
VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, item.Name, item.Notes, item.Description, item.Stage, item.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func PostBox(box Box) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO boxes (name, notes, description, stage, category) VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, box.Name, box.Notes, box.Description, box.Stage, box.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func PutItem(item Item) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `UPDATE items SET name = ?, notes = ?, description = ?, stage = ?, category = ? WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, item.Name, item.Notes, item.Description, item.Stage, item.Category, item.ID)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
func PutBox(box Box) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `UPDATE boxes SET name = ?, notes = ?, description = ?, stage = ?, category = ? WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, box.Name, box.Notes, box.Description, box.Stage, box.Category, box.ID)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
func DeleteItem(id int) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `DELETE FROM items WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, id)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
func DeleteBox(id int) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `DELETE FROM boxes WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, id)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
|
||||
// func PostBoxItem(itemid int, boxid int) (int64, error) {
|
||||
// db, err := CreateClient()
|
||||
// if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user