better setups for some view variants

This commit is contained in:
2024-03-10 03:59:52 +00:00
parent 29aeb93415
commit 01f25d4f71
7 changed files with 123 additions and 89 deletions

24
main.go
View File

@@ -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 {

View File

@@ -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,
}
}

View File

@@ -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 {

View File

@@ -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,
)

View File

@@ -49,7 +49,7 @@
<td class="whitespace-nowrap px-1 py-1">
<a
hx-put="/items/{{.ID}}"
hx-put="/items/save/{{.ID}}"
hx-target="#datarow-{{.ID}}"
hx-swap="outerHTML"
hx-indicator="#processing"
@@ -61,7 +61,7 @@
</td>
<td class="whitespace-nowrap px-1 py-1">
<a
hx-get="/company/{{.ID}}"
hx-get="/items/{{.ID}}"
hx-target="#datarow-{{.ID}}"
hx-swap="outerHTML"
hx-indicator="#processing"

View File

@@ -1,8 +1,10 @@
<tr id="datarow-{{.ID}}" class="border-b dark:border-neutral-500">
<tr id="datarow-{{.ID}}" class="border-b dark:border-neutral-500 hover:bg-slate-300 hover:bg-opacity-20">
<td class="whitespace-nowrap px-6 py-4">{{.ID}}</td>
<td class="whitespace-nowrap px-6 py-4">{{.Name}}</td>
<td class="whitespace-nowrap px-6 py-4">{{.Stage}}</td>
<td class="whitespace-nowrap px-6 py-4">{{.Category}}</td>
{{ template "parsed-category.html" .Category }}
{{ if .Description }}
<td class="whitespace-nowrap px-6 py-4">{{.Description}}</td>
@@ -18,10 +20,10 @@
<td class="whitespace-nowrap px-6 py-4">
<button
hx-get="/items/edit/{{.ID}}"
hx-target="#datarow-{{.ID}}"
hx-get="/items/edit/{{ .ID }}"
hx-target="#datarow-{{ .ID }}"
hx-swap="outerHTML"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-blue-100 transition-colors duration-150 bg-blue-700 rounded-lg focus:shadow-outline hover:bg-blue-800"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-blue-100 transition-colors duration-150 bg-yellow-700 rounded-lg focus:shadow-outline hover:bg-blue-800"
>
Edit
</button>

View File

@@ -0,0 +1,27 @@
<td class="whitespace-nowrap px-6 py-4">
{{ if eq . 0 }}
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-purple-100 text-purple-800">
Bedroom
</span>
{{ else if eq . 1 }}
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-cyan-100 text-cyan-800">
Bathroom
</span>
{{ else if eq . 2 }}
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-orange-100 text-orange-800">
Kitchen
</span>
{{ else if eq . 3 }}
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-rose-100 text-rose-800">
Office
</span>
{{ else if eq . 4 }}
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-indigo-100 text-indigo-800">
Living Room
</span>
{{ else }}
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
Other
</span>
{{ end }}
</td>