feat(transaction): #80 add errors to transactions
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 5m15s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 5m29s

This commit was merged in pull request #84.
This commit is contained in:
2025-05-16 17:09:13 +02:00
parent af9b785985
commit 3dc9f8ec6f
6 changed files with 254 additions and 149 deletions

View File

@@ -3,12 +3,14 @@ package handler
import (
"net/http"
"spend-sparrow/handler/middleware"
"spend-sparrow/log"
"spend-sparrow/service"
t "spend-sparrow/template/transaction"
"spend-sparrow/types"
"spend-sparrow/utils"
"github.com/a-h/templ"
"github.com/google/uuid"
)
type Transaction interface {
@@ -85,7 +87,9 @@ func (h TransactionImpl) handleTransactionPage() http.HandlerFunc {
return
}
comp := t.Transaction(transactions, accounts, treasureChests)
accountMap, treasureChestMap := h.getTransactionData(accounts, treasureChests)
comp := t.Transaction(transactions, accountMap, treasureChestMap)
h.r.RenderLayout(r, w, comp, user)
}
}
@@ -127,7 +131,8 @@ func (h TransactionImpl) handleTransactionItemComp() http.HandlerFunc {
if r.URL.Query().Get("edit") == "true" {
comp = t.EditTransaction(transaction, accounts, treasureChests)
} else {
comp = t.TransactionItem(transaction)
accountMap, treasureChestMap := h.getTransactionData(accounts, treasureChests)
comp = t.TransactionItem(transaction, accountMap, treasureChestMap)
}
h.r.Render(r, w, comp)
}
@@ -169,7 +174,20 @@ func (h TransactionImpl) handleUpdateTransaction() http.HandlerFunc {
}
}
comp := t.TransactionItem(transaction)
accounts, err := h.account.GetAll(user)
if err != nil {
handleError(w, r, err)
return
}
treasureChests, err := h.treasureChest.GetAll(user)
if err != nil {
handleError(w, r, err)
return
}
accountMap, treasureChestMap := h.getTransactionData(accounts, treasureChests)
comp := t.TransactionItem(transaction, accountMap, treasureChestMap)
h.r.Render(r, w, comp)
}
}
@@ -191,3 +209,22 @@ func (h TransactionImpl) handleDeleteTransaction() http.HandlerFunc {
}
}
}
func (h TransactionImpl) getTransactionData(accounts []*types.Account, treasureChests []*types.TreasureChest) (map[uuid.UUID]string, map[uuid.UUID]string) {
accountMap := make(map[uuid.UUID]string, 0)
for _, account := range accounts {
accountMap[account.Id] = account.Name
}
treasureChestMap := make(map[uuid.UUID]string, 0)
root := ""
for _, treasureChest := range treasureChests {
if treasureChest.ParentId == uuid.Nil {
root = treasureChest.Name + " > "
treasureChestMap[treasureChest.Id] = treasureChest.Name
} else {
treasureChestMap[treasureChest.Id] = root + treasureChest.Name
}
}
log.Info("treasureChestMap: %v", treasureChestMap)
return accountMap, treasureChestMap
}