All checks were successful
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 1m16s
135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package transaction_recurring
|
|
|
|
import (
|
|
"net/http"
|
|
"spend-sparrow/internal/auth_types"
|
|
"spend-sparrow/internal/core"
|
|
"spend-sparrow/internal/utils"
|
|
)
|
|
|
|
type Handler interface {
|
|
Handle(router *http.ServeMux)
|
|
}
|
|
|
|
type HandlerImpl struct {
|
|
s Service
|
|
r *core.Render
|
|
}
|
|
|
|
func NewHandler(s Service, r *core.Render) Handler {
|
|
return HandlerImpl{
|
|
s: s,
|
|
r: r,
|
|
}
|
|
}
|
|
|
|
func (h HandlerImpl) 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 HandlerImpl) handleTransactionRecurringItemComp() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
core.UpdateSpan(r)
|
|
|
|
user := core.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 HandlerImpl) handleUpdateTransactionRecurring() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
core.UpdateSpan(r)
|
|
|
|
user := core.GetUser(r)
|
|
if user == nil {
|
|
utils.DoRedirect(w, r, "/auth/signin")
|
|
return
|
|
}
|
|
|
|
input := 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(r.Context(), user, input)
|
|
if err != nil {
|
|
core.HandleError(w, r, err)
|
|
return
|
|
}
|
|
} else {
|
|
_, err := h.s.Update(r.Context(), user, input)
|
|
if err != nil {
|
|
core.HandleError(w, r, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
h.renderItems(w, r, user, "", input.AccountId, input.TreasureChestId)
|
|
}
|
|
}
|
|
|
|
func (h HandlerImpl) handleDeleteTransactionRecurring() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
core.UpdateSpan(r)
|
|
|
|
user := core.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(r.Context(), user, id)
|
|
if err != nil {
|
|
core.HandleError(w, r, err)
|
|
return
|
|
}
|
|
|
|
h.renderItems(w, r, user, "", accountId, treasureChestId)
|
|
}
|
|
}
|
|
|
|
func (h HandlerImpl) renderItems(w http.ResponseWriter, r *http.Request, user *auth_types.User, id, accountId, treasureChestId string) {
|
|
var transactionsRecurring []*TransactionRecurring
|
|
var err error
|
|
if accountId == "" && treasureChestId == "" {
|
|
utils.TriggerToastWithStatus(r.Context(), w, r, "error", "Please select an account or treasure chest", http.StatusBadRequest)
|
|
}
|
|
if accountId != "" {
|
|
transactionsRecurring, err = h.s.GetAllByAccount(r.Context(), user, accountId)
|
|
if err != nil {
|
|
core.HandleError(w, r, err)
|
|
return
|
|
}
|
|
} else {
|
|
transactionsRecurring, err = h.s.GetAllByTreasureChest(r.Context(), user, treasureChestId)
|
|
if err != nil {
|
|
core.HandleError(w, r, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
comp := TransactionRecurringItems(transactionsRecurring, id, accountId, treasureChestId)
|
|
h.r.Render(r, w, comp)
|
|
}
|