create-categories #79
@ -91,6 +91,24 @@ func (q *Queries) GetCategories(ctx context.Context, budgetID uuid.UUID) ([]GetC
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getCategoryGroupByName = `-- name: GetCategoryGroupByName :one
|
||||||
|
SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups
|
||||||
|
WHERE category_groups.budget_id = $1
|
||||||
|
AND category_groups.name = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetCategoryGroupByNameParams struct {
|
||||||
|
BudgetID uuid.UUID
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetCategoryGroupByName(ctx context.Context, arg GetCategoryGroupByNameParams) (CategoryGroup, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getCategoryGroupByName, arg.BudgetID, arg.Name)
|
||||||
|
var i CategoryGroup
|
||||||
|
err := row.Scan(&i.ID, &i.BudgetID, &i.Name)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const getCategoryGroups = `-- name: GetCategoryGroups :many
|
const getCategoryGroups = `-- name: GetCategoryGroups :many
|
||||||
SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups
|
SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups
|
||||||
WHERE category_groups.budget_id = $1
|
WHERE category_groups.budget_id = $1
|
||||||
|
@ -8,6 +8,11 @@ RETURNING *;
|
|||||||
SELECT category_groups.* FROM category_groups
|
SELECT category_groups.* FROM category_groups
|
||||||
WHERE category_groups.budget_id = $1;
|
WHERE category_groups.budget_id = $1;
|
||||||
|
|
||||||
|
-- name: GetCategoryGroupByName :one
|
||||||
|
SELECT category_groups.* FROM category_groups
|
||||||
|
WHERE category_groups.budget_id = $1
|
||||||
|
AND category_groups.name = $2;
|
||||||
|
|
||||||
-- name: CreateCategory :one
|
-- name: CreateCategory :one
|
||||||
INSERT INTO categories
|
INSERT INTO categories
|
||||||
(name, category_group_id)
|
(name, category_group_id)
|
||||||
|
49
server/category_new.go
Normal file
49
server/category_new.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
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)
|
||||||
|
}
|
@ -53,6 +53,9 @@ func (h *Handler) LoadRoutes(router *echo.Echo) {
|
|||||||
account.POST("/:accountid/reconcile", h.reconcileTransactions)
|
account.POST("/:accountid/reconcile", h.reconcileTransactions)
|
||||||
account.POST("/:accountid", h.editAccount)
|
account.POST("/:accountid", h.editAccount)
|
||||||
|
|
||||||
|
category := authenticated.Group("/category")
|
||||||
|
category.POST("/new", h.newCategory)
|
||||||
|
|
||||||
budget := authenticated.Group("/budget")
|
budget := authenticated.Group("/budget")
|
||||||
budget.POST("/new", h.newBudget)
|
budget.POST("/new", h.newBudget)
|
||||||
budget.GET("/:budgetid", h.budget)
|
budget.GET("/:budgetid", h.budget)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user