feat(treasurechest): #64 implement hirarchical treasure chests
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"spend-sparrow/handler/middleware"
|
||||
"spend-sparrow/service"
|
||||
@@ -10,7 +9,6 @@ import (
|
||||
"spend-sparrow/utils"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Account interface {
|
||||
@@ -65,19 +63,13 @@ func (h AccountImpl) handleAccountItemComp() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
idStr := r.PathValue("id")
|
||||
if idStr == "new" {
|
||||
id := r.PathValue("id")
|
||||
if id == "new" {
|
||||
comp := t.EditAccount(nil)
|
||||
h.r.Render(r, w, comp)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
handleError(w, r, fmt.Errorf("could not parse Id: %w", service.ErrBadRequest))
|
||||
return
|
||||
}
|
||||
|
||||
account, err := h.s.Get(user, id)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
@@ -106,20 +98,15 @@ func (h AccountImpl) handleUpdateAccount() http.HandlerFunc {
|
||||
account *types.Account
|
||||
err error
|
||||
)
|
||||
idStr := r.PathValue("id")
|
||||
id := r.PathValue("id")
|
||||
name := r.FormValue("name")
|
||||
if idStr == "new" {
|
||||
if id == "new" {
|
||||
account, err = h.s.Add(user, name)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
handleError(w, r, fmt.Errorf("could not parse Id: %w", service.ErrBadRequest))
|
||||
return
|
||||
}
|
||||
account, err = h.s.Update(user, id, name)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
@@ -140,13 +127,9 @@ func (h AccountImpl) handleDeleteAccount() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(r.PathValue("id"))
|
||||
if err != nil {
|
||||
handleError(w, r, fmt.Errorf("could not parse Id: %w", service.ErrBadRequest))
|
||||
return
|
||||
}
|
||||
id := r.PathValue("id")
|
||||
|
||||
err = h.s.Delete(user, id)
|
||||
err := h.s.Delete(user, id)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
|
||||
145
handler/treasure_chest.go
Normal file
145
handler/treasure_chest.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"spend-sparrow/handler/middleware"
|
||||
"spend-sparrow/service"
|
||||
t "spend-sparrow/template/treasurechest"
|
||||
"spend-sparrow/types"
|
||||
"spend-sparrow/utils"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
type TreasureChest interface {
|
||||
Handle(router *http.ServeMux)
|
||||
}
|
||||
|
||||
type TreasureChestImpl struct {
|
||||
s service.TreasureChest
|
||||
a service.Auth
|
||||
r *Render
|
||||
}
|
||||
|
||||
func NewTreasureChest(s service.TreasureChest, a service.Auth, r *Render) TreasureChest {
|
||||
return TreasureChestImpl{
|
||||
s: s,
|
||||
a: a,
|
||||
r: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (h TreasureChestImpl) Handle(r *http.ServeMux) {
|
||||
r.Handle("GET /treasurechest", h.handleTreasureChestPage())
|
||||
r.Handle("GET /treasurechest/{id}", h.handleTreasureChestItemComp())
|
||||
r.Handle("POST /treasurechest/{id}", h.handleUpdateTreasureChest())
|
||||
r.Handle("DELETE /treasurechest/{id}", h.handleDeleteTreasureChest())
|
||||
}
|
||||
|
||||
func (h TreasureChestImpl) handleTreasureChestPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
treasureChests, err := h.s.GetAll(user)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
comp := t.TreasureChest(treasureChests)
|
||||
h.r.RenderLayout(r, w, comp, user)
|
||||
}
|
||||
}
|
||||
|
||||
func (h TreasureChestImpl) handleTreasureChestItemComp() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
treasureChests, err := h.s.GetAll(user)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
id := r.PathValue("id")
|
||||
if id == "new" {
|
||||
comp := t.EditTreasureChest(nil, treasureChests)
|
||||
h.r.Render(r, w, comp)
|
||||
return
|
||||
}
|
||||
|
||||
treasureChest, err := h.s.Get(user, id)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var comp templ.Component
|
||||
if r.URL.Query().Get("edit") == "true" {
|
||||
comp = t.EditTreasureChest(treasureChest, treasureChests)
|
||||
} else {
|
||||
comp = t.TreasureChestItem(treasureChest)
|
||||
}
|
||||
h.r.Render(r, w, comp)
|
||||
}
|
||||
}
|
||||
|
||||
func (h TreasureChestImpl) handleUpdateTreasureChest() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
treasureChest *types.TreasureChest
|
||||
err error
|
||||
)
|
||||
id := r.PathValue("id")
|
||||
parentId := r.FormValue("parent-id")
|
||||
name := r.FormValue("name")
|
||||
if id == "new" {
|
||||
treasureChest, err = h.s.Add(user, parentId, name)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
treasureChest, err = h.s.Update(user, id, parentId, name)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
comp := t.TreasureChestItem(treasureChest)
|
||||
h.r.Render(r, w, comp)
|
||||
}
|
||||
}
|
||||
|
||||
func (h TreasureChestImpl) handleDeleteTreasureChest() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
id := r.PathValue("id")
|
||||
|
||||
err := h.s.Delete(user, id)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user