package server import ( "fmt" "net/http" "time" "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type FilterTransactionsRequest struct { CategoryID string `json:"category_id"` PayeeID string `json:"payee_id"` AccountID string `json:"account_id"` FromDate time.Time `json:"from_date"` ToDate time.Time `json:"to_date"` } func (h *Handler) filteredTransactions(c *gin.Context) { budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } var request FilterTransactionsRequest err = c.BindJSON(&request) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } params := postgres.GetFilteredTransactionsParams{ BudgetID: budgetUUID, FromDate: request.FromDate, ToDate: request.ToDate, } params.CategoryID, params.FilterCategory = parseEmptyUUID(request.CategoryID) accountID, filterAccount := parseEmptyUUID(request.AccountID) params.AccountID, params.FilterAccount = accountID.UUID, filterAccount params.PayeeID, params.FilterPayee = parseEmptyUUID(request.PayeeID) transactions, err := h.Service.GetFilteredTransactions(c.Request.Context(), params) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, TransactionsResponse{nil, transactions}) } func parseEmptyUUID(value string) (uuid.NullUUID, bool) { if value == "" { return uuid.NullUUID{}, false } val, err := uuid.Parse(value) if err != nil { return uuid.NullUUID{}, false } return uuid.NullUUID{val, true}, true } func (h *Handler) problematicTransactions(c *gin.Context) { budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } transactions, err := h.Service.GetProblematicTransactions(c.Request.Context(), budgetUUID) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, TransactionsResponse{nil, transactions}) } func (h *Handler) transactionsForAccount(c *gin.Context) { accountID := c.Param("accountid") accountUUID, err := uuid.Parse(accountID) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } account, err := h.Service.GetAccount(c.Request.Context(), accountUUID) if err != nil { c.AbortWithError(http.StatusNotFound, err) return } transactions, err := h.Service.GetTransactionsForAccount(c.Request.Context(), accountUUID) if err != nil { c.AbortWithError(http.StatusNotFound, err) return } c.JSON(http.StatusOK, TransactionsResponse{&account, transactions}) } type TransactionsResponse struct { Account *postgres.Account Transactions []postgres.DisplayTransaction } type EditAccountRequest struct { Name string `json:"name"` OnBudget bool `json:"onBudget"` IsOpen bool `json:"isOpen"` } func (h *Handler) editAccount(c *gin.Context) { accountID := c.Param("accountid") accountUUID, err := uuid.Parse(accountID) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } var request EditAccountRequest err = c.BindJSON(&request) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } updateParams := postgres.UpdateAccountParams{ Name: request.Name, OnBudget: request.OnBudget, IsOpen: request.IsOpen, ID: accountUUID, } account, err := h.Service.UpdateAccount(c.Request.Context(), updateParams) if err != nil { c.AbortWithError(http.StatusNotFound, err) return } h.getBudget(c, account.BudgetID) }