49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type newCategoryInformation struct {
|
|
BudgetID uuid.UUID `json:"budgetId"`
|
|
Name string `json:"name"`
|
|
Group string `json:"group"`
|
|
}
|
|
|
|
func (h *Handler) newCategory(c echo.Context) error {
|
|
var newCategory newCategoryInformation
|
|
if err := c.Bind(&newCategory); err != nil {
|
|
return echo.NewHTTPError(http.StatusNotAcceptable, err)
|
|
}
|
|
|
|
if newCategory.Name == "" {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "category name is required")
|
|
}
|
|
|
|
if newCategory.Group == "" {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "category group is required")
|
|
}
|
|
|
|
categoryGroup, err := h.Service.GetCategoryGroupByName(c.Request().Context(), postgres.GetCategoryGroupByNameParams{
|
|
BudgetID: newCategory.BudgetID,
|
|
Name: newCategory.Group,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
budget, err := h.Service.CreateCategory(c.Request().Context(), postgres.CreateCategoryParams{
|
|
CategoryGroupID: categoryGroup.ID,
|
|
Name: newCategory.Name,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, budget)
|
|
}
|