feat(account): #49 account page
This commit was merged in pull request #59.
This commit is contained in:
@@ -8,12 +8,13 @@ import (
|
|||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// While it may be duplicated to check for groupIds in the database access, it serves as a security layer
|
||||||
type Account interface {
|
type Account interface {
|
||||||
Insert(account *types.Account) error
|
Insert(groupId uuid.UUID, account *types.Account) error
|
||||||
Update(account *types.Account) error
|
Update(groupId uuid.UUID, account *types.Account) error
|
||||||
GetAll(groupId uuid.UUID) ([]*types.Account, error)
|
GetAll(groupId uuid.UUID) ([]*types.Account, error)
|
||||||
Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, error)
|
Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, error)
|
||||||
Delete(id uuid.UUID) error
|
Delete(groupId uuid.UUID, id uuid.UUID) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountSqlite struct {
|
type AccountSqlite struct {
|
||||||
@@ -24,11 +25,11 @@ func NewAccountSqlite(db *sqlx.DB) *AccountSqlite {
|
|||||||
return &AccountSqlite{db: db}
|
return &AccountSqlite{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AccountSqlite) Insert(account *types.Account) error {
|
func (db AccountSqlite) Insert(groupId uuid.UUID, account *types.Account) error {
|
||||||
|
|
||||||
_, err := db.db.Exec(`
|
_, err := db.db.Exec(`
|
||||||
INSERT INTO account (id, group_id, name, current_balance, oink_balance, created_at, created_by)
|
INSERT INTO account (id, group_id, name, current_balance, oink_balance, created_at, created_by)
|
||||||
VALUES (?,?,?,?,?,?,?)`, account.Id, account.GroupId, 0, 0, account.CreatedAt, account.CreatedBy)
|
VALUES (?,?,?,?,?,?,?)`, account.Id, groupId, account.Name, 0, 0, account.CreatedAt, account.CreatedBy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error inserting account: %v", err)
|
log.Error("Error inserting account: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
@@ -37,22 +38,34 @@ func (db AccountSqlite) Insert(account *types.Account) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AccountSqlite) Update(account *types.Account) error {
|
func (db AccountSqlite) Update(groupId uuid.UUID, account *types.Account) error {
|
||||||
|
|
||||||
_, err := db.db.Exec(`
|
log.Info("Updating account: %v", account)
|
||||||
|
r, err := db.db.Exec(`
|
||||||
UPDATE account
|
UPDATE account
|
||||||
|
SET
|
||||||
name = ?,
|
name = ?,
|
||||||
current_balance = ?,
|
current_balance = ?,
|
||||||
last_transaction = ?,
|
last_transaction = ?,
|
||||||
oink_balance = ?,
|
oink_balance = ?,
|
||||||
updated_at = ?,
|
updated_at = ?,
|
||||||
updated_by = ?,
|
updated_by = ?
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
AND group_id = ?`, account.Name, account.CurrentBalance, account.LastTransaction, account.OinkBalance, account.UpdatedAt, account.UpdatedBy, account.Id, account.GroupId)
|
AND group_id = ?`, account.Name, account.CurrentBalance, account.LastTransaction, account.OinkBalance, account.UpdatedAt, account.UpdatedBy, account.Id, groupId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error updating account: %v", err)
|
log.Error("Error updating account: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
rows, err := r.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Error deleting account, getting rows affected: %v", err)
|
||||||
|
return types.ErrInternal
|
||||||
|
}
|
||||||
|
|
||||||
|
if rows == 0 {
|
||||||
|
log.Error("Error deleting account, rows affected: %v", rows)
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -62,7 +75,7 @@ func (db AccountSqlite) GetAll(groupId uuid.UUID) ([]*types.Account, error) {
|
|||||||
accounts := make([]*types.Account, 0)
|
accounts := make([]*types.Account, 0)
|
||||||
err := db.db.Select(&accounts, `
|
err := db.db.Select(&accounts, `
|
||||||
SELECT
|
SELECT
|
||||||
id, name,
|
id, group_id, name,
|
||||||
current_balance, last_transaction, oink_balance,
|
current_balance, last_transaction, oink_balance,
|
||||||
created_at, created_by, updated_at, updated_by
|
created_at, created_by, updated_at, updated_by
|
||||||
FROM account
|
FROM account
|
||||||
@@ -81,7 +94,7 @@ func (db AccountSqlite) Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, er
|
|||||||
account := &types.Account{}
|
account := &types.Account{}
|
||||||
err := db.db.Get(account, `
|
err := db.db.Get(account, `
|
||||||
SELECT
|
SELECT
|
||||||
id, name,
|
id, group_id, name,
|
||||||
current_balance, last_transaction, oink_balance,
|
current_balance, last_transaction, oink_balance,
|
||||||
created_at, created_by, updated_at, updated_by
|
created_at, created_by, updated_at, updated_by
|
||||||
FROM account
|
FROM account
|
||||||
@@ -95,9 +108,9 @@ func (db AccountSqlite) Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, er
|
|||||||
return account, nil
|
return account, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AccountSqlite) Delete(id uuid.UUID) error {
|
func (db AccountSqlite) Delete(groupId uuid.UUID, id uuid.UUID) error {
|
||||||
|
|
||||||
res, err := db.db.Exec("DELETE FROM account WHERE id = ?", id)
|
res, err := db.db.Exec("DELETE FROM account WHERE id = ? and group_id = ?", id, groupId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error deleting account: %v", err)
|
log.Error("Error deleting account: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
@@ -110,6 +123,7 @@ func (db AccountSqlite) Delete(id uuid.UUID) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if rows == 0 {
|
if rows == 0 {
|
||||||
|
log.Error("Error deleting account, rows affected: %v", rows)
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,16 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/handler/middleware"
|
"spend-sparrow/handler/middleware"
|
||||||
|
"spend-sparrow/log"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/service"
|
||||||
"spend-sparrow/template/account"
|
t "spend-sparrow/template/account"
|
||||||
|
"spend-sparrow/types"
|
||||||
"spend-sparrow/utils"
|
"spend-sparrow/utils"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/a-h/templ"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Account interface {
|
type Account interface {
|
||||||
@@ -14,27 +19,27 @@ type Account interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AccountImpl struct {
|
type AccountImpl struct {
|
||||||
service service.Account
|
s service.Account
|
||||||
auth service.Auth
|
a service.Auth
|
||||||
render *Render
|
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{
|
return AccountImpl{
|
||||||
service: service,
|
s: s,
|
||||||
auth: auth,
|
a: a,
|
||||||
render: render,
|
r: r,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler AccountImpl) Handle(router *http.ServeMux) {
|
func (h AccountImpl) Handle(r *http.ServeMux) {
|
||||||
router.Handle("/account", handler.handleAccountPage())
|
r.Handle("GET /account", h.handleAccountPage())
|
||||||
// router.Handle("POST /account", handler.handleAddAccount())
|
r.Handle("GET /account/{id}", h.handleAccountItemComp())
|
||||||
// router.Handle("GET /account", handler.handleGetAccount())
|
r.Handle("POST /account/{id}", h.handleUpdateAccount())
|
||||||
// router.Handle("DELETE /account/{id}", handler.handleDeleteAccount())
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user := middleware.GetUser(r)
|
user := middleware.GetUser(r)
|
||||||
if user == nil {
|
if user == nil {
|
||||||
@@ -42,85 +47,111 @@ func (handler AccountImpl) handleAccountPage() http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
comp := account.AccountListComp(nil)
|
accounts, err := h.s.GetAll(user)
|
||||||
handler.render.RenderLayout(r, w, comp, 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 {
|
func (h AccountImpl) handleAccountItemComp() http.HandlerFunc {
|
||||||
// return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// user := middleware.GetUser(r)
|
user := middleware.GetUser(r)
|
||||||
// if user == nil {
|
if user == nil {
|
||||||
// utils.DoRedirect(w, r, "/auth/signin")
|
utils.DoRedirect(w, r, "/auth/signin")
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// var dateStr = r.FormValue("date")
|
idStr := r.PathValue("id")
|
||||||
// var typeStr = r.FormValue("type")
|
if idStr == "new" {
|
||||||
// var setsStr = r.FormValue("sets")
|
comp := t.EditAccount(nil)
|
||||||
// var repsStr = r.FormValue("reps")
|
log.Info("Component: %v", comp)
|
||||||
//
|
h.r.Render(r, w, comp)
|
||||||
// wo := service.NewAccountDto("", dateStr, typeStr, setsStr, repsStr)
|
return
|
||||||
// wo, err := handler.service.AddAccount(user, wo)
|
}
|
||||||
// if err != nil {
|
|
||||||
// utils.TriggerToast(w, r, "error", "Invalid input values", http.StatusBadRequest)
|
id, err := uuid.Parse(idStr)
|
||||||
// http.Error(w, "Invalid input values", http.StatusBadRequest)
|
if err != nil {
|
||||||
// return
|
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", 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)
|
account, err := h.s.Get(user, id)
|
||||||
// handler.render.Render(r, w, comp)
|
if err != nil {
|
||||||
// }
|
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||||
// }
|
return
|
||||||
//
|
}
|
||||||
// func (handler AccountImpl) handleGetAccount() http.HandlerFunc {
|
|
||||||
// return func(w http.ResponseWriter, r *http.Request) {
|
var comp templ.Component
|
||||||
// user := middleware.GetUser(r)
|
if r.URL.Query().Get("edit") == "true" {
|
||||||
// if user == nil {
|
comp = t.EditAccount(account)
|
||||||
// utils.DoRedirect(w, r, "/auth/signin")
|
} else {
|
||||||
// return
|
comp = t.AccountItem(account)
|
||||||
// }
|
}
|
||||||
//
|
h.r.Render(r, w, comp)
|
||||||
// workouts, err := handler.service.GetAccounts(user)
|
}
|
||||||
// if err != nil {
|
}
|
||||||
// return
|
|
||||||
// }
|
func (h AccountImpl) handleUpdateAccount() http.HandlerFunc {
|
||||||
//
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// wos := make([]*types.Account, 0)
|
user := middleware.GetUser(r)
|
||||||
// for _, wo := range workouts {
|
if user == nil {
|
||||||
// wos = append(wos, *types.Account{Id: wo.RowId, Date: wo.Date, Type: wo.Type, Sets: wo.Sets, Reps: wo.Reps})
|
utils.DoRedirect(w, r, "/auth/signin")
|
||||||
// }
|
return
|
||||||
//
|
}
|
||||||
// comp := account.AccountListComp(wos)
|
|
||||||
// handler.render.Render(r, w, comp)
|
var (
|
||||||
// }
|
account *types.Account
|
||||||
// }
|
err error
|
||||||
//
|
)
|
||||||
// func (handler AccountImpl) handleDeleteAccount() http.HandlerFunc {
|
idStr := r.PathValue("id")
|
||||||
// return func(w http.ResponseWriter, r *http.Request) {
|
name := r.FormValue("name")
|
||||||
// user := middleware.GetUser(r)
|
if idStr == "new" {
|
||||||
// if user == nil {
|
account, err = h.s.Add(user, name)
|
||||||
// utils.DoRedirect(w, r, "/auth/signin")
|
if err != nil {
|
||||||
// return
|
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
|
||||||
// }
|
return
|
||||||
//
|
}
|
||||||
// rowId := r.PathValue("id")
|
} else {
|
||||||
// if rowId == "" {
|
id, err := uuid.Parse(idStr)
|
||||||
// utils.TriggerToast(w, r, "error", "Missing ID field", http.StatusBadRequest)
|
if err != nil {
|
||||||
// return
|
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", http.StatusBadRequest)
|
||||||
// }
|
return
|
||||||
//
|
}
|
||||||
// rowIdInt, err := strconv.Atoi(rowId)
|
account, err = h.s.Update(user, id, name)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// utils.TriggerToast(w, r, "error", "Invalid ID", http.StatusBadRequest)
|
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// err = handler.service.DeleteAccount(user, rowIdInt)
|
|
||||||
// if err != nil {
|
comp := t.AccountItem(account)
|
||||||
// utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
h.r.Render(r, w, comp)
|
||||||
// return
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// }
|
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 != nil {
|
||||||
if err == service.ErrInvalidCredentials {
|
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 {
|
} else {
|
||||||
utils.TriggerToast(w, r, "error", "An error occurred", http.StatusInternalServerError)
|
utils.TriggerToastWithStatus(w, r, "error", "An error occurred", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -204,19 +204,19 @@ func (handler AuthImpl) handleSignUp() http.HandlerFunc {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, types.ErrInternal) {
|
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
|
return
|
||||||
} else if errors.Is(err, service.ErrInvalidEmail) {
|
} 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
|
return
|
||||||
} else if errors.Is(err, service.ErrInvalidPassword) {
|
} 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
|
return
|
||||||
}
|
}
|
||||||
// If err is "service.ErrAccountExists", then just continue
|
// 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)
|
err := handler.service.DeleteAccount(user, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == service.ErrInvalidCredentials {
|
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 {
|
} else {
|
||||||
utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -307,7 +307,7 @@ func (handler AuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
|||||||
session := middleware.GetSession(r)
|
session := middleware.GetSession(r)
|
||||||
user := middleware.GetUser(r)
|
user := middleware.GetUser(r)
|
||||||
if session == nil || user == nil {
|
if session == nil || user == nil {
|
||||||
utils.TriggerToast(w, r, "error", "Unathorized", http.StatusUnauthorized)
|
utils.TriggerToastWithStatus(w, r, "error", "Unathorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,11 +316,11 @@ func (handler AuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
|||||||
|
|
||||||
err := handler.service.ChangePassword(user, session.Id, currPass, newPass)
|
err := handler.service.ChangePassword(user, session.Id, currPass, newPass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.TriggerToast(w, r, "error", "Password not correct", http.StatusBadRequest)
|
utils.TriggerToastWithStatus(w, r, "error", "Password not correct", http.StatusBadRequest)
|
||||||
return
|
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")
|
email := r.FormValue("email")
|
||||||
if 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,9 +353,9 @@ func (handler AuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||||
} else {
|
} 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"))
|
pageUrl, err := url.Parse(r.Header.Get("HX-Current-URL"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Could not get current URL: %v", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,9 +374,9 @@ func (handler AuthImpl) handleForgotPasswordResponseComp() http.HandlerFunc {
|
|||||||
|
|
||||||
err = handler.service.ForgotPassword(token, newPass)
|
err = handler.service.ForgotPassword(token, newPass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.TriggerToast(w, r, "error", err.Error(), http.StatusBadRequest)
|
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusBadRequest)
|
||||||
} else {
|
} 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) {
|
if session == nil || csrfToken == "" || !auth.IsCsrfTokenValid(csrfToken, session.Id) {
|
||||||
log.Info("CSRF-Token not correct")
|
log.Info("CSRF-Token not correct")
|
||||||
if r.Header.Get("HX-Request") == "true" {
|
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 {
|
} else {
|
||||||
http.Error(w, "CSRF-Token not correct", http.StatusBadRequest)
|
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"
|
import "net/http"
|
||||||
|
|
||||||
|
// Chain list of handlers together
|
||||||
func Wrapper(next http.Handler, handlers ...func(http.Handler) http.Handler) http.Handler {
|
func Wrapper(next http.Handler, handlers ...func(http.Handler) http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
lastHandler := next
|
lastHandler := next
|
||||||
for i := len(handlers) - 1; i >= 0; i-- {
|
for i := 0; i < len(handlers); i++ {
|
||||||
lastHandler = handlers[i](lastHandler)
|
lastHandler = handlers[i](lastHandler)
|
||||||
}
|
}
|
||||||
lastHandler.ServeHTTP(w, r)
|
lastHandler.ServeHTTP(w, r)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ func NewIndex(service service.Auth, render *Render) Index {
|
|||||||
|
|
||||||
func (handler IndexImpl) Handle(router *http.ServeMux) {
|
func (handler IndexImpl) Handle(router *http.ServeMux) {
|
||||||
router.Handle("/", handler.handleRootAnd404())
|
router.Handle("/", handler.handleRootAnd404())
|
||||||
|
router.Handle("/empty", handler.handleEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
|
func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
|
||||||
@@ -52,3 +53,9 @@ func (handler IndexImpl) handleRootAnd404() http.HandlerFunc {
|
|||||||
handler.render.RenderLayoutWithStatus(r, w, comp, user, status)
|
handler.render.RenderLayoutWithStatus(r, w, comp, user, status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (handler IndexImpl) handleEmpty() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Return nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
18
input.css
18
input.css
@@ -28,33 +28,25 @@ input:focus {
|
|||||||
/* Button */
|
/* Button */
|
||||||
.button {
|
.button {
|
||||||
transition: all 150ms linear;
|
transition: all 150ms linear;
|
||||||
border-radius: 0.5rem;
|
@apply cursor-pointer border-2 rounded-lg border-transparent;
|
||||||
@apply inline-block cursor-pointer bg-white;
|
|
||||||
}
|
|
||||||
|
|
||||||
.button:hover {
|
|
||||||
box-shadow: 3px 3px 3px var(--color-gray-200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.button-primary:hover,
|
.button-primary:hover,
|
||||||
.button-normal:hover {
|
.button-normal:hover {
|
||||||
transform: translate(-0.25rem, -0.25rem);
|
transform: translate(-0.25rem, -0.25rem);
|
||||||
|
box-shadow: 3px 3px 3px var(--color-gray-200);
|
||||||
}
|
}
|
||||||
|
|
||||||
.button-primary {
|
.button-primary {
|
||||||
border: 2px solid var(--color-gray-400);
|
@apply border-gray-400
|
||||||
}
|
}
|
||||||
|
|
||||||
.button-normal {
|
.button-normal {
|
||||||
border: 2px solid var(--color-gray-200);
|
@apply border-gray-200
|
||||||
}
|
|
||||||
|
|
||||||
.button-neglect {
|
|
||||||
border: 2px solid white;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.button-neglect:hover {
|
.button-neglect:hover {
|
||||||
border-color: var(--color-gray-200);
|
@apply border-gray-200;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Input */
|
/* Input */
|
||||||
|
|||||||
8
main.go
8
main.go
@@ -130,10 +130,12 @@ func createHandler(d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
|||||||
|
|
||||||
return middleware.Wrapper(
|
return middleware.Wrapper(
|
||||||
router,
|
router,
|
||||||
middleware.Log,
|
|
||||||
middleware.CacheControl,
|
|
||||||
middleware.SecurityHeaders(serverSettings),
|
middleware.SecurityHeaders(serverSettings),
|
||||||
middleware.Authenticate(authService),
|
middleware.CacheControl,
|
||||||
middleware.CrossSiteRequestForgery(authService),
|
middleware.CrossSiteRequestForgery(authService),
|
||||||
|
middleware.Authenticate(authService),
|
||||||
|
middleware.Log,
|
||||||
|
// Gzip last, as it compresses the body
|
||||||
|
middleware.Gzip,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,13 +12,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
safeInputRegex = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
|
safeInputRegex = regexp.MustCompile(`^[a-zA-Z0-9äöüß -]+$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
type Account interface {
|
type Account interface {
|
||||||
Add(user *types.User, name string) (*types.Account, error)
|
Add(user *types.User, name string) (*types.Account, error)
|
||||||
Update(user *types.User, id uuid.UUID, name string) (*types.Account, error)
|
Update(user *types.User, id uuid.UUID, name string) (*types.Account, error)
|
||||||
Get(user *types.User) ([]*types.Account, error)
|
Get(user *types.User, id uuid.UUID) (*types.Account, error)
|
||||||
|
GetAll(user *types.User) ([]*types.Account, error)
|
||||||
Delete(user *types.User, id uuid.UUID) error
|
Delete(user *types.User, id uuid.UUID) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +70,7 @@ func (service AccountImpl) Add(user *types.User, name string) (*types.Account, e
|
|||||||
UpdatedBy: nil,
|
UpdatedBy: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = service.db.Insert(account)
|
err = service.db.Insert(user.Id, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -103,7 +104,7 @@ func (service AccountImpl) Update(user *types.User, id uuid.UUID, name string) (
|
|||||||
account.UpdatedAt = ×tamp
|
account.UpdatedAt = ×tamp
|
||||||
account.UpdatedBy = &user.Id
|
account.UpdatedBy = &user.Id
|
||||||
|
|
||||||
err = service.db.Update(account)
|
err = service.db.Update(user.Id, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -111,13 +112,27 @@ func (service AccountImpl) Update(user *types.User, id uuid.UUID, name string) (
|
|||||||
return account, nil
|
return account, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AccountImpl) Get(user *types.User) ([]*types.Account, error) {
|
func (service AccountImpl) Get(user *types.User, id uuid.UUID) (*types.Account, error) {
|
||||||
|
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
accounts, err := service.db.GetAll(user.GroupId)
|
account, err := service.db.Get(user.Id, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, types.ErrInternal
|
||||||
|
}
|
||||||
|
|
||||||
|
return account, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service AccountImpl) GetAll(user *types.User) ([]*types.Account, error) {
|
||||||
|
|
||||||
|
if user == nil {
|
||||||
|
return nil, types.ErrInternal
|
||||||
|
}
|
||||||
|
|
||||||
|
accounts, err := service.db.GetAll(user.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
@@ -130,16 +145,16 @@ func (service AccountImpl) Delete(user *types.User, id uuid.UUID) error {
|
|||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
account, err := service.db.Get(user.GroupId, id)
|
account, err := service.db.Get(user.Id, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if account.GroupId != user.GroupId {
|
if account.GroupId != user.Id {
|
||||||
return types.ErrUnauthorized
|
return types.ErrUnauthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
err = service.db.Delete(account.Id)
|
err = service.db.Delete(user.Id, account.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
|
|
||||||
|
|
||||||
function getClass(type) {
|
function getClass(type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "error":
|
case "error":
|
||||||
|
|||||||
143
template/account/account.templ
Normal file
143
template/account/account.templ
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
package account
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "spend-sparrow/template/svg"
|
||||||
|
import "spend-sparrow/types"
|
||||||
|
|
||||||
|
templ Account(accounts []*types.Account) {
|
||||||
|
<div class="max-w-6xl mt-10 mx-auto">
|
||||||
|
<button
|
||||||
|
hx-get="/account/new"
|
||||||
|
hx-target="#account-items"
|
||||||
|
hx-swap="afterbegin"
|
||||||
|
class="ml-auto button button-primary px-2 flex-1 flex items-center gap-2 justify-center"
|
||||||
|
>
|
||||||
|
@svg.Plus()
|
||||||
|
<p class="">New Account</p>
|
||||||
|
</button>
|
||||||
|
<div id="account-items" class="my-6 flex flex-col items-center">
|
||||||
|
for _, account := range accounts {
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
@AccountItem(account)
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
templ EditAccount(account *types.Account) {
|
||||||
|
{{
|
||||||
|
var (
|
||||||
|
name string
|
||||||
|
id string
|
||||||
|
cancelUrl string
|
||||||
|
)
|
||||||
|
if account == nil {
|
||||||
|
name = ""
|
||||||
|
id = "new"
|
||||||
|
cancelUrl = "/empty"
|
||||||
|
} else {
|
||||||
|
name = account.Name
|
||||||
|
id = account.Id.String()
|
||||||
|
cancelUrl = "/account/" + id
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
<div id="account" class="border-1 border-gray-300 w-full my-4 p-4 bg-gray-50 rounded-lg">
|
||||||
|
<form
|
||||||
|
hx-post={ "/account/" + id }
|
||||||
|
hx-target="closest #account"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
class="text-xl flex justify-end gap-4 items-center"
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
autofocus
|
||||||
|
name="name"
|
||||||
|
type="text"
|
||||||
|
value={ name }
|
||||||
|
class="mr-auto bg-white input"
|
||||||
|
/>
|
||||||
|
<button type="submit" class="button button-neglect px-1 flex items-center gap-2">
|
||||||
|
@svg.Save()
|
||||||
|
<span>
|
||||||
|
Save
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
hx-get={ cancelUrl }
|
||||||
|
hx-target="closest #account"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
class="button button-neglect px-1 flex items-center gap-2"
|
||||||
|
>
|
||||||
|
@svg.Cancel()
|
||||||
|
<span>
|
||||||
|
Cancel
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
templ AccountItem(account *types.Account) {
|
||||||
|
<div id="account" class="border-1 border-gray-300 w-full my-4 p-4 bg-gray-50 rounded-lg">
|
||||||
|
<div class="text-xl flex justify-end gap-4">
|
||||||
|
<p class="mr-auto">{ account.Name }</p>
|
||||||
|
<p class="mr-20 text-green-700">{ displayBalance(account.CurrentBalance) }</p>
|
||||||
|
<button
|
||||||
|
hx-get={ "/account/" + account.Id.String() + "?edit=true" }
|
||||||
|
hx-target="closest #account"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
class="button button-neglect px-1 flex items-center gap-2"
|
||||||
|
>
|
||||||
|
@svg.Edit()
|
||||||
|
<span>
|
||||||
|
Edit
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
hx-delete={ "/account/" + account.Id.String() }
|
||||||
|
hx-target="closest #account"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
class="button button-neglect px-1 flex items-center gap-2"
|
||||||
|
>
|
||||||
|
@svg.Delete()
|
||||||
|
<span>
|
||||||
|
Delete
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayBalance(balance int64) string {
|
||||||
|
|
||||||
|
euros := float64(balance) / 100
|
||||||
|
return fmt.Sprintf("%.2f €", euros)
|
||||||
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package account
|
|
||||||
|
|
||||||
import "spend-sparrow/types"
|
|
||||||
|
|
||||||
templ AccountListComp(accounts []*types.Account) {
|
|
||||||
<main class="mx-2"></main>
|
|
||||||
}
|
|
||||||
@@ -1,68 +1,90 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
templ SignInOrUpComp(isSignIn bool) {
|
templ SignInOrUpComp(isSignIn bool) {
|
||||||
{{
|
{{
|
||||||
var postUrl string
|
var postUrl string
|
||||||
if isSignIn {
|
if isSignIn {
|
||||||
postUrl = "/api/auth/signin"
|
postUrl = "/api/auth/signin"
|
||||||
} else {
|
} else {
|
||||||
postUrl = "/api/auth/signup"
|
postUrl = "/api/auth/signup"
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
<form class="max-w-xl px-2 mx-auto flex flex-col gap-4 h-full justify-center" hx-target="#sign-in-or-up-error" hx-post={
|
<form
|
||||||
postUrl }>
|
class="max-w-xl px-2 mx-auto flex flex-col gap-4 h-full justify-center"
|
||||||
<h2 class="text-4xl mb-4">
|
hx-target="#sign-in-or-up-error"
|
||||||
if isSignIn {
|
hx-post={ postUrl }
|
||||||
Sign In
|
>
|
||||||
} else {
|
<h2 class="text-4xl mb-4">
|
||||||
Sign Up
|
if isSignIn {
|
||||||
}
|
Sign In
|
||||||
</h2>
|
} else {
|
||||||
<label class="input flex items-center gap-2">
|
Sign Up
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="h-5 w-5 opacity-70">
|
}
|
||||||
<path
|
</h2>
|
||||||
d="M2.5 3A1.5 1.5 0 0 0 1 4.5v.793c.026.009.051.02.076.032L7.674 8.51c.206.1.446.1.652 0l6.598-3.185A.755.755 0 0 1 15 5.293V4.5A1.5 1.5 0 0 0 13.5 3h-11Z">
|
<label class="input flex items-center gap-2">
|
||||||
</path>
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="h-5 w-5 opacity-70">
|
||||||
<path
|
<path
|
||||||
d="M15 6.954 8.978 9.86a2.25 2.25 0 0 1-1.956 0L1 6.954V11.5A1.5 1.5 0 0 0 2.5 13h11a1.5 1.5 0 0 0 1.5-1.5V6.954Z">
|
d="M2.5 3A1.5 1.5 0 0 0 1 4.5v.793c.026.009.051.02.076.032L7.674 8.51c.206.1.446.1.652 0l6.598-3.185A.755.755 0 0 1 15 5.293V4.5A1.5 1.5 0 0 0 13.5 3h-11Z"
|
||||||
</path>
|
></path>
|
||||||
</svg>
|
<path
|
||||||
<input type="text" class="grow" placeholder="Email" name="email" spellcheck="false" autocomplete="off"
|
d="M15 6.954 8.978 9.86a2.25 2.25 0 0 1-1.956 0L1 6.954V11.5A1.5 1.5 0 0 0 2.5 13h11a1.5 1.5 0 0 0 1.5-1.5V6.954Z"
|
||||||
autocorrect="off" autocapitalize="off" />
|
></path>
|
||||||
</label>
|
</svg>
|
||||||
<label class="input input-bordered flex items-center gap-2">
|
<input
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="h-5 w-5 opacity-70">
|
type="text"
|
||||||
<path fill-rule="evenodd"
|
class="grow"
|
||||||
d="M14 6a4 4 0 0 1-4.899 3.899l-1.955 1.955a.5.5 0 0 1-.353.146H5v1.5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1-.5-.5v-2.293a.5.5 0 0 1 .146-.353l3.955-3.955A4 4 0 1 1 14 6Zm-4-2a.75.75 0 0 0 0 1.5.5.5 0 0 1 .5.5.75.75 0 0 0 1.5 0 2 2 0 0 0-2-2Z"
|
placeholder="Email"
|
||||||
clip-rule="evenodd"></path>
|
name="email"
|
||||||
</svg>
|
spellcheck="false"
|
||||||
<input type="password" class="grow" placeholder="Password" name="password" spellcheck="false" autocomplete="off"
|
autocomplete="off"
|
||||||
autocorrect="off" autocapitalize="off" />
|
autocorrect="off"
|
||||||
</label>
|
autocapitalize="off"
|
||||||
<div class="flex justify-end items-center gap-3 h-14">
|
autofocus
|
||||||
if isSignIn {
|
/>
|
||||||
<a href="/auth/forgot-password" class="text-gray-500 text-sm px-1 button button-neglect">
|
</label>
|
||||||
Forgot
|
<label class="input input-bordered flex items-center gap-2">
|
||||||
Password?
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="h-5 w-5 opacity-70">
|
||||||
</a>
|
<path
|
||||||
<a href="/auth/signup" class="ml-auto text-gray-500 text-sm px-1 button button-neglect">
|
fill-rule="evenodd"
|
||||||
Don't have an account?
|
d="M14 6a4 4 0 0 1-4.899 3.899l-1.955 1.955a.5.5 0 0 1-.353.146H5v1.5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1-.5-.5v-2.293a.5.5 0 0 1 .146-.353l3.955-3.955A4 4 0 1 1 14 6Zm-4-2a.75.75 0 0 0 0 1.5.5.5 0 0 1 .5.5.75.75 0 0 0 1.5 0 2 2 0 0 0-2-2Z"
|
||||||
Sign Up
|
clip-rule="evenodd"
|
||||||
</a>
|
></path>
|
||||||
<button class="button button-primary font-pirata text-gray-600 text-2xl px-1">Sign In</button>
|
</svg>
|
||||||
} else {
|
<input
|
||||||
<a href="/auth/signin" class="text-gray-500 text-sm px-1 button button-neglect">Already have an account? Sign In</a>
|
type="password"
|
||||||
<button class="button button-primary font-pirata text-gray-600 text-2xl px-1">
|
class="grow"
|
||||||
Sign Up
|
placeholder="Password"
|
||||||
</button>
|
name="password"
|
||||||
}
|
spellcheck="false"
|
||||||
</div>
|
autocomplete="off"
|
||||||
@Error("")
|
autocorrect="off"
|
||||||
</form>
|
autocapitalize="off"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<div class="flex justify-end items-center gap-3 h-14">
|
||||||
|
if isSignIn {
|
||||||
|
<a href="/auth/forgot-password" class="text-gray-500 text-sm px-1 button button-neglect">
|
||||||
|
Forgot
|
||||||
|
Password?
|
||||||
|
</a>
|
||||||
|
<a href="/auth/signup" class="ml-auto text-gray-500 text-sm px-1 button button-neglect">
|
||||||
|
Don't have an account?
|
||||||
|
Sign Up
|
||||||
|
</a>
|
||||||
|
<button class="button button-primary font-pirata text-gray-600 text-2xl px-1">Sign In</button>
|
||||||
|
} else {
|
||||||
|
<a href="/auth/signin" class="text-gray-500 text-sm px-1 button button-neglect">Already have an account? Sign In</a>
|
||||||
|
<button class="button button-primary font-pirata text-gray-600 text-2xl px-1">
|
||||||
|
Sign Up
|
||||||
|
</button>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
@Error("")
|
||||||
|
</form>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ Error(message string) {
|
templ Error(message string) {
|
||||||
<p class="text-error text-right" id="sign-in-or-up-error">
|
<p class="text-error text-right" id="sign-in-or-up-error">
|
||||||
{ message }
|
{ message }
|
||||||
</p>
|
</p>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,36 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
templ UserComp(user string) {
|
templ UserComp(user string) {
|
||||||
<div id="user-info" class="flex gap-5 items-center">
|
<div id="user-info" class="flex gap-5 items-center">
|
||||||
if user != "" {
|
if user != "" {
|
||||||
<div class="inline-block group relative">
|
<div class="inline-block group relative">
|
||||||
<button class="font-semibold py-2 px-4 inline-flex items-center">
|
<button class="font-semibold py-2 px-4 inline-flex items-center">
|
||||||
<span class="mr-1">{ user }</span>
|
<span class="mr-1">{ user }</span>
|
||||||
<!-- SVG is arrow down -->
|
<!-- SVG is arrow down -->
|
||||||
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
|
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
|
||||||
<path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"></path>
|
<path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"></path>
|
||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
<div class="absolute hidden group-has-hover:block w-full">
|
<div class="absolute hidden group-has-hover:block w-full">
|
||||||
<ul class="w-fit float-right mr-4 p-3 border-2 border-gray-200 rounded-lg">
|
<ul class="w-fit float-right mr-4 p-3 border-2 border-gray-200 rounded-lg bg-white shadow-lg">
|
||||||
<li class="mb-1">
|
<li class="mb-1">
|
||||||
<a class="button w-full px-1 button-neglect" hx-post="/api/auth/signout" hx-target="#user-info">Sign Out</a>
|
<a class="button w-full px-1 button-neglect block" hx-post="/api/auth/signout" hx-target="#user-info">Sign Out</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="mb-1">
|
<li class="mb-1">
|
||||||
<a class="button w-full px-1 button-neglect" href="/auth/change-password">Change Password</a>
|
<a class="button w-full px-1 button-neglect block" href="/auth/change-password">Change Password</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<a class="button w-full px-1 button-neglect text-gray-400 mt-4" href="/auth/delete-account" class="">
|
<a class="button w-full px-1 button-neglect text-gray-400 mt-4 block" href="/auth/delete-account" class="">
|
||||||
Delete
|
Delete
|
||||||
Account
|
Account
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
} else {
|
||||||
|
<a href="/auth/signup" class="font-pirata text-xl button px-1 button-neglect">Sign Up</a>
|
||||||
|
<a href="/auth/signin" class="font-pirata text-xl button px-1 button-neglect">Sign In</a>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
} else {
|
|
||||||
<a href="/auth/signup" class="font-pirata text-xl button px-1 button-neglect">Sign Up</a>
|
|
||||||
<a href="/auth/signin" class="font-pirata text-xl button px-1 button-neglect">Sign In</a>
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,25 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
templ VerifyComp() {
|
templ VerifyComp() {
|
||||||
<main class="h-full">
|
<main class="h-full">
|
||||||
<div class=" flex flex-col items-center justify-center h-full">
|
<div class=" flex flex-col items-center justify-center h-full">
|
||||||
<h2 class="text-6xl mb-10">
|
<h2 class="text-6xl mb-10">
|
||||||
Verify your email
|
Verify your email
|
||||||
</h2>
|
</h2>
|
||||||
<p class="text-lg text-center">
|
<p class="text-lg text-center">
|
||||||
We have sent you an email with a link to verify your account.
|
We have sent you an email with a link to verify your account.
|
||||||
</p>
|
</p>
|
||||||
<p class="text-lg text-center">
|
<p class="text-lg text-center">
|
||||||
Please check your inbox/spam and click on the link to verify your account.
|
Please check your inbox/spam and click on the link to verify your account.
|
||||||
</p>
|
</p>
|
||||||
<button class="mt-8 button button-normal px-2 text-gray-500 text-xl" hx-get="/api/auth/verify-resend"
|
<button
|
||||||
hx-sync="this:drop" hx-swap="outerHTML">
|
class="mt-8 button button-normal px-2 text-gray-500 text-xl"
|
||||||
resend verification email
|
hx-get="/api/auth/verify-resend"
|
||||||
</button>
|
hx-sync="this:drop"
|
||||||
</div>
|
hx-swap="outerHTML"
|
||||||
</main>
|
>
|
||||||
|
resend verification email
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package template
|
|
||||||
|
|
||||||
templ Index() {
|
|
||||||
<div class="h-full text-center flex flex-col items-center justify-center">
|
|
||||||
<h1 class="flex gap-2 w-full justify-center">
|
|
||||||
<img class="w-24" src="/static/favicon.svg" alt="SpendSparrow logo" />
|
|
||||||
<span class="text-8xl tracking-tighter font-bold font-pirata">SpendSparrow</span>
|
|
||||||
</h1>
|
|
||||||
<h2 class="text-3xl mt-8 text-gray-800">
|
|
||||||
Spend on the important<span class="text-sm">, like a fine Whiskey</span>
|
|
||||||
</h2>
|
|
||||||
<a href="/auth/signup" class="mt-24 button button-primary text-2xl p-4 font-bold">Getting Started</a>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
@@ -27,34 +27,35 @@ templ Layout(slot templ.Component, user templ.Component, loggedIn bool, path str
|
|||||||
<script src="/static/js/htmx.min.js"></script>
|
<script src="/static/js/htmx.min.js"></script>
|
||||||
<script src="/static/js/toast.js"></script>
|
<script src="/static/js/toast.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body hx-headers='{"csrf-token": "CSRF_TOKEN"}'>
|
<body class="h-screen flex flex-col" hx-headers='{"csrf-token": "CSRF_TOKEN"}'>
|
||||||
<div class="h-screen flex flex-col">
|
// Header
|
||||||
// Header
|
<div class="">
|
||||||
<div class="sticky top-0">
|
<nav class="flex bg-white items-center gap-2 py-1 px-2 h-12 md:gap-10 md:px-10 md:py-2">
|
||||||
<nav class="flex bg-white items-center gap-2 py-1 px-2 h-12 md:gap-10 md:px-10 md:py-2">
|
<a href="/" class="flex gap-2 mr-20">
|
||||||
<a href="/" class="flex gap-2 mr-20">
|
<img class="w-6" src="/static/favicon.svg" alt="SpendSparrow logo"/>
|
||||||
<img class="w-6" src="/static/favicon.svg" alt="SpendSparrow logo"/>
|
<span class="text-4xl font-bold font-pirata">SpendSparrow</span>
|
||||||
<span class="text-4xl font-bold font-pirata">SpendSparrow</span>
|
</a>
|
||||||
</a>
|
if loggedIn {
|
||||||
if loggedIn {
|
<a class={ layoutLinkClass(path == "/") } href="/">Dashboard</a>
|
||||||
<a class={ layoutLinkClass(path == "/") } href="/">Dashboard</a>
|
<a class={ layoutLinkClass(path == "/transaction") } href="/transaction">Transaction</a>
|
||||||
<a class={ layoutLinkClass(path == "/transaction") } href="/transaction">Transaction</a>
|
<a class={ layoutLinkClass(path == "/account") } href="/account">Account</a>
|
||||||
<a class={ layoutLinkClass(path == "/account") } href="/account">Account</a>
|
}
|
||||||
}
|
<div class="ml-auto">
|
||||||
<div class="ml-auto">
|
@user
|
||||||
@user
|
</div>
|
||||||
</div>
|
</nav>
|
||||||
</nav>
|
<div class="h-12 fixed top-12 mr-4 inset-0 bg-linear-0 from-transparent to-white"></div>
|
||||||
<div class="h-12 inset-0 bg-linear-0 from-transparent to-white"></div>
|
</div>
|
||||||
</div>
|
// Content
|
||||||
// Content
|
<div class="flex-1 overflow-auto">
|
||||||
<div class="flex-1">
|
<main class="h-full">
|
||||||
if slot != nil {
|
if slot != nil {
|
||||||
@slot
|
@slot
|
||||||
}
|
}
|
||||||
</div>
|
</main>
|
||||||
// Footer
|
|
||||||
</div>
|
</div>
|
||||||
|
// Footer
|
||||||
|
<!-- </div> -->
|
||||||
<div id="toasts" class="fixed bottom-4 right-4 ml-4 max-w-96 flex flex-col gap-2 z-50">
|
<div id="toasts" class="fixed bottom-4 right-4 ml-4 max-w-96 flex flex-col gap-2 z-50">
|
||||||
<div
|
<div
|
||||||
id="toast"
|
id="toast"
|
||||||
|
|||||||
@@ -3,21 +3,20 @@ package mail;
|
|||||||
import "net/url"
|
import "net/url"
|
||||||
|
|
||||||
templ Register(baseUrl string, token string) {
|
templ Register(baseUrl string, token string) {
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
<head>
|
||||||
<head>
|
<meta charset="UTF-8"/>
|
||||||
<meta charset="UTF-8" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<title>Welcome</title>
|
||||||
<title>Welcome</title>
|
</head>
|
||||||
</head>
|
<body>
|
||||||
|
<h4>Thank you for Sign Up!</h4>
|
||||||
<body>
|
<p>
|
||||||
<h4>Thank you for Sign Up!</h4>
|
Click <a href={ templ.URL(baseUrl + "/auth/verify-email?token=" + url.QueryEscape(token)) }>here</a> to finalize
|
||||||
<p>Click <a href={ templ.URL(baseUrl + "/auth/verify-email?token=" + url.QueryEscape(token)) }>here</a> to finalize
|
your registration.
|
||||||
your registration.</p>
|
</p>
|
||||||
<p>Kind regards</p>
|
<p>Kind regards</p>
|
||||||
</body>
|
</body>
|
||||||
|
</html>
|
||||||
</html>
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ templ NotFound() {
|
|||||||
<div class="p-16 rounded-lg">
|
<div class="p-16 rounded-lg">
|
||||||
<h1 class="text-4xl mb-5">Not Found</h1>
|
<h1 class="text-4xl mb-5">Not Found</h1>
|
||||||
<p class="text-lg mb-5">The page you are looking for does not exist.</p>
|
<p class="text-lg mb-5">The page you are looking for does not exist.</p>
|
||||||
<a href="/" class="">Go back to home</a>
|
<a href="/" class="button button-primary text-2xl py-2 px-4 mt-10">Go back to home</a>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
}
|
}
|
||||||
|
|||||||
15
template/root.templ
Normal file
15
template/root.templ
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package template
|
||||||
|
|
||||||
|
templ Index() {
|
||||||
|
<!-- <div class="h-full flex flex-col items-center justify-center"> -->
|
||||||
|
<div class="h-full flex flex-col items-center justify-center">
|
||||||
|
<h1 class="flex gap-2 w-full justify-center">
|
||||||
|
<img class="w-24" src="/static/favicon.svg" alt="SpendSparrow logo"/>
|
||||||
|
<span class="text-8xl tracking-tighter font-bold font-pirata">SpendSparrow</span>
|
||||||
|
</h1>
|
||||||
|
<h2 class="text-2xl mt-8 text-gray-800">
|
||||||
|
Spend your <span class="px-2 text-3xl text-yellow-800">treasure</span> on the important
|
||||||
|
</h2>
|
||||||
|
<a href="/auth/signup" class="mt-24 button button-primary text-2xl p-4 font-bold">Getting Started</a>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
37
template/svg/default.templ
Normal file
37
template/svg/default.templ
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package svg
|
||||||
|
|
||||||
|
templ Edit() {
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 384" class="h-4 w-4 text-gray-500">
|
||||||
|
<path fill="currentColor" d="M0 304L236 68l80 80L80 384H0v-80zM378 86l-39 39l-80-80l39-39q6-6 15-6t15 6l50 50q6 6 6 15t-6 15z"></path>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
|
||||||
|
templ Delete() {
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 304 384" class="h-4 w-4 text-gray-500">
|
||||||
|
<path fill="currentColor" d="M21 341V85h256v256q0 18-12.5 30.5T235 384H64q-18 0-30.5-12.5T21 341zM299 21v43H0V21h75L96 0h107l21 21h75z"></path>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
|
||||||
|
templ Eye() {
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 472 384" class="h-4 w-4 text-gray-500">
|
||||||
|
<path fill="currentColor" d="M235 32q79 0 142.5 44.5T469 192q-28 71-91.5 115.5T235 352T92 307.5T0 192q28-71 92-115.5T235 32zm0 267q44 0 75-31.5t31-75.5t-31-75.5T235 85t-75.5 31.5T128 192t31.5 75.5T235 299zm-.5-171q26.5 0 45.5 18.5t19 45.5t-19 45.5t-45.5 18.5t-45-18.5T171 192t18.5-45.5t45-18.5z"></path>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
|
||||||
|
templ Plus() {
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 304 384" class="h-4 w-4 text-gray-500">
|
||||||
|
<path fill="currentColor" d="M299 213H171v128h-43V213H0v-42h128V43h43v128h128v42z"></path>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
|
||||||
|
templ Save() {
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="h-4 w-4 text-gray-500">
|
||||||
|
<path fill="currentColor" d="M21 7v12q0 .825-.588 1.413T19 21H5q-.825 0-1.413-.588T3 19V5q0-.825.588-1.413T5 3h12l4 4Zm-9 11q1.25 0 2.125-.875T15 15q0-1.25-.875-2.125T12 12q-1.25 0-2.125.875T9 15q0 1.25.875 2.125T12 18Zm-6-8h9V6H6v4Z"></path>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
|
||||||
|
templ Cancel() {
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 1000" class="h-4 w-4 text-gray-500">
|
||||||
|
<path fill="currentColor" d="m654 501l346 346l-154 154l-346-346l-346 346L0 847l346-346L0 155L154 1l346 346L846 1l154 154z"></path>
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
@@ -1 +0,0 @@
|
|||||||
package workout
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
package workout
|
|
||||||
|
|
||||||
templ WorkoutComp(currentDate string) {
|
|
||||||
<main class="mx-2">
|
|
||||||
<form
|
|
||||||
class="max-w-xl mx-auto flex flex-col gap-4 justify-center mt-10"
|
|
||||||
hx-post="/api/workout"
|
|
||||||
hx-target="#workout-placeholder"
|
|
||||||
hx-swap="outerHTML"
|
|
||||||
>
|
|
||||||
<h2 class="text-4xl mb-8">Track your workout</h2>
|
|
||||||
<input id="date" type="date" class="" value={ currentDate } name="date"/>
|
|
||||||
<select class="w-full" name="type">
|
|
||||||
<option>Push Ups</option>
|
|
||||||
<option>Pull Ups</option>
|
|
||||||
</select>
|
|
||||||
<input type="number" class="" placeholder="Sets" name="sets"/>
|
|
||||||
<input type="number" class="" placeholder="Reps" name="reps"/>
|
|
||||||
<button class="self-end">Save</button>
|
|
||||||
</form>
|
|
||||||
<div hx-get="/api/workout" hx-trigger="load"></div>
|
|
||||||
</main>
|
|
||||||
}
|
|
||||||
|
|
||||||
type Workout struct {
|
|
||||||
Id string
|
|
||||||
Date string
|
|
||||||
Type string
|
|
||||||
Sets string
|
|
||||||
Reps string
|
|
||||||
}
|
|
||||||
|
|
||||||
templ WorkoutListComp(workouts []Workout) {
|
|
||||||
<div class="overflow-x-auto mx-auto max-w-lg">
|
|
||||||
<h2 class="text-4xl mt-14 mb-8">Workout history</h2>
|
|
||||||
<table class="table table-auto max-w-full">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Date</th>
|
|
||||||
<th>Type</th>
|
|
||||||
<th>Sets</th>
|
|
||||||
<th>Reps</th>
|
|
||||||
<th></th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<tr class="hidden" id="workout-placeholder"></tr>
|
|
||||||
for _,w := range workouts {
|
|
||||||
@WorkoutItemComp(w, false)
|
|
||||||
}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
templ WorkoutItemComp(w Workout, includePlaceholder bool) {
|
|
||||||
if includePlaceholder {
|
|
||||||
<tr class="hidden" id="workout-placeholder"></tr>
|
|
||||||
}
|
|
||||||
<tr>
|
|
||||||
<th>{ w.Date }</th>
|
|
||||||
<th>{ w.Type }</th>
|
|
||||||
<th>{ w.Sets }</th>
|
|
||||||
<th>{ w.Reps }</th>
|
|
||||||
<th>
|
|
||||||
<div class="tooltip" data-tip="Delete Entry">
|
|
||||||
<button hx-delete={ "api/workout/" + w.Id } hx-target="closest tr" type="submit">
|
|
||||||
Delete
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</th>
|
|
||||||
</tr>
|
|
||||||
}
|
|
||||||
@@ -38,21 +38,21 @@ type Transaction struct {
|
|||||||
// The Account holds money
|
// The Account holds money
|
||||||
type Account struct {
|
type Account struct {
|
||||||
Id uuid.UUID
|
Id uuid.UUID
|
||||||
GroupId uuid.UUID
|
GroupId uuid.UUID `db:"group_id"`
|
||||||
|
|
||||||
// Custom Name of the account, e.g. "Bank", "Cash", "Credit Card"
|
// Custom Name of the account, e.g. "Bank", "Cash", "Credit Card"
|
||||||
Name string
|
Name string
|
||||||
|
|
||||||
CurrentBalance int64
|
CurrentBalance int64 `db:"current_balance"`
|
||||||
LastTransaction *time.Time
|
LastTransaction *time.Time `db:"last_transaction"`
|
||||||
// The current precalculated value of:
|
// The current precalculated value of:
|
||||||
// Account.Balance - [PiggyBank.Balance...]
|
// Account.Balance - [PiggyBank.Balance...]
|
||||||
OinkBalance int64
|
OinkBalance int64 `db:"oink_balance"`
|
||||||
|
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time `db:"created_at"`
|
||||||
CreatedBy uuid.UUID
|
CreatedBy uuid.UUID `db:"created_by"`
|
||||||
UpdatedAt *time.Time
|
UpdatedAt *time.Time `db:"updated_at"`
|
||||||
UpdatedBy *uuid.UUID
|
UpdatedBy *uuid.UUID `db:"updated_by"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// The PiggyBank is a fictional account. The money it "holds" is actually in the Account
|
// The PiggyBank is a fictional account. The money it "holds" is actually in the Account
|
||||||
|
|||||||
@@ -3,20 +3,25 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TriggerToast(w http.ResponseWriter, r *http.Request, class string, message string, statusCode int) {
|
func TriggerToast(w http.ResponseWriter, r *http.Request, class string, message string) {
|
||||||
if isHtmx(r) {
|
if isHtmx(r) {
|
||||||
w.Header().Set("HX-Trigger", fmt.Sprintf(`{"toast": "%v|%v"}`, class, message))
|
w.Header().Set("HX-Trigger", fmt.Sprintf(`{"toast": "%v|%v"}`, class, strings.ReplaceAll(message, `"`, `\"`)))
|
||||||
w.WriteHeader(statusCode)
|
|
||||||
} else {
|
} else {
|
||||||
log.Error("Trying to trigger toast in non-HTMX request")
|
log.Error("Trying to trigger toast in non-HTMX request")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TriggerToastWithStatus(w http.ResponseWriter, r *http.Request, class string, message string, statusCode int) {
|
||||||
|
TriggerToast(w, r, class, message)
|
||||||
|
w.WriteHeader(statusCode)
|
||||||
|
}
|
||||||
|
|
||||||
func DoRedirect(w http.ResponseWriter, r *http.Request, url string) {
|
func DoRedirect(w http.ResponseWriter, r *http.Request, url string) {
|
||||||
if isHtmx(r) {
|
if isHtmx(r) {
|
||||||
w.Header().Add("HX-Redirect", url)
|
w.Header().Add("HX-Redirect", url)
|
||||||
|
|||||||
Reference in New Issue
Block a user