photum/backend/internal/anos_formaturas/service.go
NANDO9322 7f1d4144db feat(api): Implementado novas tabelas ,refatorado a autenticação e aprimorado a documentação Swagger
- Implementado CRUDs para: cursos, empresas, anos_formaturas, tipos_servicos, tipos_eventos
- Implementado lógica de precificação de eventos (precos_tipos_eventos)
- Refatorado a autenticação: Simplificar o payload de cadastro/login (somente e-mail/senha), função padrão 'profissional'
- Corrigido o middleware de autenticação: Resolvido a incompatibilidade de tipo UUID vs String (corrigir erro 500)
- Aprimorado o Swagger: Adicionado structs nomeados, validação de duplicatas (409 Conflict) e segurança BearerAuth
- Atualizar o esquema do banco de dados: Adicionar tabelas e restrições
2025-12-09 17:05:19 -03:00

80 lines
2 KiB
Go

package anos_formaturas
import (
"context"
"errors"
"regexp"
"photum-backend/internal/db/generated"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
type Service struct {
queries *generated.Queries
}
func NewService(queries *generated.Queries) *Service {
return &Service{queries: queries}
}
func (s *Service) Create(ctx context.Context, anoSemestre string) (*generated.AnosFormatura, error) {
// Validate format YYYY.S
matched, _ := regexp.MatchString(`^\d{4}\.[1-2]$`, anoSemestre)
if !matched {
return nil, errors.New("invalid format. Expected YYYY.1 or YYYY.2")
}
ano, err := s.queries.CreateAnoFormatura(ctx, anoSemestre)
if err != nil {
return nil, err
}
return &ano, nil
}
func (s *Service) List(ctx context.Context) ([]generated.AnosFormatura, error) {
return s.queries.ListAnosFormaturas(ctx)
}
func (s *Service) GetByID(ctx context.Context, id string) (*generated.AnosFormatura, error) {
uuidVal, err := uuid.Parse(id)
if err != nil {
return nil, errors.New("invalid id")
}
ano, err := s.queries.GetAnoFormaturaByID(ctx, pgtype.UUID{Bytes: uuidVal, Valid: true})
if err != nil {
return nil, err
}
return &ano, nil
}
func (s *Service) Update(ctx context.Context, id, anoSemestre string) (*generated.AnosFormatura, error) {
uuidVal, err := uuid.Parse(id)
if err != nil {
return nil, errors.New("invalid id")
}
// Validate format YYYY.S
matched, _ := regexp.MatchString(`^\d{4}\.[1-2]$`, anoSemestre)
if !matched {
return nil, errors.New("invalid format. Expected YYYY.1 or YYYY.2")
}
ano, err := s.queries.UpdateAnoFormatura(ctx, generated.UpdateAnoFormaturaParams{
ID: pgtype.UUID{Bytes: uuidVal, Valid: true},
AnoSemestre: anoSemestre,
})
if err != nil {
return nil, err
}
return &ano, nil
}
func (s *Service) Delete(ctx context.Context, id string) error {
uuidVal, err := uuid.Parse(id)
if err != nil {
return errors.New("invalid id")
}
return s.queries.DeleteAnoFormatura(ctx, pgtype.UUID{Bytes: uuidVal, Valid: true})
}