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:
@@ -16,8 +16,8 @@ var ErrNotFound = errors.New("not found")
|
||||
type CategoryRepository interface {
|
||||
List(ctx context.Context) ([]model.Category, error)
|
||||
GetByID(ctx context.Context, id int) (*model.Category, error)
|
||||
Create(ctx context.Context, name, color string, isTax bool) (*model.Category, error)
|
||||
Update(ctx context.Context, id int, 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, budget *float64) (*model.Category, error)
|
||||
Delete(ctx context.Context, id int) 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) {
|
||||
pid := middleware.ProfileIDFromCtx(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
|
||||
WHERE profile_id = $1
|
||||
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
|
||||
for rows.Next() {
|
||||
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
|
||||
}
|
||||
out = append(out, c)
|
||||
@@ -55,37 +55,37 @@ func (r *categoryRepo) GetByID(ctx context.Context, id int) (*model.Category, er
|
||||
pid := middleware.ProfileIDFromCtx(ctx)
|
||||
var c model.Category
|
||||
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).
|
||||
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) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
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)
|
||||
var c model.Category
|
||||
err := r.db.QueryRow(ctx, `
|
||||
INSERT INTO categories (name, color, is_tax, profile_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, name, color, is_default, is_tax, created_at, updated_at`,
|
||||
name, color, isTax, pid).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
|
||||
INSERT INTO categories (name, color, is_tax, monthly_budget, profile_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, name, color, is_default, is_tax, monthly_budget, created_at, updated_at`,
|
||||
name, color, isTax, budget, pid).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.MonthlyBudget, &c.CreatedAt, &c.UpdatedAt)
|
||||
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)
|
||||
var c model.Category
|
||||
err := r.db.QueryRow(ctx, `
|
||||
UPDATE categories
|
||||
SET name = $1, color = $2, is_tax = $3, updated_at = NOW()
|
||||
WHERE id = $4 AND profile_id = $5
|
||||
RETURNING id, name, color, is_default, is_tax, created_at, updated_at`,
|
||||
name, color, isTax, id, pid).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
|
||||
SET name = $1, color = $2, is_tax = $3, monthly_budget = $4, updated_at = NOW()
|
||||
WHERE id = $5 AND profile_id = $6
|
||||
RETURNING id, name, color, is_default, is_tax, monthly_budget, created_at, updated_at`,
|
||||
name, color, isTax, budget, id, pid).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.MonthlyBudget, &c.CreatedAt, &c.UpdatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user