feat(account): #49 account page
This commit was merged in pull request #59.
This commit is contained in:
@@ -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.AccountListComp(nil)
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,9 +96,9 @@ func (handler AuthImpl) handleSignIn() http.HandlerFunc {
|
||||
|
||||
if err != nil {
|
||||
if err == service.ErrInvalidCredentials {
|
||||
utils.TriggerToast(w, r, "error", "Invalid email or password", http.StatusUnauthorized)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Invalid email or password", http.StatusUnauthorized)
|
||||
} else {
|
||||
utils.TriggerToast(w, r, "error", "An error occurred", http.StatusInternalServerError)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "An error occurred", http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -204,19 +204,19 @@ func (handler AuthImpl) handleSignUp() http.HandlerFunc {
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, types.ErrInternal) {
|
||||
utils.TriggerToast(w, r, "error", "An error occurred", http.StatusInternalServerError)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "An error occurred", http.StatusInternalServerError)
|
||||
return
|
||||
} else if errors.Is(err, service.ErrInvalidEmail) {
|
||||
utils.TriggerToast(w, r, "error", "The email provided is invalid", http.StatusBadRequest)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "The email provided is invalid", http.StatusBadRequest)
|
||||
return
|
||||
} else if errors.Is(err, service.ErrInvalidPassword) {
|
||||
utils.TriggerToast(w, r, "error", service.ErrInvalidPassword.Error(), http.StatusBadRequest)
|
||||
utils.TriggerToastWithStatus(w, r, "error", service.ErrInvalidPassword.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// If err is "service.ErrAccountExists", then just continue
|
||||
}
|
||||
|
||||
utils.TriggerToast(w, r, "success", "An activation link has been send to your email", http.StatusOK)
|
||||
utils.TriggerToastWithStatus(w, r, "success", "An activation link has been send to your email", http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,9 +273,9 @@ func (handler AuthImpl) handleDeleteAccountComp() http.HandlerFunc {
|
||||
err := handler.service.DeleteAccount(user, password)
|
||||
if err != nil {
|
||||
if err == service.ErrInvalidCredentials {
|
||||
utils.TriggerToast(w, r, "error", "Password not correct", http.StatusBadRequest)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Password not correct", http.StatusBadRequest)
|
||||
} else {
|
||||
utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -307,7 +307,7 @@ func (handler AuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
||||
session := middleware.GetSession(r)
|
||||
user := middleware.GetUser(r)
|
||||
if session == nil || user == nil {
|
||||
utils.TriggerToast(w, r, "error", "Unathorized", http.StatusUnauthorized)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Unathorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -316,11 +316,11 @@ func (handler AuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
||||
|
||||
err := handler.service.ChangePassword(user, session.Id, currPass, newPass)
|
||||
if err != nil {
|
||||
utils.TriggerToast(w, r, "error", "Password not correct", http.StatusBadRequest)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Password not correct", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
utils.TriggerToast(w, r, "success", "Password changed", http.StatusOK)
|
||||
utils.TriggerToastWithStatus(w, r, "success", "Password changed", http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,7 +343,7 @@ func (handler AuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
||||
|
||||
email := r.FormValue("email")
|
||||
if email == "" {
|
||||
utils.TriggerToast(w, r, "error", "Please enter an email", http.StatusBadRequest)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Please enter an email", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -353,9 +353,9 @@ func (handler AuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
} else {
|
||||
utils.TriggerToast(w, r, "info", "If the address exists, an email has been sent.", http.StatusOK)
|
||||
utils.TriggerToastWithStatus(w, r, "info", "If the address exists, an email has been sent.", http.StatusOK)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -365,7 +365,7 @@ func (handler AuthImpl) handleForgotPasswordResponseComp() http.HandlerFunc {
|
||||
pageUrl, err := url.Parse(r.Header.Get("HX-Current-URL"))
|
||||
if err != nil {
|
||||
log.Error("Could not get current URL: %v", err)
|
||||
utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -374,9 +374,9 @@ func (handler AuthImpl) handleForgotPasswordResponseComp() http.HandlerFunc {
|
||||
|
||||
err = handler.service.ForgotPassword(token, newPass)
|
||||
if err != nil {
|
||||
utils.TriggerToast(w, r, "error", err.Error(), http.StatusBadRequest)
|
||||
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
utils.TriggerToast(w, r, "success", "Password changed", http.StatusOK)
|
||||
utils.TriggerToastWithStatus(w, r, "success", "Password changed", http.StatusOK)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func CrossSiteRequestForgery(auth service.Auth) func(http.Handler) http.Handler
|
||||
if session == nil || csrfToken == "" || !auth.IsCsrfTokenValid(csrfToken, session.Id) {
|
||||
log.Info("CSRF-Token not correct")
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
utils.TriggerToast(w, r, "error", "CSRF-Token not correct", http.StatusBadRequest)
|
||||
utils.TriggerToastWithStatus(w, r, "error", "CSRF-Token not correct", http.StatusBadRequest)
|
||||
} else {
|
||||
http.Error(w, "CSRF-Token not correct", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
38
handler/middleware/gzip.go
Normal file
38
handler/middleware/gzip.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"spend-sparrow/log"
|
||||
)
|
||||
|
||||
type gzipResponseWriter struct {
|
||||
io.Writer
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
||||
return w.Writer.Write(b)
|
||||
}
|
||||
|
||||
func Gzip(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
gz := gzip.NewWriter(w)
|
||||
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
|
||||
next.ServeHTTP(gzr, r)
|
||||
|
||||
err := gz.Close()
|
||||
if err != nil {
|
||||
log.Error("Gzip: could not close Writer: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -2,10 +2,11 @@ package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Chain list of handlers together
|
||||
func Wrapper(next http.Handler, handlers ...func(http.Handler) http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
lastHandler := next
|
||||
for i := len(handlers) - 1; i >= 0; i-- {
|
||||
for i := 0; i < len(handlers); i++ {
|
||||
lastHandler = handlers[i](lastHandler)
|
||||
}
|
||||
lastHandler.ServeHTTP(w, r)
|
||||
|
||||
@@ -28,6 +28,7 @@ func NewIndex(service service.Auth, render *Render) Index {
|
||||
|
||||
func (handler IndexImpl) Handle(router *http.ServeMux) {
|
||||
router.Handle("/", handler.handleRootAnd404())
|
||||
router.Handle("/empty", handler.handleEmpty())
|
||||
}
|
||||
|
||||
func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
|
||||
@@ -52,3 +53,9 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
|
||||
handler.render.RenderLayoutWithStatus(r, w, comp, user, status)
|
||||
}
|
||||
}
|
||||
|
||||
func (handler IndexImpl) handleEmpty() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Return nothing
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user