From 99801f025579db3f614b7127dab837f001e9c093 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Sat, 2 Mar 2024 17:13:15 +0000 Subject: [PATCH] in progress: adding sqlite support --- .gitignore | 1 + Dockerfile | 5 ++++ data.go | 4 ++- data/entities.go | 47 +++++++++++++++++++++++++++++++++++ data/seed.go | 51 ++++++++++++++++++++++++++++++++++++++ data/sql.go | 54 +++++++++++++++++++++++++++++++++++++++++ go.mod | 6 +++-- go.sum | 4 +-- main.go | 3 ++- routes.go | 9 ++++++- templates/index.html | 4 +-- templates/item-row.html | 31 +++++++++++++++++++++++ templates/items.html | 40 ++++++++++++++++++++++++++++++ 13 files changed, 250 insertions(+), 9 deletions(-) create mode 100644 data/entities.go create mode 100644 data/seed.go create mode 100644 data/sql.go create mode 100644 templates/item-row.html create mode 100644 templates/items.html diff --git a/.gitignore b/.gitignore index a817cd4..03a4ecd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ app *.test *.out tmp +output.css diff --git a/Dockerfile b/Dockerfile index 449554f..31f171a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,11 @@ COPY . . ENV CGO_ENABLED=0 GOOS=linux GOPROXY=direct RUN go build -v -o app . +# Install SQLite +FROM alpine:latest as db +RUN apk --no-cache add sqlite + FROM scratch +COPY --from=db ./example.db . COPY --from=build /go/src/app/app /go/bin/app ENTRYPOINT ["/go/bin/app"] diff --git a/data.go b/data.go index 70192d4..19ed0b7 100644 --- a/data.go +++ b/data.go @@ -1,6 +1,8 @@ package main -import "strconv" +import ( + "strconv" +) var data []Company diff --git a/data/entities.go b/data/entities.go new file mode 100644 index 0000000..79e7428 --- /dev/null +++ b/data/entities.go @@ -0,0 +1,47 @@ +package data + +// enums +type PackingStage int +type Category int + +const ( + Essentials PackingStage = iota + StageOne + StageTwo + StageThree +) + +const ( + Bedroom Category = iota + Bathroom + Kitchen + Office + LivingRoom + Other +) + +// entities +type Item struct { + ID int + Name string + Notes *string + Description *string + Stage PackingStage + Category Category +} + +type Box struct { + ID int + Name string + Notes *string + Description *string + Stage PackingStage + Category Category +} + +// joins +type BoxItem struct { + ID int + BoxID int + ItemID int +} diff --git a/data/seed.go b/data/seed.go new file mode 100644 index 0000000..d15d007 --- /dev/null +++ b/data/seed.go @@ -0,0 +1,51 @@ +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 +} diff --git a/data/sql.go b/data/sql.go new file mode 100644 index 0000000..92e7f5c --- /dev/null +++ b/data/sql.go @@ -0,0 +1,54 @@ +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 +} diff --git a/go.mod b/go.mod index d318025..e017e6f 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ -module github.com/jritsema/go-htmx-starter +module github.com/innocuous-symmetry/moving-mgmt -go 1.20 +go 1.22.0 require github.com/jritsema/gotoolbox v0.8.0 + +require github.com/mattn/go-sqlite3 v1.14.22 // indirect diff --git a/go.sum b/go.sum index f0cc2d1..cba6971 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ -github.com/jritsema/gotoolbox v0.7.0 h1:pvEf4VnB/Gjf/UYoq8T6f9vnIb7ssteGlFJEXg0Ejpo= -github.com/jritsema/gotoolbox v0.7.0/go.mod h1:OgV4sjpMB/bx/ZZPpXWvfalGrniFvkvGtqFRQH6GGHY= github.com/jritsema/gotoolbox v0.8.0 h1:guUvlilrUcT24i0iGnasLch6pjWJ437Qnabk6WTmPEU= github.com/jritsema/gotoolbox v0.8.0/go.mod h1:OgV4sjpMB/bx/ZZPpXWvfalGrniFvkvGtqFRQH6GGHY= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/main.go b/main.go index e79bb9d..3088e67 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/jritsema/gotoolbox" "github.com/jritsema/gotoolbox/web" + "github.com/mattn/go-sqlite3" ) var ( @@ -40,7 +41,7 @@ func main() { //add routes router := http.NewServeMux() - router.Handle("/css/output.css", http.FileServer(http.FS(css))) + // router.Handle("/css/output.css", http.FileServer(http.FS(css))) router.Handle("/company/add", web.Action(companyAdd)) router.Handle("/company/add/", web.Action(companyAdd)) diff --git a/routes.go b/routes.go index fa35ea0..3fe8bca 100644 --- a/routes.go +++ b/routes.go @@ -2,6 +2,7 @@ package main import ( "net/http" + sql "github.com/innocuous-symmetry/moving-mgmt/data" "github.com/jritsema/gotoolbox/web" ) @@ -17,9 +18,15 @@ import ( // Cancel -> GET /company -> nothing, companys.html func index(r *http.Request) *web.Response { - return web.HTML(http.StatusOK, html, "index.html", data, nil) + result, err := sql.GetAllItems() + if err != nil { + panic(err) + } + + return web.HTML(http.StatusOK, html, "index.html", result, nil) } + // GET /company/add func companyAdd(r *http.Request) *web.Response { return web.HTML(http.StatusOK, html, "company-add.html", data, nil) diff --git a/templates/index.html b/templates/index.html index 0eb330d..fa2d3ab 100644 --- a/templates/index.html +++ b/templates/index.html @@ -11,10 +11,10 @@
-

Go + HTMX + Tailwind Example

+

Mikayla's Move Manager


Companies -
{{ template "companies.html" . }}
+
{{ template "items.html" . }}
diff --git a/templates/item-row.html b/templates/item-row.html new file mode 100644 index 0000000..13bcebf --- /dev/null +++ b/templates/item-row.html @@ -0,0 +1,31 @@ + + {{.ID}} + {{.Name}} + {{.Stage}} + {{.Category}} + {{.Description}} + {{.Notes}} + + + Edit + + + + Delete + + diff --git a/templates/items.html b/templates/items.html new file mode 100644 index 0000000..9dec082 --- /dev/null +++ b/templates/items.html @@ -0,0 +1,40 @@ +
+
Processing...
+
+
+
+
+ + + + + + + + + + + + + + {{range .}} + {{ template "item-row.html". }} + {{end}} + +
#NameStageCategoryDescriptionNotes
+
+
+
+
+