lot of breaking changes, checking in before stepping away
This commit is contained in:
@@ -20,28 +20,46 @@ const (
|
||||
Other
|
||||
)
|
||||
|
||||
// entities
|
||||
type Item struct {
|
||||
ID int
|
||||
Name string
|
||||
Notes *string
|
||||
Description *string
|
||||
Stage PackingStage
|
||||
Category Category
|
||||
var CategoryMap = map[Category]string{
|
||||
Bedroom: "Bedroom",
|
||||
Bathroom: "Bathroom",
|
||||
Kitchen: "Kitchen",
|
||||
Office: "Office",
|
||||
LivingRoom: "Living Room",
|
||||
Other: "Other",
|
||||
}
|
||||
|
||||
type Box struct {
|
||||
ID int
|
||||
Name string
|
||||
Notes *string
|
||||
Description *string
|
||||
Stage PackingStage
|
||||
Category Category
|
||||
var PackingStageMap = map[PackingStage]string{
|
||||
Essentials: "Essentials",
|
||||
StageOne: "Stage One",
|
||||
StageTwo: "Stage Two",
|
||||
StageThree: "Stage Three",
|
||||
}
|
||||
|
||||
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
|
||||
type BoxItem struct {
|
||||
ID int
|
||||
BoxID int
|
||||
ItemID int
|
||||
ID int
|
||||
BoxID int
|
||||
ItemID int
|
||||
}
|
||||
|
||||
14
db/seed.go
14
db/seed.go
@@ -58,12 +58,12 @@ func GetSeedData() (items []Item, boxes []Box, boxitems []BoxItem) {
|
||||
func CreateTables(client *sql.DB) (int64, error) {
|
||||
script, err := os.ReadFile("/home/mikayla/go/go-htmx-tailwind-example/db/seed.sql")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return -1, err
|
||||
}
|
||||
|
||||
result, err := client.Exec(string(script))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
@@ -86,6 +86,11 @@ func SeedDB() (int64, error) {
|
||||
for i := range(items) {
|
||||
_, err := PostItem(items[i])
|
||||
if err != nil {
|
||||
// ignore unique constraint violations and continue
|
||||
if err.Error() == "UNIQUE constraint failed: items.Name" {
|
||||
continue
|
||||
}
|
||||
|
||||
return -1, err
|
||||
}
|
||||
insertCount++
|
||||
@@ -94,6 +99,11 @@ func SeedDB() (int64, error) {
|
||||
for i := range(boxes) {
|
||||
_, err := PostBox(boxes[i])
|
||||
if err != nil {
|
||||
// ignore unique constraint violations and continue
|
||||
if err.Error() == "UNIQUE constraint failed: boxes.Name" {
|
||||
continue
|
||||
}
|
||||
|
||||
return -1, err
|
||||
}
|
||||
insertCount++
|
||||
|
||||
153
db/sql.go
153
db/sql.go
@@ -2,13 +2,33 @@ package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func CreateClient() (db *sql.DB, err error) {
|
||||
return sql.Open("sqlite3", "./example.db")
|
||||
}
|
||||
|
||||
func GetAllItems() (result []Item, err error) {
|
||||
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) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -16,46 +36,50 @@ func GetAllItems() (result []Item, err error) {
|
||||
|
||||
defer db.Close()
|
||||
|
||||
rows, err := db.Query("SELECT * FROM items")
|
||||
rows, err = db.Query("SELECT * FROM ?", table)
|
||||
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 GetItemByID(id int) (item Item, err error) {
|
||||
func GetByID(table EntityLabel, id int) (row *sql.Row, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return Item{}, err
|
||||
return nil, 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)
|
||||
|
||||
row = db.QueryRow("SELECT * FROM ? WHERE id = ?", table, id)
|
||||
return
|
||||
}
|
||||
|
||||
func PostItem(item Item) (int64, error) {
|
||||
func Put[T Entity](table EntityLabel, record Entity) (sql.Result, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func PostItem(record Item) (sql.Result, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
@@ -63,37 +87,16 @@ func PostItem(item Item) (int64, error) {
|
||||
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)
|
||||
result, err := db.Exec(query, record.Name, record.Notes, record.Description, record.Stage, record.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
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) {
|
||||
func PostBox(record Box) (sql.Result, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -101,34 +104,50 @@ func GetAllBoxes() (result []Box, err error) {
|
||||
|
||||
defer db.Close()
|
||||
|
||||
rows, err := db.Query("SELECT * FROM boxes")
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
return result, nil
|
||||
}
|
||||
|
||||
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)
|
||||
func Delete(table EntityLabel, id int) (sql.Result, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `DELETE FROM ? WHERE id = ?`
|
||||
|
||||
return db.Exec(query, table, id)
|
||||
}
|
||||
|
||||
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 ParseBox(box *Box, scan func(dest ...any) error) error {
|
||||
return scan(&box.ID, &box.Name, &box.Notes, &box.Description, &box.Stage, &box.Category)
|
||||
}
|
||||
|
||||
func ParseEntityFromBytes(b []byte) (entity Entity, err error) {
|
||||
err = json.Unmarshal(b, &entity)
|
||||
return
|
||||
}
|
||||
|
||||
// func PostBoxItem(itemid int, boxid int) (int64, error) {
|
||||
// db, err := CreateClient()
|
||||
// if err != nil {
|
||||
// return -1, err
|
||||
// }
|
||||
func ParseItemFromBytes(b []byte) (item Item, err error) {
|
||||
err = json.Unmarshal(b, &item)
|
||||
return
|
||||
}
|
||||
|
||||
// defer db.Close()
|
||||
// // query :=
|
||||
// }
|
||||
func ParseBoxFromBytes(b []byte) (box Box, err error) {
|
||||
err = json.Unmarshal(b, &box)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user