Files
carvalho-finances/apps/api/internal/service/account.go
T
Mlcavalho1andClaude Sonnet 4.6 9a964423fd feat(#21): contas bancárias e patrimônio consolidado
CRUD de contas (corrente/poupança/investimento/cartão) com saldo calculado
automaticamente. account_id nullable em transactions. Widget patrimônio no
dashboard. Tela /contas. Seletor de conta nas transações manuais.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-26 21:34:56 -03:00

71 lines
1.8 KiB
Go

package service
import (
"context"
"errors"
"financeiro-carvalho/internal/model"
)
var (
ErrAccountEmptyName = errors.New("account name cannot be empty")
ErrAccountInvalidType = errors.New("account type must be checking, savings, investment, or credit")
)
var validAccountTypes = map[string]bool{
"checking": true, "savings": true, "investment": true, "credit": true,
}
type AccountRepo interface {
List(ctx context.Context) ([]model.Account, error)
GetByID(ctx context.Context, id int) (*model.Account, error)
Create(ctx context.Context, in model.AccountInput) (*model.Account, error)
Update(ctx context.Context, id int, in model.AccountInput) (*model.Account, error)
Delete(ctx context.Context, id int) error
TotalPatrimony(ctx context.Context) (float64, error)
}
type AccountService struct {
repo AccountRepo
}
func NewAccountService(repo AccountRepo) *AccountService {
return &AccountService{repo: repo}
}
func (s *AccountService) validate(in model.AccountInput) error {
if in.Name == "" {
return ErrAccountEmptyName
}
if !validAccountTypes[in.Type] {
return ErrAccountInvalidType
}
return nil
}
func (s *AccountService) List(ctx context.Context) ([]model.Account, error) {
return s.repo.List(ctx)
}
func (s *AccountService) Create(ctx context.Context, in model.AccountInput) (*model.Account, error) {
if err := s.validate(in); err != nil {
return nil, err
}
return s.repo.Create(ctx, in)
}
func (s *AccountService) Update(ctx context.Context, id int, in model.AccountInput) (*model.Account, error) {
if err := s.validate(in); err != nil {
return nil, err
}
return s.repo.Update(ctx, id, in)
}
func (s *AccountService) Delete(ctx context.Context, id int) error {
return s.repo.Delete(ctx, id)
}
func (s *AccountService) TotalPatrimony(ctx context.Context) (float64, error) {
return s.repo.TotalPatrimony(ctx)
}