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 } } }