diff --git a/main.go b/main.go index bf8e72d..daeee2e 100644 --- a/main.go +++ b/main.go @@ -23,9 +23,6 @@ var ( //go:embed all:templates/* templateFS embed.FS - //go:embed css/output.css - css embed.FS - //parsed templates html *template.Template ) @@ -45,14 +42,25 @@ func main() { //add routes router := http.NewServeMux() - // router.Handle("/css/output.css", http.FileServer(http.FS(css))) - // router.Handle("/company/add", web.Action(companyAdd)) - // router.Handle("/company/add/", web.Action(companyAdd)) + itemActions := routes.Items(html) + boxActions := routes.Boxes(html) + + router.Handle("/items/edit", web.Action(itemActions.Edit)) + router.Handle("/items/delete", web.Action(itemActions.Delete)) + router.Handle("/items/save", web.Action(itemActions.Save)) + router.Handle("/items/edit/", web.Action(itemActions.Edit)) + router.Handle("/items/delete/", web.Action(itemActions.Delete)) + router.Handle("/items/save/", web.Action(itemActions.Save)) + + router.Handle("/items", web.Action(itemActions.Get)) + router.Handle("/items/:id", web.Action(itemActions.Get)) + router.Handle("/boxes", web.Action(boxActions.GetAll)) + router.Handle("/items/", web.Action(itemActions.Get)) + router.Handle("/boxes/", web.Action(boxActions.GetAll)) router.Handle("/", web.Action(routes.HomePage)) - router.Handle("/items", web.Action(routes.Items(html).GetAll)) - router.Handle("/boxes", web.Action(routes.Boxes(html).GetAll)) + router.Handle("/index.html", web.Action(routes.HomePage)) //logging/tracing nextRequestID := func() string { diff --git a/routes/base.go b/routes/base.go deleted file mode 100644 index f304360..0000000 --- a/routes/base.go +++ /dev/null @@ -1,45 +0,0 @@ -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 index c2a2414..71ec8f2 100644 --- a/routes/boxes.go +++ b/routes/boxes.go @@ -8,20 +8,24 @@ import ( "github.com/jritsema/gotoolbox/web" ) -func Boxes(_html *template.Template) *Router { +type BoxActions 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 +} + +func Boxes(_html *template.Template) *BoxActions { html = _html - return NewRouter( - Box, - "/boxes", - RouterActions{ - GetAll: GetAllBoxes, - GetByID: nil, - Post: nil, - Put: nil, - Delete: nil, - }, - ) + return &BoxActions{ + GetAll: GetAllBoxes, + GetByID: nil, + Post: nil, + Put: nil, + Delete: nil, + } } func GetAllBoxes(_ *http.Request) *web.Response { diff --git a/routes/items.go b/routes/items.go index 1f4018f..9fe2d8a 100644 --- a/routes/items.go +++ b/routes/items.go @@ -9,20 +9,34 @@ import ( "github.com/jritsema/gotoolbox/web" ) -func Items(_html *template.Template) *Router { +type ItemActions struct { + Get func(r *http.Request) *web.Response + GetAll func(r *http.Request) *web.Response + Edit func(r *http.Request) *web.Response + Delete func(r *http.Request) *web.Response + Save func(r *http.Request) *web.Response +} + +func Items(_html *template.Template) *ItemActions { html = _html - return NewRouter( - Item, - "/items", - RouterActions{ - GetAll: GetAllItems, - GetByID: nil, - Post: nil, - Put: nil, - Delete: nil, - }, - ) + return &ItemActions{ + Get: Get, + GetAll: GetAllItems, + Edit: EditItem, + Delete: nil, + Save: nil, + } +} + +func Get(r *http.Request) *web.Response { + _, count := web.PathLast(r) + + if count == 0 { + return GetAllItems(r) + } else { + return GetItemByID(r) + } } func GetAllItems(_ *http.Request) *web.Response { @@ -40,14 +54,15 @@ func GetAllItems(_ *http.Request) *web.Response { ) } -func GetItemByID(r *http.Request) *web.Response { - var id int - id, err := strconv.Atoi(r.URL.Query().Get("id")) +func EditItem(r *http.Request) *web.Response { + idFromPath, _ := web.PathLast(r) + + id, err := strconv.ParseInt(idFromPath, 10, 64) if err != nil { return web.Error(http.StatusBadRequest, err, nil) } - result, err := db.GetItemByID(id) + result, err := db.GetItemByID(int(id)) if err != nil { return web.Error(http.StatusInternalServerError, err, nil) @@ -56,7 +71,30 @@ func GetItemByID(r *http.Request) *web.Response { return web.HTML( http.StatusOK, html, - "item-by-id.html", + "entity-edit.html", + result, + nil, + ) +} + +func GetItemByID(r *http.Request) *web.Response { + idFromPath, _ := web.PathLast(r) + + id, err := strconv.ParseInt(idFromPath, 10, 64) + if err != nil { + return web.Error(http.StatusBadRequest, err, nil) + } + + result, err := db.GetItemByID(int(id)) + + if err != nil { + return web.Error(http.StatusInternalServerError, err, nil) + } + + return web.HTML( + http.StatusOK, + html, + "entity-row.html", result, nil, ) diff --git a/templates/row-edit.html b/templates/entity-edit.html similarity index 97% rename from templates/row-edit.html rename to templates/entity-edit.html index 0e0f839..342538a 100644 --- a/templates/row-edit.html +++ b/templates/entity-edit.html @@ -49,7 +49,7 @@ + {{.ID}} {{.Name}} + {{.Stage}} - {{.Category}} + + {{ template "parsed-category.html" .Category }} {{ if .Description }} {{.Description}} @@ -18,10 +20,10 @@ diff --git a/templates/parsed-category.html b/templates/parsed-category.html new file mode 100644 index 0000000..5a892dd --- /dev/null +++ b/templates/parsed-category.html @@ -0,0 +1,27 @@ + + {{ if eq . 0 }} + + Bedroom + + {{ else if eq . 1 }} + + Bathroom + + {{ else if eq . 2 }} + + Kitchen + + {{ else if eq . 3 }} + + Office + + {{ else if eq . 4 }} + + Living Room + + {{ else }} + + Other + + {{ end }} +