From 3d1d1308ac05622879c31e37cea10228eae6fb9d Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 4 Feb 2022 21:43:35 +0000 Subject: [PATCH] Use JSON params instead of PostForm --- http/transaction.go | 50 ++++++++------------------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/http/transaction.go b/http/transaction.go index 12b2553..41cf67c 100644 --- a/http/transaction.go +++ b/http/transaction.go @@ -59,48 +59,16 @@ func (h *Handler) newTransaction(c *gin.Context) { } fmt.Printf("%v\n", payload) - /* c.JSON(http.StatusOK, payload)*/ return - transactionMemo, _ := c.GetPostForm("memo") transactionAccountID, err := getUUID(c, "account_id") if err != nil { c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id: %w", err)) return } - transactionCategoryID, err := getNullUUIDFromForm(c, "category_id") - if err != nil { - c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("category_id: %w", err)) - return - } - - transactionPayeeID, err := getNullUUIDFromForm(c, "payee_id") - if err != nil { - c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("payee_id: %w", err)) - return - } - - transactionDate, succ := c.GetPostForm("date") - if !succ { - c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("date missing")) - return - } - - transactionDateValue, err := time.Parse("2006-01-02", transactionDate) - if err != nil { - c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("date is not a valid date")) - return - } - - transactionAmount, succ := c.GetPostForm("amount") - if !succ { - c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("amount missing")) - return - } - amount := postgres.Numeric{} - amount.Set(transactionAmount) + amount.Set(payload.Amount) transactionUUID, err := getNullUUIDFromParam(c, "transactionid") if err != nil { @@ -110,12 +78,12 @@ func (h *Handler) newTransaction(c *gin.Context) { if !transactionUUID.Valid { new := postgres.CreateTransactionParams{ - Memo: transactionMemo, - Date: transactionDateValue, + Memo: payload.Memo, + Date: time.Time(payload.Date), Amount: amount, AccountID: transactionAccountID, - PayeeID: transactionPayeeID, - CategoryID: transactionCategoryID, + PayeeID: payload.Payee.ID, //TODO handle new payee + CategoryID: payload.Category.ID, //TODO handle new category } _, err = h.Service.CreateTransaction(c.Request.Context(), new) if err != nil { @@ -136,12 +104,12 @@ func (h *Handler) newTransaction(c *gin.Context) { update := postgres.UpdateTransactionParams{ ID: transactionUUID.UUID, - Memo: transactionMemo, - Date: transactionDateValue, + Memo: payload.Memo, + Date: time.Time(payload.Date), Amount: amount, AccountID: transactionAccountID, - PayeeID: transactionPayeeID, - CategoryID: transactionCategoryID, + PayeeID: payload.Payee.ID, //TODO handle new payee + CategoryID: payload.Category.ID, //TODO handle new category } err = h.Service.UpdateTransaction(c.Request.Context(), update) if err != nil {