Files
carvalho-finances/apps/api/internal/repository/category.go
T
Mlcavalho1andClaude Sonnet 4.6 d6782d51a5 feat(#17): CRUD de categorias — API Go + tela Vue + testes unitários
API REST /api/categories (GET/POST/PUT/DELETE) com regras de negócio:
categorias padrão não podem ser excluídas; categorias com transações vinculadas
também bloqueadas. Tela Vue com formulário inline de criação/edição, indicador
visual de categorias padrão e botão de exclusão condicional.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-26 20:11:54 -03:00

107 lines
3.0 KiB
Go

package repository
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"financeiro-carvalho/internal/model"
)
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)
Delete(ctx context.Context, id int) error
HasTransactions(ctx context.Context, id int) (bool, error)
}
type categoryRepo struct{ db *pgxpool.Pool }
func NewCategoryRepository(db *pgxpool.Pool) CategoryRepository {
return &categoryRepo{db: db}
}
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
FROM categories
ORDER BY is_default DESC, name ASC`)
if err != nil {
return nil, err
}
defer rows.Close()
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 {
return nil, err
}
out = append(out, c)
}
return out, rows.Err()
}
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
FROM categories WHERE id = $1`, id).
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &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) {
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)
return &c, err
}
func (r *categoryRepo) Update(ctx context.Context, id int, name, color string) (*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)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrNotFound
}
return &c, err
}
func (r *categoryRepo) Delete(ctx context.Context, id int) error {
tag, err := r.db.Exec(ctx, `DELETE FROM categories WHERE id = $1`, id)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrNotFound
}
return nil
}
func (r *categoryRepo) HasTransactions(ctx context.Context, id int) (bool, error) {
var count int
err := r.db.QueryRow(ctx,
`SELECT COUNT(*) FROM transactions WHERE category_id = $1`, id).
Scan(&count)
return count > 0, err
}