Continue migration to echo
This commit is contained in:
@ -8,7 +8,6 @@ import (
|
||||
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
@ -30,40 +29,35 @@ type NewTransactionPayload struct {
|
||||
|
||||
func (h *Handler) updateTransaction(c echo.Context) error {
|
||||
var payload NewTransactionPayload
|
||||
err := c.BindJSON(&payload)
|
||||
err := c.Bind(&payload)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
amount, err := numeric.Parse(payload.Amount)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("amount: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
transactionID := c.Param("transactionid")
|
||||
transactionUUID, err := uuid.Parse(transactionID)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"transactionid missing from URL"})
|
||||
return
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "transactionid missing from URL")
|
||||
}
|
||||
|
||||
h.UpdateTransaction(payload, amount, transactionUUID, c)
|
||||
return h.UpdateTransaction(payload, amount, transactionUUID, c)
|
||||
}
|
||||
|
||||
func (h *Handler) newTransaction(c echo.Context) error {
|
||||
var payload NewTransactionPayload
|
||||
err := c.BindJSON(&payload)
|
||||
err := c.Bind(&payload)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
amount, err := numeric.Parse(payload.Amount)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("amount: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
newTransaction := postgres.CreateTransactionParams{
|
||||
@ -79,7 +73,6 @@ func (h *Handler) newTransaction(c echo.Context) error {
|
||||
groupID, err := h.CreateTransferForOtherAccount(newTransaction, amount, payload, c)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
newTransaction.GroupID = groupID
|
||||
@ -94,16 +87,14 @@ func (h *Handler) newTransaction(c echo.Context) error {
|
||||
transactionUUID, err := h.Service.CreateTransaction(c.Request().Context(), newTransaction)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
transaction, err := h.Service.GetTransaction(c.Request().Context(), transactionUUID)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("get transaction: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, transaction)
|
||||
return c.JSON(http.StatusOK, transaction)
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateTransaction(payload NewTransactionPayload, amount numeric.Numeric, transactionUUID uuid.UUID, c echo.Context) error {
|
||||
@ -112,7 +103,7 @@ func (h *Handler) UpdateTransaction(payload NewTransactionPayload, amount numeri
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("delete transaction: %w", err))
|
||||
}
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
editTransaction := postgres.UpdateTransactionParams{
|
||||
@ -127,19 +118,17 @@ func (h *Handler) UpdateTransaction(payload NewTransactionPayload, amount numeri
|
||||
err := h.Service.UpdateTransaction(c.Request().Context(), editTransaction)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("edit transaction: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
transaction, err := h.Service.GetTransaction(c.Request().Context(), transactionUUID)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("get transaction: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, transaction)
|
||||
return c.JSON(http.StatusOK, transaction)
|
||||
}
|
||||
|
||||
func (h *Handler) CreateTransferForOtherAccount(newTransaction postgres.CreateTransactionParams, amount numeric.Numeric, payload NewTransactionPayload, c *gin.Context) (uuid.NullUUID, error) {
|
||||
func (h *Handler) CreateTransferForOtherAccount(newTransaction postgres.CreateTransactionParams, amount numeric.Numeric, payload NewTransactionPayload, c echo.Context) (uuid.NullUUID, error) {
|
||||
newTransaction.GroupID = uuid.NullUUID{UUID: uuid.New(), Valid: true}
|
||||
newTransaction.Amount = amount.Neg()
|
||||
newTransaction.AccountID = payload.Payee.ID.UUID
|
||||
|
Reference in New Issue
Block a user