better setups for some view variants
This commit is contained in:
24
main.go
24
main.go
@@ -23,9 +23,6 @@ var (
|
|||||||
//go:embed all:templates/*
|
//go:embed all:templates/*
|
||||||
templateFS embed.FS
|
templateFS embed.FS
|
||||||
|
|
||||||
//go:embed css/output.css
|
|
||||||
css embed.FS
|
|
||||||
|
|
||||||
//parsed templates
|
//parsed templates
|
||||||
html *template.Template
|
html *template.Template
|
||||||
)
|
)
|
||||||
@@ -45,14 +42,25 @@ func main() {
|
|||||||
|
|
||||||
//add routes
|
//add routes
|
||||||
router := http.NewServeMux()
|
router := http.NewServeMux()
|
||||||
// router.Handle("/css/output.css", http.FileServer(http.FS(css)))
|
|
||||||
|
|
||||||
// router.Handle("/company/add", web.Action(companyAdd))
|
itemActions := routes.Items(html)
|
||||||
// router.Handle("/company/add/", web.Action(companyAdd))
|
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("/", web.Action(routes.HomePage))
|
||||||
router.Handle("/items", web.Action(routes.Items(html).GetAll))
|
router.Handle("/index.html", web.Action(routes.HomePage))
|
||||||
router.Handle("/boxes", web.Action(routes.Boxes(html).GetAll))
|
|
||||||
|
|
||||||
//logging/tracing
|
//logging/tracing
|
||||||
nextRequestID := func() string {
|
nextRequestID := func() string {
|
||||||
|
|||||||
@@ -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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,20 +8,24 @@ import (
|
|||||||
"github.com/jritsema/gotoolbox/web"
|
"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
|
html = _html
|
||||||
|
|
||||||
return NewRouter(
|
return &BoxActions{
|
||||||
Box,
|
|
||||||
"/boxes",
|
|
||||||
RouterActions{
|
|
||||||
GetAll: GetAllBoxes,
|
GetAll: GetAllBoxes,
|
||||||
GetByID: nil,
|
GetByID: nil,
|
||||||
Post: nil,
|
Post: nil,
|
||||||
Put: nil,
|
Put: nil,
|
||||||
Delete: nil,
|
Delete: nil,
|
||||||
},
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllBoxes(_ *http.Request) *web.Response {
|
func GetAllBoxes(_ *http.Request) *web.Response {
|
||||||
|
|||||||
@@ -9,20 +9,34 @@ import (
|
|||||||
"github.com/jritsema/gotoolbox/web"
|
"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
|
html = _html
|
||||||
|
|
||||||
return NewRouter(
|
return &ItemActions{
|
||||||
Item,
|
Get: Get,
|
||||||
"/items",
|
|
||||||
RouterActions{
|
|
||||||
GetAll: GetAllItems,
|
GetAll: GetAllItems,
|
||||||
GetByID: nil,
|
Edit: EditItem,
|
||||||
Post: nil,
|
|
||||||
Put: nil,
|
|
||||||
Delete: nil,
|
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 {
|
func GetAllItems(_ *http.Request) *web.Response {
|
||||||
@@ -40,14 +54,15 @@ func GetAllItems(_ *http.Request) *web.Response {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetItemByID(r *http.Request) *web.Response {
|
func EditItem(r *http.Request) *web.Response {
|
||||||
var id int
|
idFromPath, _ := web.PathLast(r)
|
||||||
id, err := strconv.Atoi(r.URL.Query().Get("id"))
|
|
||||||
|
id, err := strconv.ParseInt(idFromPath, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return web.Error(http.StatusBadRequest, err, nil)
|
return web.Error(http.StatusBadRequest, err, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := db.GetItemByID(id)
|
result, err := db.GetItemByID(int(id))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return web.Error(http.StatusInternalServerError, err, nil)
|
return web.Error(http.StatusInternalServerError, err, nil)
|
||||||
@@ -56,7 +71,30 @@ func GetItemByID(r *http.Request) *web.Response {
|
|||||||
return web.HTML(
|
return web.HTML(
|
||||||
http.StatusOK,
|
http.StatusOK,
|
||||||
html,
|
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,
|
result,
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@
|
|||||||
|
|
||||||
<td class="whitespace-nowrap px-1 py-1">
|
<td class="whitespace-nowrap px-1 py-1">
|
||||||
<a
|
<a
|
||||||
hx-put="/items/{{.ID}}"
|
hx-put="/items/save/{{.ID}}"
|
||||||
hx-target="#datarow-{{.ID}}"
|
hx-target="#datarow-{{.ID}}"
|
||||||
hx-swap="outerHTML"
|
hx-swap="outerHTML"
|
||||||
hx-indicator="#processing"
|
hx-indicator="#processing"
|
||||||
@@ -61,7 +61,7 @@
|
|||||||
</td>
|
</td>
|
||||||
<td class="whitespace-nowrap px-1 py-1">
|
<td class="whitespace-nowrap px-1 py-1">
|
||||||
<a
|
<a
|
||||||
hx-get="/company/{{.ID}}"
|
hx-get="/items/{{.ID}}"
|
||||||
hx-target="#datarow-{{.ID}}"
|
hx-target="#datarow-{{.ID}}"
|
||||||
hx-swap="outerHTML"
|
hx-swap="outerHTML"
|
||||||
hx-indicator="#processing"
|
hx-indicator="#processing"
|
||||||
@@ -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">{{.ID}}</td>
|
||||||
<td class="whitespace-nowrap px-6 py-4">{{.Name}}</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">{{.Stage}}</td>
|
||||||
<td class="whitespace-nowrap px-6 py-4">{{.Category}}</td>
|
|
||||||
|
{{ template "parsed-category.html" .Category }}
|
||||||
|
|
||||||
{{ if .Description }}
|
{{ if .Description }}
|
||||||
<td class="whitespace-nowrap px-6 py-4">{{.Description}}</td>
|
<td class="whitespace-nowrap px-6 py-4">{{.Description}}</td>
|
||||||
@@ -18,10 +20,10 @@
|
|||||||
|
|
||||||
<td class="whitespace-nowrap px-6 py-4">
|
<td class="whitespace-nowrap px-6 py-4">
|
||||||
<button
|
<button
|
||||||
hx-get="/items/edit/{{.ID}}"
|
hx-get="/items/edit/{{ .ID }}"
|
||||||
hx-target="#datarow-{{.ID}}"
|
hx-target="#datarow-{{ .ID }}"
|
||||||
hx-swap="outerHTML"
|
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
|
Edit
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
27
templates/parsed-category.html
Normal file
27
templates/parsed-category.html
Normal 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>
|
||||||
Reference in New Issue
Block a user