setup for sqlite
This commit is contained in:
51
data/seed.go
51
data/seed.go
@@ -1,51 +0,0 @@
|
|||||||
package data
|
|
||||||
|
|
||||||
func getSeedData() (items []Item, boxes []Box, boxitems []BoxItem) {
|
|
||||||
items = []Item{
|
|
||||||
{
|
|
||||||
ID: 1,
|
|
||||||
Name: "Toothbrush",
|
|
||||||
Stage: Essentials,
|
|
||||||
Category: Bathroom,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ID: 2,
|
|
||||||
Name: "Toothpaste",
|
|
||||||
Stage: Essentials,
|
|
||||||
Category: Bathroom,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ID: 3,
|
|
||||||
Name: "TV",
|
|
||||||
Stage: StageTwo,
|
|
||||||
Category: Bedroom,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ID: 4,
|
|
||||||
Name: "Micro USB Bundle",
|
|
||||||
Stage: StageOne,
|
|
||||||
Category: Office,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
plasticTubDescription := "Plastic tub with blue lid"
|
|
||||||
|
|
||||||
boxes = []Box{
|
|
||||||
{
|
|
||||||
ID: 1,
|
|
||||||
Name: "Cable Box",
|
|
||||||
Description: &plasticTubDescription,
|
|
||||||
Stage: StageOne,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
boxitems = []BoxItem{
|
|
||||||
{
|
|
||||||
ID: 1,
|
|
||||||
BoxID: 1,
|
|
||||||
ItemID: 4,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
54
data/sql.go
54
data/sql.go
@@ -1,54 +0,0 @@
|
|||||||
package data
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"github.com/mattn/go-sqlite3"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CreateClient() (db *sql.DB, err error) {
|
|
||||||
return sql.Open("sqlite3", "./example.db")
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAllItems() (result []Item, err error) {
|
|
||||||
db, err := CreateClient()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
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 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
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package data
|
package db
|
||||||
|
|
||||||
// enums
|
// enums
|
||||||
type PackingStage int
|
type PackingStage int
|
||||||
103
db/seed.go
Normal file
103
db/seed.go
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetSeedData() (items []Item, boxes []Box, boxitems []BoxItem) {
|
||||||
|
items = []Item{
|
||||||
|
{
|
||||||
|
ID: 1,
|
||||||
|
Name: "Toothbrush",
|
||||||
|
Stage: Essentials,
|
||||||
|
Category: Bathroom,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 2,
|
||||||
|
Name: "Toothpaste",
|
||||||
|
Stage: Essentials,
|
||||||
|
Category: Bathroom,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 3,
|
||||||
|
Name: "TV",
|
||||||
|
Stage: StageTwo,
|
||||||
|
Category: Bedroom,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 4,
|
||||||
|
Name: "Micro USB Bundle",
|
||||||
|
Stage: StageOne,
|
||||||
|
Category: Office,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
plasticTubDescription := "Plastic tub with blue lid"
|
||||||
|
|
||||||
|
boxes = []Box{
|
||||||
|
{
|
||||||
|
ID: 1,
|
||||||
|
Name: "Cable Box",
|
||||||
|
Description: &plasticTubDescription,
|
||||||
|
Stage: StageOne,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
boxitems = []BoxItem{
|
||||||
|
{
|
||||||
|
ID: 1,
|
||||||
|
BoxID: 1,
|
||||||
|
ItemID: 4,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := client.Exec(string(script))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.RowsAffected()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SeedDB() (int64, error) {
|
||||||
|
client, err := CreateClient()
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
CreateTables(client)
|
||||||
|
|
||||||
|
items, boxes, _ := GetSeedData()
|
||||||
|
insertCount := 0
|
||||||
|
|
||||||
|
// loop through initial items and run post request for each
|
||||||
|
for i := range(items) {
|
||||||
|
_, err := PostItem(items[i])
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
insertCount++
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range(boxes) {
|
||||||
|
_, err := PostBox(boxes[i])
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
insertCount++
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64(insertCount), nil
|
||||||
|
}
|
||||||
28
db/seed.sql
Normal file
28
db/seed.sql
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
-- CREATE DATABASE IF NOT EXISTS inventory;
|
||||||
|
-- USE inventory;
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS items (
|
||||||
|
ID INTEGER PRIMARY KEY,
|
||||||
|
Name TEXT NOT NULL,
|
||||||
|
Notes TEXT,
|
||||||
|
Description TEXT,
|
||||||
|
Stage INTEGER,
|
||||||
|
Category INTEGER
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS boxes (
|
||||||
|
ID INTEGER PRIMARY KEY,
|
||||||
|
Name TEXT NOT NULL,
|
||||||
|
Notes TEXT,
|
||||||
|
Description TEXT,
|
||||||
|
Stage INTEGER,
|
||||||
|
Category INTEGER
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS boxitems (
|
||||||
|
ItemID INTEGER,
|
||||||
|
BoxID INTEGER,
|
||||||
|
PRIMARY KEY (ItemID, BoxID),
|
||||||
|
FOREIGN KEY (ItemID) REFERENCES items(ID),
|
||||||
|
FOREIGN KEY (BoxID) REFERENCES boxes(ID)
|
||||||
|
);
|
||||||
104
db/sql.go
Normal file
104
db/sql.go
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateClient() (db *sql.DB, err error) {
|
||||||
|
return sql.Open("sqlite3", "./example.db")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllItems() (result []Item, err error) {
|
||||||
|
db, err := CreateClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
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 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 PostBoxItem(itemid int, boxid int) (int64, error) {
|
||||||
|
// db, err := CreateClient()
|
||||||
|
// if err != nil {
|
||||||
|
// return -1, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// defer db.Close()
|
||||||
|
// // query :=
|
||||||
|
// }
|
||||||
BIN
example.db
Normal file
BIN
example.db
Normal file
Binary file not shown.
5
main.go
5
main.go
@@ -11,9 +11,11 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/innocuous-symmetry/moving-mgmt/db"
|
||||||
|
|
||||||
"github.com/jritsema/gotoolbox"
|
"github.com/jritsema/gotoolbox"
|
||||||
"github.com/jritsema/gotoolbox/web"
|
"github.com/jritsema/gotoolbox/web"
|
||||||
"github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -31,6 +33,7 @@ func main() {
|
|||||||
|
|
||||||
//exit process immediately upon sigterm
|
//exit process immediately upon sigterm
|
||||||
handleSigTerms()
|
handleSigTerms()
|
||||||
|
db.SeedDB()
|
||||||
|
|
||||||
//parse templates
|
//parse templates
|
||||||
var err error
|
var err error
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
sql "github.com/innocuous-symmetry/moving-mgmt/data"
|
db "github.com/innocuous-symmetry/moving-mgmt/db"
|
||||||
|
|
||||||
"github.com/jritsema/gotoolbox/web"
|
"github.com/jritsema/gotoolbox/web"
|
||||||
)
|
)
|
||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
// Cancel -> GET /company -> nothing, companys.html
|
// Cancel -> GET /company -> nothing, companys.html
|
||||||
|
|
||||||
func index(r *http.Request) *web.Response {
|
func index(r *http.Request) *web.Response {
|
||||||
result, err := sql.GetAllItems()
|
result, err := db.GetAllItems()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<td class="whitespace-nowrap px-6 py-4">{{.Category}}</td>
|
<td class="whitespace-nowrap px-6 py-4">{{.Category}}</td>
|
||||||
<td class="whitespace-nowrap px-6 py-4">{{.Description}}</td>
|
<td class="whitespace-nowrap px-6 py-4">{{.Description}}</td>
|
||||||
<td class="whitespace-nowrap px-6 py-4">{{.Notes}}</td>
|
<td class="whitespace-nowrap px-6 py-4">{{.Notes}}</td>
|
||||||
<td class="whitespace-nowrap px-1 py-1">
|
<!-- <td class="whitespace-nowrap px-1 py-1">
|
||||||
<a
|
<a
|
||||||
hx-get="/company/edit/{{.ID}}"
|
hx-get="/company/edit/{{.ID}}"
|
||||||
hx-target="#datarow-{{.ID}}"
|
hx-target="#datarow-{{.ID}}"
|
||||||
@@ -16,8 +16,8 @@
|
|||||||
>
|
>
|
||||||
Edit
|
Edit
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td> -->
|
||||||
<td class="whitespace-nowrap px-1 py-1">
|
<!-- <td class="whitespace-nowrap px-1 py-1">
|
||||||
<a
|
<a
|
||||||
hx-delete="/company/{{.ID}}"
|
hx-delete="/company/{{.ID}}"
|
||||||
hx-target="#companies"
|
hx-target="#companies"
|
||||||
@@ -27,5 +27,5 @@
|
|||||||
href=""
|
href=""
|
||||||
>Delete</a
|
>Delete</a
|
||||||
>
|
>
|
||||||
</td>
|
</td> -->
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user