package logistica import ( "context" "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} } type CreateCarroInput struct { AgendaID string `json:"agenda_id"` MotoristaID *string `json:"motorista_id"` NomeMotorista *string `json:"nome_motorista"` HorarioChegada *string `json:"horario_chegada"` Observacoes *string `json:"observacoes"` } func (s *Service) CreateCarro(ctx context.Context, input CreateCarroInput) (*generated.LogisticaCarro, error) { agendaID, err := uuid.Parse(input.AgendaID) if err != nil { return nil, err } params := generated.CreateCarroParams{ AgendaID: pgtype.UUID{Bytes: agendaID, Valid: true}, } if input.MotoristaID != nil && *input.MotoristaID != "" { if parsed, err := uuid.Parse(*input.MotoristaID); err == nil { params.MotoristaID = pgtype.UUID{Bytes: parsed, Valid: true} } } if input.NomeMotorista != nil { params.NomeMotorista = pgtype.Text{String: *input.NomeMotorista, Valid: true} } if input.HorarioChegada != nil { params.HorarioChegada = pgtype.Text{String: *input.HorarioChegada, Valid: true} } if input.Observacoes != nil { params.Observacoes = pgtype.Text{String: *input.Observacoes, Valid: true} } carro, err := s.queries.CreateCarro(ctx, params) if err != nil { return nil, err } return &carro, nil } func (s *Service) ListCarros(ctx context.Context, agendaID string) ([]generated.ListCarrosByAgendaIDRow, error) { parsedUUID, err := uuid.Parse(agendaID) if err != nil { return nil, err } return s.queries.ListCarrosByAgendaID(ctx, pgtype.UUID{Bytes: parsedUUID, Valid: true}) } func (s *Service) DeleteCarro(ctx context.Context, id string) error { parsedUUID, err := uuid.Parse(id) if err != nil { return err } return s.queries.DeleteCarro(ctx, pgtype.UUID{Bytes: parsedUUID, Valid: true}) } // UpdateCarroInput matches the update fields type UpdateCarroInput struct { MotoristaID *string `json:"motorista_id"` NomeMotorista *string `json:"nome_motorista"` HorarioChegada *string `json:"horario_chegada"` Observacoes *string `json:"observacoes"` } func (s *Service) UpdateCarro(ctx context.Context, id string, input UpdateCarroInput) (*generated.LogisticaCarro, error) { parsedUUID, err := uuid.Parse(id) if err != nil { return nil, err } params := generated.UpdateCarroParams{ ID: pgtype.UUID{Bytes: parsedUUID, Valid: true}, } if input.MotoristaID != nil { if *input.MotoristaID == "" { params.MotoristaID = pgtype.UUID{Valid: false} } else { if parsed, err := uuid.Parse(*input.MotoristaID); err == nil { params.MotoristaID = pgtype.UUID{Bytes: parsed, Valid: true} } } } if input.NomeMotorista != nil { params.NomeMotorista = pgtype.Text{String: *input.NomeMotorista, Valid: true} } if input.HorarioChegada != nil { params.HorarioChegada = pgtype.Text{String: *input.HorarioChegada, Valid: true} } if input.Observacoes != nil { params.Observacoes = pgtype.Text{String: *input.Observacoes, Valid: true} } carro, err := s.queries.UpdateCarro(ctx, params) if err != nil { return nil, err } return &carro, nil } func (s *Service) AddPassageiro(ctx context.Context, carroID, profissionalID string) error { cID, err := uuid.Parse(carroID) if err != nil { return err } pID, err := uuid.Parse(profissionalID) if err != nil { return err } _, err = s.queries.AddPassageiro(ctx, generated.AddPassageiroParams{ CarroID: pgtype.UUID{Bytes: cID, Valid: true}, ProfissionalID: pgtype.UUID{Bytes: pID, Valid: true}, }) return err } func (s *Service) RemovePassageiro(ctx context.Context, carroID, profissionalID string) error { cID, err := uuid.Parse(carroID) if err != nil { return err } pID, err := uuid.Parse(profissionalID) if err != nil { return err } return s.queries.RemovePassageiro(ctx, generated.RemovePassageiroParams{ CarroID: pgtype.UUID{Bytes: cID, Valid: true}, ProfissionalID: pgtype.UUID{Bytes: pID, Valid: true}, }) } func (s *Service) ListPassageiros(ctx context.Context, carroID string) ([]generated.ListPassageirosByCarroIDRow, error) { cID, err := uuid.Parse(carroID) if err != nil { return nil, err } return s.queries.ListPassageirosByCarroID(ctx, pgtype.UUID{Bytes: cID, Valid: true}) }