Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
137 lines
2.7 KiB
Go
137 lines
2.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"spend-sparrow/internal/handler/middleware"
|
|
"spend-sparrow/internal/service"
|
|
t "spend-sparrow/internal/template/account"
|
|
"spend-sparrow/internal/types"
|
|
"spend-sparrow/internal/utils"
|
|
|
|
"github.com/a-h/templ"
|
|
)
|
|
|
|
type Account interface {
|
|
Handle(router *http.ServeMux)
|
|
}
|
|
|
|
type AccountImpl struct {
|
|
s service.Account
|
|
r *Render
|
|
}
|
|
|
|
func NewAccount(s service.Account, r *Render) Account {
|
|
return AccountImpl{
|
|
s: s,
|
|
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 {
|
|
handleError(w, r, err)
|
|
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
|
|
}
|
|
|
|
id := r.PathValue("id")
|
|
if id == "new" {
|
|
comp := t.EditAccount(nil)
|
|
h.r.Render(r, w, comp)
|
|
return
|
|
}
|
|
|
|
account, 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.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
|
|
)
|
|
id := r.PathValue("id")
|
|
name := r.FormValue("name")
|
|
if id == "new" {
|
|
account, err = h.s.Add(user, name)
|
|
if err != nil {
|
|
handleError(w, r, err)
|
|
return
|
|
}
|
|
} else {
|
|
account, err = h.s.UpdateName(user, id, name)
|
|
if err != nil {
|
|
handleError(w, r, err)
|
|
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 := r.PathValue("id")
|
|
|
|
err := h.s.Delete(user, id)
|
|
if err != nil {
|
|
handleError(w, r, err)
|
|
return
|
|
}
|
|
}
|
|
}
|