53 lines
1.3 KiB
Go
53 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/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type SetCategoryAssignmentRequest struct {
|
|
Assigned float64
|
|
}
|
|
|
|
func (h *Handler) setCategoryAssignment(c echo.Context) error {
|
|
categoryID := c.Param("categoryid")
|
|
categoryUUID, err := uuid.Parse(categoryID)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "categoryid missing from URL")
|
|
}
|
|
|
|
var request SetCategoryAssignmentRequest
|
|
err = c.Bind(&request)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid payload: %w", err))
|
|
}
|
|
|
|
date, err := getDate(c)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("date invalid: %w", err))
|
|
}
|
|
|
|
var amount numeric.Numeric
|
|
err = amount.Set(request.Assigned)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("parse amount: %w", err))
|
|
}
|
|
|
|
updateArgs := postgres.UpdateAssignmentParams{
|
|
CategoryID: categoryUUID,
|
|
Date: date.FirstOfMonth(),
|
|
Amount: amount,
|
|
}
|
|
err = h.Service.UpdateAssignment(c.Request().Context(), updateArgs)
|
|
if err != nil {
|
|
return fmt.Errorf("update assignment: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|