Remove restrição que bloqueava delete de transações importadas. Adiciona DELETE /transactions?month=YYYY-MM para exclusão em lote e botão "EXCLUIR MÊS" na view de transações. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
130 lines
4.6 KiB
Go
130 lines
4.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"financeiro-carvalho/internal/model"
|
|
)
|
|
|
|
type ManualTransactionRepository interface {
|
|
List(ctx context.Context, month string) ([]model.Transaction, error)
|
|
GetByID(ctx context.Context, id int) (*model.Transaction, error)
|
|
Create(ctx context.Context, t model.Transaction) (*model.Transaction, error)
|
|
Update(ctx context.Context, t model.Transaction) (*model.Transaction, error)
|
|
Delete(ctx context.Context, id int) error
|
|
DeleteByMonth(ctx context.Context, month string) (int, error)
|
|
// HasMatchingTransaction checks if a category has a transaction of the given type in the given month.
|
|
HasMatchingTransaction(ctx context.Context, categoryID *int, month string, amount float64, txType string) (bool, error)
|
|
}
|
|
|
|
type manualTxRepo struct{ db *pgxpool.Pool }
|
|
|
|
func NewManualTransactionRepository(db *pgxpool.Pool) ManualTransactionRepository {
|
|
return &manualTxRepo{db: db}
|
|
}
|
|
|
|
func (r *manualTxRepo) List(ctx context.Context, month string) ([]model.Transaction, error) {
|
|
query := `
|
|
SELECT id, date::text, amount, description, type, source, category_id, account_id, created_at, updated_at
|
|
FROM transactions`
|
|
args := []any{}
|
|
if month != "" {
|
|
query += ` WHERE TO_CHAR(date, 'YYYY-MM') = $1`
|
|
args = append(args, month)
|
|
}
|
|
query += ` ORDER BY date DESC, id DESC`
|
|
|
|
rows, err := r.db.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []model.Transaction
|
|
for rows.Next() {
|
|
var t model.Transaction
|
|
if err := rows.Scan(&t.ID, &t.Date, &t.Amount, &t.Description, &t.Type, &t.Source, &t.CategoryID, &t.AccountID, &t.CreatedAt, &t.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, t)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (r *manualTxRepo) GetByID(ctx context.Context, id int) (*model.Transaction, error) {
|
|
var t model.Transaction
|
|
err := r.db.QueryRow(ctx, `
|
|
SELECT id, date::text, amount, description, type, source, category_id, account_id, created_at, updated_at
|
|
FROM transactions WHERE id = $1`, id).
|
|
Scan(&t.ID, &t.Date, &t.Amount, &t.Description, &t.Type, &t.Source, &t.CategoryID, &t.AccountID, &t.CreatedAt, &t.UpdatedAt)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return &t, err
|
|
}
|
|
|
|
func (r *manualTxRepo) Create(ctx context.Context, t model.Transaction) (*model.Transaction, error) {
|
|
var out model.Transaction
|
|
err := r.db.QueryRow(ctx, `
|
|
INSERT INTO transactions (date, amount, description, type, source, category_id, account_id)
|
|
VALUES ($1, $2, $3, $4, 'manual', $5, $6)
|
|
RETURNING id, date::text, amount, description, type, source, category_id, account_id, created_at, updated_at`,
|
|
t.Date, t.Amount, t.Description, t.Type, t.CategoryID, t.AccountID).
|
|
Scan(&out.ID, &out.Date, &out.Amount, &out.Description, &out.Type, &out.Source, &out.CategoryID, &out.AccountID, &out.CreatedAt, &out.UpdatedAt)
|
|
return &out, err
|
|
}
|
|
|
|
func (r *manualTxRepo) Update(ctx context.Context, t model.Transaction) (*model.Transaction, error) {
|
|
var out model.Transaction
|
|
err := r.db.QueryRow(ctx, `
|
|
UPDATE transactions
|
|
SET date=$1, amount=$2, description=$3, type=$4, category_id=$5, account_id=$6, updated_at=NOW()
|
|
WHERE id=$7 AND source='manual'
|
|
RETURNING id, date::text, amount, description, type, source, category_id, account_id, created_at, updated_at`,
|
|
t.Date, t.Amount, t.Description, t.Type, t.CategoryID, t.AccountID, t.ID).
|
|
Scan(&out.ID, &out.Date, &out.Amount, &out.Description, &out.Type, &out.Source, &out.CategoryID, &out.AccountID, &out.CreatedAt, &out.UpdatedAt)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return &out, err
|
|
}
|
|
|
|
func (r *manualTxRepo) Delete(ctx context.Context, id int) error {
|
|
tag, err := r.db.Exec(ctx, `DELETE FROM transactions WHERE id = $1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *manualTxRepo) DeleteByMonth(ctx context.Context, month string) (int, error) {
|
|
tag, err := r.db.Exec(ctx, `DELETE FROM transactions WHERE TO_CHAR(date, 'YYYY-MM') = $1`, month)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return int(tag.RowsAffected()), nil
|
|
}
|
|
|
|
func (r *manualTxRepo) HasMatchingTransaction(ctx context.Context, categoryID *int, month string, amount float64, txType string) (bool, error) {
|
|
if categoryID == nil {
|
|
// No category set — cannot auto-match, always report as uncovered
|
|
return false, nil
|
|
}
|
|
var count int
|
|
err := r.db.QueryRow(ctx, `
|
|
SELECT COUNT(*) FROM transactions
|
|
WHERE category_id = $1
|
|
AND TO_CHAR(date, 'YYYY-MM') = $2
|
|
AND type = $3
|
|
AND amount BETWEEN $4 * 0.9 AND $4 * 1.1`,
|
|
*categoryID, month, txType, amount).Scan(&count)
|
|
return count > 0, err
|
|
}
|