wip: recurring transactions
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 5m15s
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 5m15s
This commit is contained in:
@@ -7,8 +7,6 @@ import (
|
||||
t "spend-sparrow/template/transaction_recurring"
|
||||
"spend-sparrow/types"
|
||||
"spend-sparrow/utils"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
type TransactionRecurring interface {
|
||||
@@ -28,7 +26,7 @@ func NewTransactionRecurring(s service.TransactionRecurring, r *Render) Transact
|
||||
}
|
||||
|
||||
func (h TransactionRecurringImpl) Handle(r *http.ServeMux) {
|
||||
r.Handle("GET /transaction-recurring/{id}", h.handleTransactionRecurringItemComp())
|
||||
r.Handle("GET /transaction-recurring", h.handleTransactionRecurringItemComp())
|
||||
r.Handle("POST /transaction-recurring/{id}", h.handleUpdateTransactionRecurring())
|
||||
r.Handle("DELETE /transaction-recurring/{id}", h.handleDeleteTransactionRecurring())
|
||||
}
|
||||
@@ -41,29 +39,10 @@ func (h TransactionRecurringImpl) handleTransactionRecurringItemComp() http.Hand
|
||||
return
|
||||
}
|
||||
|
||||
id := r.PathValue("id")
|
||||
id := r.URL.Query().Get("id")
|
||||
accountId := r.URL.Query().Get("account-id")
|
||||
treasureChestId := r.URL.Query().Get("treasure-chest-id")
|
||||
|
||||
if id == "new" {
|
||||
comp := t.EditTransactionRecurring(nil, accountId, treasureChestId)
|
||||
h.r.Render(r, w, comp)
|
||||
return
|
||||
}
|
||||
|
||||
transaction, 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.EditTransactionRecurring(transaction, accountId, treasureChestId)
|
||||
} else {
|
||||
comp = t.TransactionRecurringItem(transaction)
|
||||
}
|
||||
h.r.Render(r, w, comp)
|
||||
h.renderItems(w, r, user, id, accountId, treasureChestId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,10 +54,6 @@ func (h TransactionRecurringImpl) handleUpdateTransactionRecurring() http.Handle
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
transaction *types.TransactionRecurring
|
||||
err error
|
||||
)
|
||||
input := types.TransactionRecurringInput{
|
||||
Id: r.PathValue("id"),
|
||||
IntervalMonths: r.FormValue("interval-months"),
|
||||
@@ -91,21 +66,20 @@ func (h TransactionRecurringImpl) handleUpdateTransactionRecurring() http.Handle
|
||||
}
|
||||
|
||||
if input.Id == "new" {
|
||||
transaction, err = h.s.Add(user, input)
|
||||
_, err := h.s.Add(user, input)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
transaction, err = h.s.Update(user, input)
|
||||
_, err := h.s.Update(user, input)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
comp := t.TransactionRecurringItem(transaction)
|
||||
h.r.Render(r, w, comp)
|
||||
h.renderItems(w, r, user, "", input.AccountId, input.TreasureChestId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,11 +92,40 @@ func (h TransactionRecurringImpl) handleDeleteTransactionRecurring() http.Handle
|
||||
}
|
||||
|
||||
id := r.PathValue("id")
|
||||
accountId := r.URL.Query().Get("account-id")
|
||||
treasureChestId := r.URL.Query().Get("treasure-chest-id")
|
||||
|
||||
err := h.s.Delete(user, id)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.renderItems(w, r, user, "", accountId, treasureChestId)
|
||||
}
|
||||
}
|
||||
|
||||
func (h TransactionRecurringImpl) renderItems(w http.ResponseWriter, r *http.Request, user *types.User, id, accountId, treasureChestId string) {
|
||||
|
||||
var transactionsRecurring []*types.TransactionRecurring
|
||||
var err error
|
||||
if accountId == "" && treasureChestId == "" {
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Please select an account or treasure chest", http.StatusBadRequest)
|
||||
}
|
||||
if accountId != "" {
|
||||
transactionsRecurring, err = h.s.GetAllByAccount(user, accountId)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
transactionsRecurring, err = h.s.GetAllByTreasureChest(user, treasureChestId)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
comp := t.TransactionRecurringItems(transactionsRecurring, id, accountId, treasureChestId)
|
||||
h.r.Render(r, w, comp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user