61 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package server
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"net/http"
 | |
| 	"time"
 | |
| 
 | |
| 	"git.javil.eu/jacob1123/budgeteer/postgres"
 | |
| 	"github.com/gin-gonic/gin"
 | |
| 	"github.com/google/uuid"
 | |
| )
 | |
| 
 | |
| type NewTransactionPayload struct {
 | |
| 	Date  JSONDate `json:"date"`
 | |
| 	Payee struct {
 | |
| 		ID   uuid.NullUUID
 | |
| 		Name string
 | |
| 	} `json:"payee"`
 | |
| 	Category struct {
 | |
| 		ID   uuid.NullUUID
 | |
| 		Name string
 | |
| 	} `json:"category"`
 | |
| 	Memo      string    `json:"memo"`
 | |
| 	Amount    string    `json:"amount"`
 | |
| 	BudgetID  uuid.UUID `json:"budgetId"`
 | |
| 	AccountID uuid.UUID `json:"accountId"`
 | |
| 	State     string    `json:"state"`
 | |
| }
 | |
| 
 | |
| func (h *Handler) newTransaction(c *gin.Context) {
 | |
| 	var payload NewTransactionPayload
 | |
| 	err := c.BindJSON(&payload)
 | |
| 	if err != nil {
 | |
| 		c.AbortWithError(http.StatusBadRequest, err)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	fmt.Printf("%v\n", payload)
 | |
| 
 | |
| 	amount := postgres.Numeric{}
 | |
| 	err = amount.Set(payload.Amount)
 | |
| 	if err != nil {
 | |
| 		c.AbortWithError(http.StatusBadRequest, fmt.Errorf("amount: %w", err))
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	newTransaction := postgres.CreateTransactionParams{
 | |
| 		Memo:       payload.Memo,
 | |
| 		Date:       time.Time(payload.Date),
 | |
| 		Amount:     amount,
 | |
| 		AccountID:  payload.AccountID,
 | |
| 		PayeeID:    payload.Payee.ID,
 | |
| 		CategoryID: payload.Category.ID,
 | |
| 		Status:     postgres.TransactionStatus(payload.State),
 | |
| 	}
 | |
| 	_, err = h.Service.CreateTransaction(c.Request.Context(), newTransaction)
 | |
| 	if err != nil {
 | |
| 		c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
 | |
| 	}
 | |
| }
 |