merge tmp to main
This commit is contained in:
@@ -20,46 +20,37 @@ const (
|
||||
Other
|
||||
)
|
||||
|
||||
var CategoryMap = map[Category]string{
|
||||
Bedroom: "Bedroom",
|
||||
Bathroom: "Bathroom",
|
||||
Kitchen: "Kitchen",
|
||||
Office: "Office",
|
||||
LivingRoom: "Living Room",
|
||||
Other: "Other",
|
||||
// entities
|
||||
type Item struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Notes *string `json:"notes"`
|
||||
Description *string `json:"description"`
|
||||
Stage PackingStage `json:"stage"`
|
||||
Category Category `json:"category"`
|
||||
}
|
||||
|
||||
var PackingStageMap = map[PackingStage]string{
|
||||
Essentials: "Essentials",
|
||||
StageOne: "Stage One",
|
||||
StageTwo: "Stage Two",
|
||||
StageThree: "Stage Three",
|
||||
type Box struct {
|
||||
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 EntityLabel string
|
||||
|
||||
const (
|
||||
ItemType EntityLabel = "items"
|
||||
BoxType EntityLabel = "boxes"
|
||||
BoxItemType EntityLabel = "box_items"
|
||||
)
|
||||
|
||||
type Entity struct {
|
||||
ID int
|
||||
EntityLabel EntityLabel
|
||||
Name string
|
||||
Notes *string
|
||||
Description *string
|
||||
Stage PackingStage
|
||||
Category Category
|
||||
}
|
||||
|
||||
type Item Entity
|
||||
type Box Entity
|
||||
|
||||
// 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
|
||||
Description *string
|
||||
Notes *string
|
||||
}
|
||||
|
||||
313
db/sql.go
313
db/sql.go
@@ -2,33 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func CreateClient() (db *sql.DB, err error) {
|
||||
return sql.Open("sqlite3", "./example.db")
|
||||
}
|
||||
|
||||
func GetAllItems() (rows *sql.Rows, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
rows, err = db.Query("SELECT * FROM items")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// fmt.Println("rows", rows)
|
||||
|
||||
defer rows.Close()
|
||||
return
|
||||
}
|
||||
|
||||
func GetAll(table EntityLabel) (rows *sql.Rows, err error) {
|
||||
func GetAllItems() (result []Item, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -36,16 +16,29 @@ func GetAll(table EntityLabel) (rows *sql.Rows, err error) {
|
||||
|
||||
defer db.Close()
|
||||
|
||||
rows, err = db.Query("SELECT * FROM ?", table)
|
||||
rows, err := db.Query("SELECT * FROM items")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
item := Item{}
|
||||
|
||||
err = rows.Scan(&item.ID, &item.Name, &item.Notes, &item.Description, &item.Stage, &item.Category)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result = append(result, item)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetByID(table EntityLabel, id int) (row *sql.Row, err error) {
|
||||
func GetAllBoxes() (result []Box, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -53,11 +46,29 @@ func GetByID(table EntityLabel, id int) (row *sql.Row, err error) {
|
||||
|
||||
defer db.Close()
|
||||
|
||||
row = db.QueryRow("SELECT * FROM ? WHERE id = ?", table, id)
|
||||
rows, err := db.Query("SELECT * FROM boxes")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
box := Box{}
|
||||
|
||||
err = rows.Scan(&box.ID, &box.Name, &box.Notes, &box.Description, &box.Stage, &box.Category)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result = append(result, box)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func Put[T Entity](table EntityLabel, record Entity) (sql.Result, error) {
|
||||
func GetAllBoxItems() ([]BoxItem, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -65,18 +76,76 @@ func Put[T Entity](table EntityLabel, record Entity) (sql.Result, error) {
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `UPDATE ? SET name = ?, notes = ?, description = ?, stage = ?, category = ? WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, table, record.Name, record.Notes, record.Description, record.Stage, record.Category, record.ID)
|
||||
|
||||
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 PostItem(record Item) (sql.Result, error) {
|
||||
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 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
|
||||
@@ -84,70 +153,200 @@ func PostItem(record Item) (sql.Result, error) {
|
||||
|
||||
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, items.description, items.notes 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, &boxItem.Description, &boxItem.Notes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result = append(result, boxItem)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
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, record.Name, record.Notes, record.Description, record.Stage, record.Category)
|
||||
result, err := db.Exec(query, item.Name, item.Notes, item.Description, item.Stage, item.Category)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func PostBox(record Box) (sql.Result, error) {
|
||||
func PostBox(box Box) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO boxes (name, notes, description, stage, category)
|
||||
VALUES (?, ?, ?, ?, ?)`
|
||||
query := `INSERT INTO boxes (name, notes, description, stage, category) VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, record.Name, record.Notes, record.Description, record.Stage, record.Category)
|
||||
result, err := db.Exec(query, box.Name, box.Notes, box.Description, box.Stage, box.Category)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func Delete(table EntityLabel, id int) (sql.Result, error) {
|
||||
func PostBoxItem(boxItem BoxItem) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `DELETE FROM ? WHERE id = ?`
|
||||
query := `INSERT INTO box_items (boxid, item_id) VALUES (?, ?)`
|
||||
|
||||
return db.Exec(query, table, id)
|
||||
result, err := db.Exec(query, boxItem.BoxID, boxItem.ItemID)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func ParseItem(item *Item, scan func(dest ...any) error) (err error) {
|
||||
return scan(&item.ID, &item.Name, &item.Notes, &item.Description, &item.Stage, &item.Category)
|
||||
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 ParseBox(box *Box, scan func(dest ...any) error) error {
|
||||
return scan(&box.ID, &box.Name, &box.Notes, &box.Description, &box.Stage, &box.Category)
|
||||
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 ParseEntityFromBytes(b []byte) (entity Entity, err error) {
|
||||
err = json.Unmarshal(b, &entity)
|
||||
return
|
||||
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 ParseItemFromBytes(b []byte) (item Item, err error) {
|
||||
err = json.Unmarshal(b, &item)
|
||||
return
|
||||
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 ParseBoxFromBytes(b []byte) (box Box, err error) {
|
||||
err = json.Unmarshal(b, &box)
|
||||
return
|
||||
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 DeleteBoxItem(id int) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
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