131 lines
3.6 KiB
Go
131 lines
3.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"spend-sparrow/internal/handler/middleware"
|
|
"spend-sparrow/internal/service"
|
|
t "spend-sparrow/internal/template/transaction_recurring"
|
|
"spend-sparrow/internal/types"
|
|
"spend-sparrow/internal/utils"
|
|
)
|
|
|
|
type TransactionRecurring interface {
|
|
Handle(router *http.ServeMux)
|
|
}
|
|
|
|
type TransactionRecurringImpl struct {
|
|
s service.TransactionRecurring
|
|
r *Render
|
|
}
|
|
|
|
func NewTransactionRecurring(s service.TransactionRecurring, r *Render) TransactionRecurring {
|
|
return TransactionRecurringImpl{
|
|
s: s,
|
|
r: r,
|
|
}
|
|
}
|
|
|
|
func (h TransactionRecurringImpl) Handle(r *http.ServeMux) {
|
|
r.Handle("GET /transaction-recurring", h.handleTransactionRecurringItemComp())
|
|
r.Handle("POST /transaction-recurring/{id}", h.handleUpdateTransactionRecurring())
|
|
r.Handle("DELETE /transaction-recurring/{id}", h.handleDeleteTransactionRecurring())
|
|
}
|
|
|
|
func (h TransactionRecurringImpl) handleTransactionRecurringItemComp() 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.URL.Query().Get("id")
|
|
accountId := r.URL.Query().Get("account-id")
|
|
treasureChestId := r.URL.Query().Get("treasure-chest-id")
|
|
h.renderItems(w, r, user, id, accountId, treasureChestId)
|
|
}
|
|
}
|
|
|
|
func (h TransactionRecurringImpl) handleUpdateTransactionRecurring() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
user := middleware.GetUser(r)
|
|
if user == nil {
|
|
utils.DoRedirect(w, r, "/auth/signin")
|
|
return
|
|
}
|
|
|
|
input := types.TransactionRecurringInput{
|
|
Id: r.PathValue("id"),
|
|
IntervalMonths: r.FormValue("interval-months"),
|
|
NextExecution: r.FormValue("next-execution"),
|
|
Party: r.FormValue("party"),
|
|
Description: r.FormValue("description"),
|
|
AccountId: r.FormValue("account-id"),
|
|
TreasureChestId: r.FormValue("treasure-chest-id"),
|
|
Value: r.FormValue("value"),
|
|
}
|
|
|
|
if input.Id == "new" {
|
|
_, err := h.s.Add(user, input)
|
|
if err != nil {
|
|
handleError(w, r, err)
|
|
return
|
|
}
|
|
} else {
|
|
_, err := h.s.Update(user, input)
|
|
if err != nil {
|
|
handleError(w, r, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
h.renderItems(w, r, user, "", input.AccountId, input.TreasureChestId)
|
|
}
|
|
}
|
|
|
|
func (h TransactionRecurringImpl) handleDeleteTransactionRecurring() 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")
|
|
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)
|
|
}
|