feat: categoria is_tax exclui impostos do denominador do savings_pct

savings_pct = (receita - despesas) / (receita - impostos).
Toggle IMPOSTO no CRUD de categorias; badge dourado na lista.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-27 15:53:53 -03:00
co-authored by Claude Sonnet 4.6
parent 2cf6d94a03
commit 7dae05cabf
10 changed files with 90 additions and 35 deletions
+18 -18
View File
@@ -15,8 +15,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) (*model.Category, error)
Update(ctx context.Context, id int, name, color string) (*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)
Delete(ctx context.Context, id int) error
HasTransactions(ctx context.Context, id int) (bool, error)
}
@@ -29,7 +29,7 @@ func NewCategoryRepository(db *pgxpool.Pool) CategoryRepository {
func (r *categoryRepo) List(ctx context.Context) ([]model.Category, error) {
rows, err := r.db.Query(ctx, `
SELECT id, name, color, is_default, created_at, updated_at
SELECT id, name, color, is_default, is_tax, created_at, updated_at
FROM categories
ORDER BY is_default DESC, name ASC`)
if err != nil {
@@ -40,7 +40,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.CreatedAt, &c.UpdatedAt); err != nil {
if err := rows.Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt); err != nil {
return nil, err
}
out = append(out, c)
@@ -51,35 +51,35 @@ func (r *categoryRepo) List(ctx context.Context) ([]model.Category, error) {
func (r *categoryRepo) GetByID(ctx context.Context, id int) (*model.Category, error) {
var c model.Category
err := r.db.QueryRow(ctx, `
SELECT id, name, color, is_default, created_at, updated_at
SELECT id, name, color, is_default, is_tax, created_at, updated_at
FROM categories WHERE id = $1`, id).
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.CreatedAt, &c.UpdatedAt)
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &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) (*model.Category, error) {
func (r *categoryRepo) Create(ctx context.Context, name, color string, isTax bool) (*model.Category, error) {
var c model.Category
err := r.db.QueryRow(ctx, `
INSERT INTO categories (name, color)
VALUES ($1, $2)
RETURNING id, name, color, is_default, created_at, updated_at`,
name, color).
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.CreatedAt, &c.UpdatedAt)
INSERT INTO categories (name, color, is_tax)
VALUES ($1, $2, $3)
RETURNING id, name, color, is_default, is_tax, created_at, updated_at`,
name, color, isTax).
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
return &c, err
}
func (r *categoryRepo) Update(ctx context.Context, id int, name, color string) (*model.Category, error) {
func (r *categoryRepo) Update(ctx context.Context, id int, name, color string, isTax bool) (*model.Category, error) {
var c model.Category
err := r.db.QueryRow(ctx, `
UPDATE categories
SET name = $1, color = $2, updated_at = NOW()
WHERE id = $3
RETURNING id, name, color, is_default, created_at, updated_at`,
name, color, id).
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.CreatedAt, &c.UpdatedAt)
SET name = $1, color = $2, is_tax = $3, updated_at = NOW()
WHERE id = $4
RETURNING id, name, color, is_default, is_tax, created_at, updated_at`,
name, color, isTax, id).
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrNotFound
}