From 9b2caddeb4fe50effaec10120840912dc021ab03 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Sun, 3 Mar 2024 04:19:57 +0000 Subject: [PATCH] enabled tab switching --- data.go | 90 -------------------- db/sql.go | 30 +++++++ main.go | 12 +-- routes.go | 17 ---- routes/base.go | 45 ++++++++++ routes/boxes.go | 40 +++++++++ routes/index.go | 26 ++++++ routes/items.go | 63 ++++++++++++++ templates/{items.html => boxes.html} | 4 +- templates/entity-list.html | 40 +++++++++ templates/{item-row.html => entity-row.html} | 0 templates/index.html | 27 +++++- 12 files changed, 275 insertions(+), 119 deletions(-) delete mode 100644 data.go delete mode 100644 routes.go create mode 100644 routes/base.go create mode 100644 routes/boxes.go create mode 100644 routes/index.go create mode 100644 routes/items.go rename templates/{items.html => boxes.html} (95%) create mode 100644 templates/entity-list.html rename templates/{item-row.html => entity-row.html} (100%) diff --git a/data.go b/data.go deleted file mode 100644 index 19ed0b7..0000000 --- a/data.go +++ /dev/null @@ -1,90 +0,0 @@ -package main - -import ( - "strconv" -) - -var data []Company - -type Company struct { - ID string - Company string - Contact string - Country string -} - -func init() { - data = []Company{ - { - ID: "1", - Company: "Amazon", - Contact: "Jeff Bezos", - Country: "United States", - }, - { - ID: "2", - Company: "Apple", - Contact: "Tim Cook", - Country: "United States", - }, - { - ID: "3", - Company: "Microsoft", - Contact: "Satya Nadella", - Country: "United States", - }, - } -} - -func getCompanyByID(id string) Company { - var result Company - for _, i := range data { - if i.ID == id { - result = i - break - } - } - return result -} - -func updateCompany(company Company) { - result := []Company{} - for _, i := range data { - if i.ID == company.ID { - i.Company = company.Company - i.Contact = company.Contact - i.Country = company.Country - } - result = append(result, i) - } - data = result -} - -func addCompany(company Company) { - max := 0 - for _, i := range data { - n, _ := strconv.Atoi(i.ID) - if n > max { - max = n - } - } - max++ - id := strconv.Itoa(max) - - data = append(data, Company{ - ID: id, - Company: company.Company, - Contact: company.Contact, - Country: company.Country, - }) -} - -func deleteCompany(id string) { - result := []Company{} - for _, i := range data { - if i.ID != id { - result = append(result, i) - } - } - data = result -} diff --git a/db/sql.go b/db/sql.go index 25b2162..f2def63 100644 --- a/db/sql.go +++ b/db/sql.go @@ -93,6 +93,36 @@ func PostBox(box Box) (int64, error) { return result.LastInsertId() } +func GetAllBoxes() (result []Box, err error) { + db, err := CreateClient() + if err != nil { + return nil, err + } + + defer db.Close() + + 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 PostBoxItem(itemid int, boxid int) (int64, error) { // db, err := CreateClient() // if err != nil { diff --git a/main.go b/main.go index 2619f6e..bf8e72d 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "time" "github.com/innocuous-symmetry/moving-mgmt/db" + "github.com/innocuous-symmetry/moving-mgmt/routes" "github.com/jritsema/gotoolbox" "github.com/jritsema/gotoolbox/web" @@ -49,14 +50,9 @@ func main() { // router.Handle("/company/add", web.Action(companyAdd)) // router.Handle("/company/add/", web.Action(companyAdd)) - // router.Handle("/company/edit", web.Action(companyEdit)) - // router.Handle("/company/edit/", web.Action(companyEdit)) - - // router.Handle("/company", web.Action(companies)) - // router.Handle("/company/", web.Action(companies)) - - router.Handle("/", web.Action(index)) - router.Handle("/index.html", web.Action(index)) + router.Handle("/", web.Action(routes.HomePage)) + router.Handle("/items", web.Action(routes.Items(html).GetAll)) + router.Handle("/boxes", web.Action(routes.Boxes(html).GetAll)) //logging/tracing nextRequestID := func() string { diff --git a/routes.go b/routes.go deleted file mode 100644 index 3038948..0000000 --- a/routes.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "net/http" - db "github.com/innocuous-symmetry/moving-mgmt/db" - - "github.com/jritsema/gotoolbox/web" -) - -func index(r *http.Request) *web.Response { - result, err := db.GetAllItems() - if err != nil { - panic(err) - } - - return web.HTML(http.StatusOK, html, "index.html", result, nil) -} diff --git a/routes/base.go b/routes/base.go new file mode 100644 index 0000000..f304360 --- /dev/null +++ b/routes/base.go @@ -0,0 +1,45 @@ +package routes + +import ( + "net/http" + + "github.com/jritsema/gotoolbox/web" +) + +type Entity int + +const ( + Item Entity = iota + Box + BoxItem +) + +type RouterActions struct { + GetAll func(r *http.Request) *web.Response + GetByID func(r *http.Request) *web.Response + Post func(r *http.Request) *web.Response + Put func(r *http.Request) *web.Response + Delete func(r *http.Request) *web.Response +} + +type Router struct { + Entity Entity + Path string + GetAll func(r *http.Request) *web.Response + GetByID func(r *http.Request) *web.Response + Post func(r *http.Request) *web.Response + Put func(r *http.Request) *web.Response + Delete func(r *http.Request) *web.Response +} + +func NewRouter(entity Entity, path string, actions RouterActions) *Router { + return &Router{ + Entity: entity, + Path: path, + GetAll: actions.GetAll, + GetByID: actions.GetByID, + Post: actions.Post, + Put: actions.Put, + Delete: actions.Delete, + } +} diff --git a/routes/boxes.go b/routes/boxes.go new file mode 100644 index 0000000..c2a2414 --- /dev/null +++ b/routes/boxes.go @@ -0,0 +1,40 @@ +package routes + +import ( + "html/template" + "net/http" + + "github.com/innocuous-symmetry/moving-mgmt/db" + "github.com/jritsema/gotoolbox/web" +) + +func Boxes(_html *template.Template) *Router { + html = _html + + return NewRouter( + Box, + "/boxes", + RouterActions{ + GetAll: GetAllBoxes, + GetByID: nil, + Post: nil, + Put: nil, + Delete: nil, + }, + ) +} + +func GetAllBoxes(_ *http.Request) *web.Response { + result, err := db.GetAllBoxes() + if err != nil { + return web.Error(http.StatusBadRequest, err, nil) + } + + return web.HTML( + http.StatusOK, + html, + "entity-list.html", + result, + nil, + ) +} diff --git a/routes/index.go b/routes/index.go new file mode 100644 index 0000000..b2dc6e6 --- /dev/null +++ b/routes/index.go @@ -0,0 +1,26 @@ +package routes + +import ( + "html/template" + "net/http" + // "github.com/innocuous-symmetry/moving-mgmt/" + db "github.com/innocuous-symmetry/moving-mgmt/db" + "github.com/jritsema/gotoolbox/web" +) + +var html *template.Template + +func HomePage(r *http.Request) *web.Response { + result, err := db.GetAllItems() + if err != nil { + panic(err) + } + + return web.HTML( + http.StatusOK, + html, + "index.html", + result, + nil, + ) +} diff --git a/routes/items.go b/routes/items.go new file mode 100644 index 0000000..1f4018f --- /dev/null +++ b/routes/items.go @@ -0,0 +1,63 @@ +package routes + +import ( + "html/template" + "net/http" + "strconv" + + db "github.com/innocuous-symmetry/moving-mgmt/db" + "github.com/jritsema/gotoolbox/web" +) + +func Items(_html *template.Template) *Router { + html = _html + + return NewRouter( + Item, + "/items", + RouterActions{ + GetAll: GetAllItems, + GetByID: nil, + Post: nil, + Put: nil, + Delete: nil, + }, + ) +} + +func GetAllItems(_ *http.Request) *web.Response { + result, err := db.GetAllItems() + if err != nil { + panic(err) + } + + return web.HTML( + http.StatusOK, + html, + "entity-list.html", + result, + nil, + ) +} + +func GetItemByID(r *http.Request) *web.Response { + var id int + id, err := strconv.Atoi(r.URL.Query().Get("id")) + if err != nil { + return web.Error(http.StatusBadRequest, err, nil) + } + + result, err := db.GetItemByID(id) + + if err != nil { + return web.Error(http.StatusInternalServerError, err, nil) + } + + return web.HTML( + http.StatusOK, + html, + "item-by-id.html", + result, + nil, + ) +} diff --git a/templates/items.html b/templates/boxes.html similarity index 95% rename from templates/items.html rename to templates/boxes.html index 9dec082..2c8eed5 100644 --- a/templates/items.html +++ b/templates/boxes.html @@ -1,4 +1,4 @@ -
+
Processing...
@@ -29,7 +29,7 @@ {{range .}} - {{ template "item-row.html". }} + {{ template "entity-row.html". }} {{end}} diff --git a/templates/entity-list.html b/templates/entity-list.html new file mode 100644 index 0000000..f004f8a --- /dev/null +++ b/templates/entity-list.html @@ -0,0 +1,40 @@ +
+
Processing...
+
+
+
+
+ + + + + + + + + + + + + + {{range .}} + {{ template "entity-row.html". }} + {{end}} + +
#NameStageCategoryDescriptionNotes
+
+
+
+
+
diff --git a/templates/item-row.html b/templates/entity-row.html similarity index 100% rename from templates/item-row.html rename to templates/entity-row.html diff --git a/templates/index.html b/templates/index.html index fa2d3ab..e7d8f42 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,9 +12,32 @@

Mikayla's Move Manager

+ + +
- Companies -
{{ template "items.html" . }}
+ Items +
+ {{ template "entity-list.html" . }} +