Files
spend-sparrow/handler/account.go
Tim Wundenberg ef40f04f11
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
feat(account): #49 implement account get, insert, update and delete
2025-05-07 23:02:17 +02:00

158 lines
3.6 KiB
Go

package handler
import (
"spend-sparrow/handler/middleware"
"spend-sparrow/log"
"spend-sparrow/service"
t "spend-sparrow/template/account"
"spend-sparrow/types"
"spend-sparrow/utils"
"net/http"
"github.com/a-h/templ"
"github.com/google/uuid"
)
type Account interface {
Handle(router *http.ServeMux)
}
type AccountImpl struct {
s service.Account
a service.Auth
r *Render
}
func NewAccount(s service.Account, a service.Auth, r *Render) Account {
return AccountImpl{
s: s,
a: a,
r: r,
}
}
func (h AccountImpl) Handle(r *http.ServeMux) {
r.Handle("GET /account", h.handleAccountPage())
r.Handle("GET /account/{id}", h.handleAccountItemComp())
r.Handle("POST /account/{id}", h.handleUpdateAccount())
r.Handle("DELETE /account/{id}", h.handleDeleteAccount())
}
func (h AccountImpl) handleAccountPage() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := middleware.GetUser(r)
if user == nil {
utils.DoRedirect(w, r, "/auth/signin")
return
}
accounts, err := h.s.GetAll(user)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
return
}
comp := t.Account(accounts)
h.r.RenderLayout(r, w, comp, user)
}
}
func (h AccountImpl) handleAccountItemComp() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := middleware.GetUser(r)
if user == nil {
utils.DoRedirect(w, r, "/auth/signin")
return
}
idStr := r.PathValue("id")
if idStr == "new" {
comp := t.EditAccount(nil)
log.Info("Component: %v", comp)
h.r.Render(r, w, comp)
return
}
id, err := uuid.Parse(idStr)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", http.StatusBadRequest)
return
}
account, err := h.s.Get(user, id)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
return
}
var comp templ.Component
if r.URL.Query().Get("edit") == "true" {
comp = t.EditAccount(account)
} else {
comp = t.AccountItem(account)
}
h.r.Render(r, w, comp)
}
}
func (h AccountImpl) handleUpdateAccount() 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 (
account *types.Account
err error
)
idStr := r.PathValue("id")
name := r.FormValue("name")
if idStr == "new" {
account, err = h.s.Add(user, name)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
return
}
} else {
id, err := uuid.Parse(idStr)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", http.StatusBadRequest)
return
}
account, err = h.s.Update(user, id, name)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
return
}
}
comp := t.AccountItem(account)
h.r.Render(r, w, comp)
}
}
func (h AccountImpl) handleDeleteAccount() 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, err := uuid.Parse(r.PathValue("id"))
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", http.StatusBadRequest)
return
}
err = h.s.Delete(user, id)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
return
}
}
}