budgeteer/http/transaction.go
Jan Bader a8bd03a805 Handle circular required keys
Use a dummy-value at first and update it later.
Deferrable doesn't seem to work for NOT NULL - only
for FOREIGN KEYs.
2021-12-14 14:13:23 +00:00

61 lines
1.5 KiB
Go

package http
import (
"fmt"
"net/http"
"time"
"git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func (h *Handler) newTransaction(c *gin.Context) {
transactionMemo, _ := c.GetPostForm("memo")
transactionAccount, succ := c.GetPostForm("account_id")
if !succ {
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id missing"))
return
}
transactionAccountID, err := uuid.Parse(transactionAccount)
if !succ {
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id is not a valid uuid"))
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)
new := postgres.CreateTransactionParams{
Memo: transactionMemo,
Date: transactionDateValue,
Amount: amount,
AccountID: transactionAccountID,
PayeeID: uuid.NullUUID{},
CategoryID: uuid.NullUUID{},
}
_, err = h.Service.CreateTransaction(c.Request.Context(), new)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
return
}
}