55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package http
|
|
|
|
import (
|
|
"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, succ := c.GetPostForm("memo")
|
|
if !succ {
|
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
|
return
|
|
}
|
|
|
|
transactionAccount, succ := c.GetPostForm("account_id")
|
|
if !succ {
|
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
|
return
|
|
}
|
|
|
|
transactionAccountID, err := uuid.Parse(transactionAccount)
|
|
if !succ {
|
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
|
return
|
|
}
|
|
|
|
transactionDate, succ := c.GetPostForm("date")
|
|
if !succ {
|
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
|
return
|
|
}
|
|
|
|
transactionDateValue, err := time.Parse("2006-01-02", transactionDate)
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
|
return
|
|
}
|
|
|
|
new := postgres.CreateTransactionParams{
|
|
Memo: transactionMemo,
|
|
Date: transactionDateValue,
|
|
Amount: postgres.Numeric{},
|
|
AccountID: transactionAccountID,
|
|
}
|
|
_, err = h.Service.CreateTransaction(c.Request.Context(), new)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
}
|