budgeteer/server/category_new.go
Jan Bader 2b15231ed1
All checks were successful
continuous-integration/drone/push Build is passing
Implement backend
2022-09-11 21:10:04 +00:00

50 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 == "" {
// TODO Handle new Group here?
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)
}