Start building out the backend with database

This commit is contained in:
Michał Gdula 2024-05-05 17:51:34 +01:00
parent 17476d3f2c
commit 677b4277a6
11 changed files with 241 additions and 63 deletions

40
database/database.go Normal file
View file

@ -0,0 +1,40 @@
package database
import (
"database/sql"
"github.com/mattn/go-sqlite3"
"log"
_ "github.com/mattn/go-sqlite3"
)
var Conn *sql.DB
func Open() {
var err error
sql.Register("sqlite3_with_extensions",
&sqlite3.SQLiteDriver{
Extensions: []string{
"sqlite3_mod_regexp",
},
})
Conn, err = sql.Open("sqlite3", "tastybites.db?_journal_mode=WAL")
if err != nil {
log.Fatal("Error opening connection: ", err)
}
//Set the connection to use WAL mode
_, err = Conn.Exec("PRAGMA journal_mode=WAL;")
if err != nil {
log.Fatal(err)
}
}
func Close() {
err := Conn.Close()
if err != nil {
log.Fatal(err)
}
}

16
database/tables.go Normal file
View file

@ -0,0 +1,16 @@
package database
import (
sb "github.com/huandu/go-sqlbuilder"
)
type Item struct {
UUID string `json:"uuid" db:"uuid"`
Name string `json:"name" db:"name"`
Price int64 `json:"price" db:"price"`
Description string `json:"description,omitempty" db:"description"`
//Labels []string `json:"labels,omitempty" db:"labels"`
//Images []string `json:"images,omitempty" db:"images"`
}
var ItemStruct = sb.NewStruct(new(Item))

69
database/test_data.go Normal file
View file

@ -0,0 +1,69 @@
package database
var SampleData = []Item{
{
UUID: "soap",
Name: "Bar of Soap",
Price: 6999,
//Labels: []string{"vegan", "spicy"},
Description: "Example",
},
{
UUID: "sock",
Name: "Sock",
Price: 21,
//Labels: []string{"vegan", "fish", "nut", "spicy"},
Description: "Example",
},
{
UUID: "brick",
Name: "Brick",
Price: 0,
//Labels: []string{"spicy"},
Description: "Example",
},
{
UUID: "toast",
Name: "Toast",
Price: 4382749832743,
//Labels: []string{"gluten"},
Description: "Example",
},
{
UUID: "water",
Name: "water",
Price: 1,
//Labels: []string{"fish"},
Description: "Example",
},
{
UUID: "mouldy_bread",
Name: "half eaten mouldy bread",
Price: -99,
//Labels: []string{"nut"},
Description: "Example",
},
{
UUID: "gwagwa",
Name: "GwaGwa",
Price: 69,
//Labels: []string{"nut"},
//Images: []string{"/dab.jpg"},
},
{
UUID: "hogmelon",
Name: "Hogermellon",
Price: 1111,
//Labels: []string{"fish"},
//Images: []string{"/wathog.jpg"},
Description: "Example",
},
{
UUID: "bluhog",
Name: "Blue HOGGGGG",
Price: 0,
//Labels: []string{"nut", "gluten", "spicy"},
//Images: []string{"/sonichog.jpg"},
Description: "Example",
},
}