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
This commit is contained in:
@@ -55,14 +55,14 @@ func main() {
|
|||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
r.Use(middleware.Recoverer)
|
r.Use(middleware.Recoverer)
|
||||||
|
|
||||||
categoryRepo := repository.NewCategoryRepository(pool)
|
|
||||||
categorySvc := service.NewCategoryService(categoryRepo)
|
|
||||||
categoryHandler := handler.NewCategoryHandler(categorySvc)
|
|
||||||
|
|
||||||
gameRepo := repository.NewGameRepository(pool)
|
gameRepo := repository.NewGameRepository(pool)
|
||||||
gameSvc := service.NewGameService(gameRepo)
|
gameSvc := service.NewGameService(gameRepo)
|
||||||
gameHandler := handler.NewGameHandler(gameSvc)
|
gameHandler := handler.NewGameHandler(gameSvc)
|
||||||
|
|
||||||
|
categoryRepo := repository.NewCategoryRepository(pool)
|
||||||
|
categorySvc := service.NewCategoryService(categoryRepo)
|
||||||
|
categoryHandler := handler.NewCategoryHandler(categorySvc, gameSvc)
|
||||||
|
|
||||||
transactionRepo := repository.NewTransactionRepository(pool)
|
transactionRepo := repository.NewTransactionRepository(pool)
|
||||||
importSvc := service.NewImportService(transactionRepo)
|
importSvc := service.NewImportService(transactionRepo)
|
||||||
pendingBillRepo := repository.NewPendingBillRepository(pool)
|
pendingBillRepo := repository.NewPendingBillRepository(pool)
|
||||||
|
|||||||
@@ -14,11 +14,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CategoryHandler struct {
|
type CategoryHandler struct {
|
||||||
svc *service.CategoryService
|
svc *service.CategoryService
|
||||||
|
gameSvc *service.GameService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCategoryHandler(svc *service.CategoryService) *CategoryHandler {
|
func NewCategoryHandler(svc *service.CategoryService, gameSvc *service.GameService) *CategoryHandler {
|
||||||
return &CategoryHandler{svc: svc}
|
return &CategoryHandler{svc: svc, gameSvc: gameSvc}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *CategoryHandler) List(w http.ResponseWriter, r *http.Request) {
|
func (h *CategoryHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -44,6 +45,7 @@ func (h *CategoryHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
respondError(w, http.StatusBadRequest, err.Error())
|
respondError(w, http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
h.gameSvc.OnCategoryBudgetSet(r.Context(), cat.ID, cat.Name, in.MonthlyBudget)
|
||||||
respondJSON(w, http.StatusCreated, cat)
|
respondJSON(w, http.StatusCreated, cat)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +69,7 @@ func (h *CategoryHandler) Update(w http.ResponseWriter, r *http.Request) {
|
|||||||
respondError(w, http.StatusBadRequest, err.Error())
|
respondError(w, http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
h.gameSvc.OnCategoryBudgetSet(r.Context(), cat.ID, cat.Name, in.MonthlyBudget)
|
||||||
respondJSON(w, http.StatusOK, cat)
|
respondJSON(w, http.StatusOK, cat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,6 +59,9 @@ var m016 string
|
|||||||
//go:embed sql/017_user_settings.sql
|
//go:embed sql/017_user_settings.sql
|
||||||
var m017 string
|
var m017 string
|
||||||
|
|
||||||
|
//go:embed sql/018_dynamic_achievements.sql
|
||||||
|
var m018 string
|
||||||
|
|
||||||
func Run(ctx context.Context, pool *pgxpool.Pool) error {
|
func Run(ctx context.Context, pool *pgxpool.Pool) error {
|
||||||
// Bootstrap: ensure schema_migrations table exists before checking versions.
|
// Bootstrap: ensure schema_migrations table exists before checking versions.
|
||||||
if _, err := pool.Exec(ctx, `
|
if _, err := pool.Exec(ctx, `
|
||||||
@@ -70,7 +73,7 @@ func Run(ctx context.Context, pool *pgxpool.Pool) error {
|
|||||||
return fmt.Errorf("bootstrap schema_migrations: %w", err)
|
return fmt.Errorf("bootstrap schema_migrations: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011, m012, m013, m014, m015, m016, m017}
|
migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011, m012, m013, m014, m015, m016, m017, m018}
|
||||||
for i, sql := range migrations {
|
for i, sql := range migrations {
|
||||||
version := i + 1
|
version := i + 1
|
||||||
var applied bool
|
var applied bool
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
ALTER TABLE categories ADD COLUMN IF NOT EXISTS monthly_budget NUMERIC(12,2);
|
||||||
|
|
||||||
|
ALTER TABLE achievements ADD COLUMN IF NOT EXISTS category_id INTEGER REFERENCES categories(id) ON DELETE CASCADE;
|
||||||
|
|
||||||
|
-- Remove conquistas hardcoded com categorias específicas
|
||||||
|
DELETE FROM player_achievements WHERE achievement_id IN (
|
||||||
|
SELECT id FROM achievements WHERE unlock_condition IN ('veleiro_under_500', 'health_3months')
|
||||||
|
);
|
||||||
|
DELETE FROM achievements WHERE unlock_condition IN ('veleiro_under_500', 'health_3months');
|
||||||
|
|
||||||
|
-- Reset game state do profile_id=1
|
||||||
|
DELETE FROM player_achievements WHERE profile_id = 1;
|
||||||
|
DELETE FROM xp_events WHERE profile_id = 1;
|
||||||
|
DELETE FROM player_quests WHERE profile_id = 1;
|
||||||
|
UPDATE player_profile SET level = 1, xp = 0, xp_to_next = 100 WHERE profile_id = 1;
|
||||||
|
|
||||||
|
INSERT INTO schema_migrations (version) VALUES (18) ON CONFLICT DO NOTHING;
|
||||||
@@ -3,17 +3,19 @@ package model
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Category struct {
|
type Category struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
IsDefault bool `json:"is_default"`
|
IsDefault bool `json:"is_default"`
|
||||||
IsTax bool `json:"is_tax"`
|
IsTax bool `json:"is_tax"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
MonthlyBudget *float64 `json:"monthly_budget"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CategoryInput struct {
|
type CategoryInput struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
IsTax bool `json:"is_tax"`
|
IsTax bool `json:"is_tax"`
|
||||||
|
MonthlyBudget *float64 `json:"monthly_budget"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,14 +41,20 @@ type PlayerQuest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Achievement struct {
|
type Achievement struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
XPReward int `json:"xp_reward"`
|
XPReward int `json:"xp_reward"`
|
||||||
UnlockCondition string `json:"unlock_condition"`
|
UnlockCondition string `json:"unlock_condition"`
|
||||||
Earned bool `json:"earned"`
|
CategoryID *int `json:"category_id,omitempty"`
|
||||||
EarnedAt string `json:"earned_at,omitempty"`
|
Earned bool `json:"earned"`
|
||||||
|
EarnedAt string `json:"earned_at,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CategoryBudgetInfo struct {
|
||||||
|
CategoryID int
|
||||||
|
MonthlyBudget float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type Cosmetic struct {
|
type Cosmetic struct {
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ var ErrNotFound = errors.New("not found")
|
|||||||
type CategoryRepository interface {
|
type CategoryRepository interface {
|
||||||
List(ctx context.Context) ([]model.Category, error)
|
List(ctx context.Context) ([]model.Category, error)
|
||||||
GetByID(ctx context.Context, id int) (*model.Category, error)
|
GetByID(ctx context.Context, id int) (*model.Category, error)
|
||||||
Create(ctx context.Context, name, color string, isTax bool) (*model.Category, error)
|
Create(ctx context.Context, name, color string, isTax bool, budget *float64) (*model.Category, error)
|
||||||
Update(ctx context.Context, id int, name, color string, isTax bool) (*model.Category, error)
|
Update(ctx context.Context, id int, name, color string, isTax bool, budget *float64) (*model.Category, error)
|
||||||
Delete(ctx context.Context, id int) error
|
Delete(ctx context.Context, id int) error
|
||||||
HasTransactions(ctx context.Context, id int) (bool, error)
|
HasTransactions(ctx context.Context, id int) (bool, error)
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ func NewCategoryRepository(db *pgxpool.Pool) CategoryRepository {
|
|||||||
func (r *categoryRepo) List(ctx context.Context) ([]model.Category, error) {
|
func (r *categoryRepo) List(ctx context.Context) ([]model.Category, error) {
|
||||||
pid := middleware.ProfileIDFromCtx(ctx)
|
pid := middleware.ProfileIDFromCtx(ctx)
|
||||||
rows, err := r.db.Query(ctx, `
|
rows, err := r.db.Query(ctx, `
|
||||||
SELECT id, name, color, is_default, is_tax, created_at, updated_at
|
SELECT id, name, color, is_default, is_tax, monthly_budget, created_at, updated_at
|
||||||
FROM categories
|
FROM categories
|
||||||
WHERE profile_id = $1
|
WHERE profile_id = $1
|
||||||
ORDER BY is_default DESC, name ASC`, pid)
|
ORDER BY is_default DESC, name ASC`, pid)
|
||||||
@@ -43,7 +43,7 @@ func (r *categoryRepo) List(ctx context.Context) ([]model.Category, error) {
|
|||||||
var out []model.Category
|
var out []model.Category
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var c model.Category
|
var c model.Category
|
||||||
if err := rows.Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt); err != nil {
|
if err := rows.Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.MonthlyBudget, &c.CreatedAt, &c.UpdatedAt); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
out = append(out, c)
|
out = append(out, c)
|
||||||
@@ -55,37 +55,37 @@ func (r *categoryRepo) GetByID(ctx context.Context, id int) (*model.Category, er
|
|||||||
pid := middleware.ProfileIDFromCtx(ctx)
|
pid := middleware.ProfileIDFromCtx(ctx)
|
||||||
var c model.Category
|
var c model.Category
|
||||||
err := r.db.QueryRow(ctx, `
|
err := r.db.QueryRow(ctx, `
|
||||||
SELECT id, name, color, is_default, is_tax, created_at, updated_at
|
SELECT id, name, color, is_default, is_tax, monthly_budget, created_at, updated_at
|
||||||
FROM categories WHERE id = $1 AND profile_id = $2`, id, pid).
|
FROM categories WHERE id = $1 AND profile_id = $2`, id, pid).
|
||||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
|
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.MonthlyBudget, &c.CreatedAt, &c.UpdatedAt)
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
return &c, err
|
return &c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *categoryRepo) Create(ctx context.Context, name, color string, isTax bool) (*model.Category, error) {
|
func (r *categoryRepo) Create(ctx context.Context, name, color string, isTax bool, budget *float64) (*model.Category, error) {
|
||||||
pid := middleware.ProfileIDFromCtx(ctx)
|
pid := middleware.ProfileIDFromCtx(ctx)
|
||||||
var c model.Category
|
var c model.Category
|
||||||
err := r.db.QueryRow(ctx, `
|
err := r.db.QueryRow(ctx, `
|
||||||
INSERT INTO categories (name, color, is_tax, profile_id)
|
INSERT INTO categories (name, color, is_tax, monthly_budget, profile_id)
|
||||||
VALUES ($1, $2, $3, $4)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
RETURNING id, name, color, is_default, is_tax, created_at, updated_at`,
|
RETURNING id, name, color, is_default, is_tax, monthly_budget, created_at, updated_at`,
|
||||||
name, color, isTax, pid).
|
name, color, isTax, budget, pid).
|
||||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
|
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.MonthlyBudget, &c.CreatedAt, &c.UpdatedAt)
|
||||||
return &c, err
|
return &c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *categoryRepo) Update(ctx context.Context, id int, name, color string, isTax bool) (*model.Category, error) {
|
func (r *categoryRepo) Update(ctx context.Context, id int, name, color string, isTax bool, budget *float64) (*model.Category, error) {
|
||||||
pid := middleware.ProfileIDFromCtx(ctx)
|
pid := middleware.ProfileIDFromCtx(ctx)
|
||||||
var c model.Category
|
var c model.Category
|
||||||
err := r.db.QueryRow(ctx, `
|
err := r.db.QueryRow(ctx, `
|
||||||
UPDATE categories
|
UPDATE categories
|
||||||
SET name = $1, color = $2, is_tax = $3, updated_at = NOW()
|
SET name = $1, color = $2, is_tax = $3, monthly_budget = $4, updated_at = NOW()
|
||||||
WHERE id = $4 AND profile_id = $5
|
WHERE id = $5 AND profile_id = $6
|
||||||
RETURNING id, name, color, is_default, is_tax, created_at, updated_at`,
|
RETURNING id, name, color, is_default, is_tax, monthly_budget, created_at, updated_at`,
|
||||||
name, color, isTax, id, pid).
|
name, color, isTax, budget, id, pid).
|
||||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
|
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.MonthlyBudget, &c.CreatedAt, &c.UpdatedAt)
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,14 +32,14 @@ func (s *CategoryService) Create(ctx context.Context, in model.CategoryInput) (*
|
|||||||
if err := validateInput(in); err != nil {
|
if err := validateInput(in); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return s.repo.Create(ctx, strings.TrimSpace(in.Name), in.Color, in.IsTax)
|
return s.repo.Create(ctx, strings.TrimSpace(in.Name), in.Color, in.IsTax, in.MonthlyBudget)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CategoryService) Update(ctx context.Context, id int, in model.CategoryInput) (*model.Category, error) {
|
func (s *CategoryService) Update(ctx context.Context, id int, in model.CategoryInput) (*model.Category, error) {
|
||||||
if err := validateInput(in); err != nil {
|
if err := validateInput(in); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return s.repo.Update(ctx, id, strings.TrimSpace(in.Name), in.Color, in.IsTax)
|
return s.repo.Update(ctx, id, strings.TrimSpace(in.Name), in.Color, in.IsTax, in.MonthlyBudget)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CategoryService) Delete(ctx context.Context, id int) error {
|
func (s *CategoryService) Delete(ctx context.Context, id int) error {
|
||||||
|
|||||||
@@ -33,13 +33,13 @@ func (m *mockCategoryRepo) GetByID(_ context.Context, id int) (*model.Category,
|
|||||||
return nil, repository.ErrNotFound
|
return nil, repository.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockCategoryRepo) Create(_ context.Context, name, color string, _ bool) (*model.Category, error) {
|
func (m *mockCategoryRepo) Create(_ context.Context, name, color string, _ bool, _ *float64) (*model.Category, error) {
|
||||||
c := model.Category{ID: len(m.categories) + 1, Name: name, Color: color}
|
c := model.Category{ID: len(m.categories) + 1, Name: name, Color: color}
|
||||||
m.categories = append(m.categories, c)
|
m.categories = append(m.categories, c)
|
||||||
return &c, nil
|
return &c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockCategoryRepo) Update(_ context.Context, id int, name, color string, _ bool) (*model.Category, error) {
|
func (m *mockCategoryRepo) Update(_ context.Context, id int, name, color string, _ bool, _ *float64) (*model.Category, error) {
|
||||||
for i, c := range m.categories {
|
for i, c := range m.categories {
|
||||||
if c.ID == id {
|
if c.ID == id {
|
||||||
m.categories[i].Name = name
|
m.categories[i].Name = name
|
||||||
|
|||||||
Reference in New Issue
Block a user