56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
|
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type SetCategoryAssignmentRequest struct {
|
|
Assigned string
|
|
}
|
|
|
|
func (h *Handler) setCategoryAssignment(c *gin.Context) {
|
|
categoryID := c.Param("categoryid")
|
|
categoryUUID, err := uuid.Parse(categoryID)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"categoryid missing from URL"})
|
|
return
|
|
}
|
|
|
|
var request SetCategoryAssignmentRequest
|
|
err = c.BindJSON(&request)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid payload: %w", err))
|
|
return
|
|
}
|
|
|
|
date, err := getDate(c)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("date invalid: %w", err))
|
|
return
|
|
}
|
|
|
|
var amount numeric.Numeric
|
|
err = amount.Set(request.Assigned)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("parse amount: %w", err))
|
|
return
|
|
}
|
|
|
|
updateArgs := postgres.UpdateAssignmentParams{
|
|
CategoryID: categoryUUID,
|
|
Date: date.FirstOfMonth(),
|
|
Amount: amount,
|
|
}
|
|
err = h.Service.UpdateAssignment(c.Request.Context(), updateArgs)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update assignment: %w", err))
|
|
return
|
|
}
|
|
}
|