Files
carvalho-finances/apps/api/internal/handler/category.go
T
Mlcavalho1 f60ca423d0 feat(schema): #48 monthly_budget em categories + category_id em achievements
- Migration 018: ADD COLUMN monthly_budget NUMERIC(12,2) em categories
- Migration 018: ADD COLUMN category_id INTEGER REFERENCES categories(id) ON DELETE CASCADE em achievements
- Remove conquistas hardcoded veleiro_under_500 e health_3months do banco
- Reset game state do profile_id=1
- Atualiza CategoryRepository, CategoryInput e Category model para incluir monthly_budget
2026-05-28 23:26:39 -03:00

96 lines
2.7 KiB
Go

package handler
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
"financeiro-carvalho/internal/service"
)
type CategoryHandler struct {
svc *service.CategoryService
gameSvc *service.GameService
}
func NewCategoryHandler(svc *service.CategoryService, gameSvc *service.GameService) *CategoryHandler {
return &CategoryHandler{svc: svc, gameSvc: gameSvc}
}
func (h *CategoryHandler) List(w http.ResponseWriter, r *http.Request) {
cats, err := h.svc.List(r.Context())
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to list categories")
return
}
if cats == nil {
cats = []model.Category{}
}
respondJSON(w, http.StatusOK, cats)
}
func (h *CategoryHandler) Create(w http.ResponseWriter, r *http.Request) {
var in model.CategoryInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
cat, err := h.svc.Create(r.Context(), in)
if err != nil {
respondError(w, http.StatusBadRequest, err.Error())
return
}
h.gameSvc.OnCategoryBudgetSet(r.Context(), cat.ID, cat.Name, in.MonthlyBudget)
respondJSON(w, http.StatusCreated, cat)
}
func (h *CategoryHandler) Update(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
var in model.CategoryInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
cat, err := h.svc.Update(r.Context(), id, in)
if errors.Is(err, repository.ErrNotFound) {
respondError(w, http.StatusNotFound, "category not found")
return
}
if err != nil {
respondError(w, http.StatusBadRequest, err.Error())
return
}
h.gameSvc.OnCategoryBudgetSet(r.Context(), cat.ID, cat.Name, in.MonthlyBudget)
respondJSON(w, http.StatusOK, cat)
}
func (h *CategoryHandler) Delete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
err = h.svc.Delete(r.Context(), id)
switch {
case errors.Is(err, repository.ErrNotFound):
respondError(w, http.StatusNotFound, "category not found")
case errors.Is(err, service.ErrDefaultCategory):
respondError(w, http.StatusConflict, err.Error())
case errors.Is(err, service.ErrHasTransactions):
respondError(w, http.StatusConflict, err.Error())
case err != nil:
respondError(w, http.StatusInternalServerError, "failed to delete")
default:
w.WriteHeader(http.StatusNoContent)
}
}