Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 4m44s
151 lines
3.9 KiB
Go
151 lines
3.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"spend-sparrow/handler/middleware"
|
|
"spend-sparrow/service"
|
|
"spend-sparrow/template/account"
|
|
"spend-sparrow/utils"
|
|
|
|
"net/http"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Account interface {
|
|
Handle(router *http.ServeMux)
|
|
}
|
|
|
|
type AccountImpl struct {
|
|
service service.Account
|
|
auth service.Auth
|
|
render *Render
|
|
}
|
|
|
|
func NewAccount(service service.Account, auth service.Auth, render *Render) Account {
|
|
return AccountImpl{
|
|
service: service,
|
|
auth: auth,
|
|
render: render,
|
|
}
|
|
}
|
|
|
|
func (handler AccountImpl) Handle(router *http.ServeMux) {
|
|
router.Handle("GET /account", handler.handleAccountPage())
|
|
router.Handle("GET /account/{id}", handler.handleAccountItemComp())
|
|
router.Handle("POST /account/{id}", handler.handleUpdateAccount())
|
|
// router.Handle("GET /account", handler.handleGetAccount())
|
|
// router.Handle("DELETE /account/{id}", handler.handleDeleteAccount())
|
|
}
|
|
|
|
func (handler 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 := handler.service.GetAll(user)
|
|
if err != nil {
|
|
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
comp := account.Account(accounts)
|
|
handler.render.RenderLayout(r, w, comp, user)
|
|
}
|
|
}
|
|
|
|
func (handler 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
|
|
}
|
|
id, err := uuid.Parse(r.PathValue("id"))
|
|
if err != nil {
|
|
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
accounts, err := handler.service.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 = account.EditAccount(accounts)
|
|
} else {
|
|
comp = account.AccountItem(accounts)
|
|
}
|
|
handler.render.Render(r, w, comp)
|
|
}
|
|
}
|
|
|
|
func (handler 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
|
|
}
|
|
|
|
utils.TriggerToastWithStatus(w, r, "error", "Account not yet updated", http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
// }
|
|
// }
|
|
// }
|