feat(account): #49 implement account get, insert, update and delete
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled

This commit is contained in:
2025-05-07 18:49:56 +02:00
parent c8daf6a04d
commit ef40f04f11
14 changed files with 363 additions and 181 deletions

View File

@@ -2,11 +2,16 @@ package handler
import (
"spend-sparrow/handler/middleware"
"spend-sparrow/log"
"spend-sparrow/service"
"spend-sparrow/template/account"
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 {
@@ -14,27 +19,27 @@ type Account interface {
}
type AccountImpl struct {
service service.Account
auth service.Auth
render *Render
s service.Account
a service.Auth
r *Render
}
func NewAccount(service service.Account, auth service.Auth, render *Render) Account {
func NewAccount(s service.Account, a service.Auth, r *Render) Account {
return AccountImpl{
service: service,
auth: auth,
render: render,
s: s,
a: a,
r: r,
}
}
func (handler AccountImpl) Handle(router *http.ServeMux) {
router.Handle("/account", handler.handleAccountPage())
// router.Handle("POST /account", handler.handleAddAccount())
// router.Handle("GET /account", handler.handleGetAccount())
// router.Handle("DELETE /account/{id}", handler.handleDeleteAccount())
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 (handler AccountImpl) handleAccountPage() http.HandlerFunc {
func (h AccountImpl) handleAccountPage() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := middleware.GetUser(r)
if user == nil {
@@ -42,85 +47,111 @@ func (handler AccountImpl) handleAccountPage() http.HandlerFunc {
return
}
comp := account.Account()
handler.render.RenderLayout(r, w, comp, user)
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 (handler AccountImpl) handleAddAccount() 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 dateStr = r.FormValue("date")
// var typeStr = r.FormValue("type")
// var setsStr = r.FormValue("sets")
// var repsStr = r.FormValue("reps")
//
// wo := service.NewAccountDto("", dateStr, typeStr, setsStr, repsStr)
// wo, err := handler.service.AddAccount(user, wo)
// if err != nil {
// utils.TriggerToast(w, r, "error", "Invalid input values", http.StatusBadRequest)
// http.Error(w, "Invalid input values", http.StatusBadRequest)
// return
// }
// wor := account.Account{Id: wo.RowId, Date: wo.Date, Type: wo.Type, Sets: wo.Sets, Reps: wo.Reps}
//
// comp := account.AccountItemComp(wor, true)
// handler.render.Render(r, w, comp)
// }
// }
//
// func (handler AccountImpl) handleGetAccount() http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
// user := middleware.GetUser(r)
// if user == nil {
// utils.DoRedirect(w, r, "/auth/signin")
// return
// }
//
// workouts, err := handler.service.GetAccounts(user)
// if err != nil {
// return
// }
//
// wos := make([]*types.Account, 0)
// for _, wo := range workouts {
// wos = append(wos, *types.Account{Id: wo.RowId, Date: wo.Date, Type: wo.Type, Sets: wo.Sets, Reps: wo.Reps})
// }
//
// comp := account.AccountListComp(wos)
// handler.render.Render(r, w, comp)
// }
// }
//
// func (handler 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
// }
//
// rowId := r.PathValue("id")
// if rowId == "" {
// utils.TriggerToast(w, r, "error", "Missing ID field", http.StatusBadRequest)
// return
// }
//
// rowIdInt, err := strconv.Atoi(rowId)
// if err != nil {
// utils.TriggerToast(w, r, "error", "Invalid ID", http.StatusBadRequest)
// return
// }
//
// err = handler.service.DeleteAccount(user, rowIdInt)
// if err != nil {
// utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
// return
// }
// }
// }
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
}
}
}