Use JSON params instead of PostForm

This commit is contained in:
Jan Bader 2022-02-04 21:43:35 +00:00
parent d5904a7c4a
commit 3d1d1308ac

View File

@ -59,48 +59,16 @@ func (h *Handler) newTransaction(c *gin.Context) {
} }
fmt.Printf("%v\n", payload) fmt.Printf("%v\n", payload)
/* c.JSON(http.StatusOK, payload)*/
return return
transactionMemo, _ := c.GetPostForm("memo")
transactionAccountID, err := getUUID(c, "account_id") transactionAccountID, err := getUUID(c, "account_id")
if err != nil { if err != nil {
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id: %w", err)) c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id: %w", err))
return 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 := postgres.Numeric{}
amount.Set(transactionAmount) amount.Set(payload.Amount)
transactionUUID, err := getNullUUIDFromParam(c, "transactionid") transactionUUID, err := getNullUUIDFromParam(c, "transactionid")
if err != nil { if err != nil {
@ -110,12 +78,12 @@ func (h *Handler) newTransaction(c *gin.Context) {
if !transactionUUID.Valid { if !transactionUUID.Valid {
new := postgres.CreateTransactionParams{ new := postgres.CreateTransactionParams{
Memo: transactionMemo, Memo: payload.Memo,
Date: transactionDateValue, Date: time.Time(payload.Date),
Amount: amount, Amount: amount,
AccountID: transactionAccountID, AccountID: transactionAccountID,
PayeeID: transactionPayeeID, PayeeID: payload.Payee.ID, //TODO handle new payee
CategoryID: transactionCategoryID, CategoryID: payload.Category.ID, //TODO handle new category
} }
_, err = h.Service.CreateTransaction(c.Request.Context(), new) _, err = h.Service.CreateTransaction(c.Request.Context(), new)
if err != nil { if err != nil {
@ -136,12 +104,12 @@ func (h *Handler) newTransaction(c *gin.Context) {
update := postgres.UpdateTransactionParams{ update := postgres.UpdateTransactionParams{
ID: transactionUUID.UUID, ID: transactionUUID.UUID,
Memo: transactionMemo, Memo: payload.Memo,
Date: transactionDateValue, Date: time.Time(payload.Date),
Amount: amount, Amount: amount,
AccountID: transactionAccountID, AccountID: transactionAccountID,
PayeeID: transactionPayeeID, PayeeID: payload.Payee.ID, //TODO handle new payee
CategoryID: transactionCategoryID, CategoryID: payload.Category.ID, //TODO handle new category
} }
err = h.Service.UpdateTransaction(c.Request.Context(), update) err = h.Service.UpdateTransaction(c.Request.Context(), update)
if err != nil { if err != nil {