Split new and update-transaction into separate methods
This commit is contained in:
parent
967f1c71e5
commit
eb2b31e622
@ -52,6 +52,7 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
anonymous.POST("/register", h.registerPost)
|
||||
|
||||
authenticated := api.Group("")
|
||||
{
|
||||
authenticated.Use(h.verifyLoginWithForbidden)
|
||||
authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount)
|
||||
authenticated.POST("/account/:accountid/reconcile", h.reconcileTransactions)
|
||||
@ -60,7 +61,7 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
|
||||
budget := authenticated.Group("/budget")
|
||||
budget.POST("/new", h.newBudget)
|
||||
budget.GET("/:budgetid", h.budgeting)
|
||||
budget.GET("/:budgetid", h.budget)
|
||||
budget.GET("/:budgetid/:year/:month", h.budgetingForMonth)
|
||||
budget.POST("/:budgetid/category/:categoryid/:year/:month", h.setCategoryAssignment)
|
||||
budget.GET("/:budgetid/autocomplete/payees", h.autocompletePayee)
|
||||
@ -73,7 +74,8 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
|
||||
transaction := authenticated.Group("/transaction")
|
||||
transaction.POST("/new", h.newTransaction)
|
||||
transaction.POST("/:transactionid", h.newTransaction)
|
||||
transaction.POST("/:transactionid", h.updateTransaction)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeStatic(c *gin.Context) {
|
||||
|
@ -27,7 +27,7 @@ type NewTransactionPayload struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
func (h *Handler) newTransaction(c *gin.Context) {
|
||||
func (h *Handler) updateTransaction(c *gin.Context) {
|
||||
var payload NewTransactionPayload
|
||||
err := c.BindJSON(&payload)
|
||||
if err != nil {
|
||||
@ -42,8 +42,26 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
||||
}
|
||||
|
||||
transactionID := c.Param("transactionid")
|
||||
if transactionID != "" {
|
||||
h.UpdateTransaction(payload, amount, transactionID, c)
|
||||
transactionUUID, err := uuid.Parse(transactionID)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"transactionid missing from URL"})
|
||||
return
|
||||
}
|
||||
|
||||
h.UpdateTransaction(payload, amount, transactionUUID, c)
|
||||
}
|
||||
|
||||
func (h *Handler) newTransaction(c *gin.Context) {
|
||||
var payload NewTransactionPayload
|
||||
err := c.BindJSON(&payload)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
amount, err := numeric.Parse(payload.Amount)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("amount: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
@ -87,8 +105,7 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, transaction)
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateTransaction(payload NewTransactionPayload, amount numeric.Numeric, transactionID string, c *gin.Context) {
|
||||
transactionUUID := uuid.MustParse(transactionID)
|
||||
func (h *Handler) UpdateTransaction(payload NewTransactionPayload, amount numeric.Numeric, transactionUUID uuid.UUID, c *gin.Context) {
|
||||
if amount.IsZero() {
|
||||
err := h.Service.DeleteTransaction(c.Request.Context(), transactionUUID)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user