52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
flightinspection "wucher/internal/domain/flight_inspection"
|
|
)
|
|
|
|
type FlightInspectionService struct {
|
|
repo flightinspection.FlightInspectionRepository
|
|
}
|
|
|
|
func NewFlightInspectionService(repo flightinspection.FlightInspectionRepository) *FlightInspectionService {
|
|
return &FlightInspectionService{repo: repo}
|
|
}
|
|
|
|
func (s *FlightInspectionService) CreateDraft(ctx context.Context, row *flightinspection.FlightInspection) error {
|
|
row.Status = flightinspection.StatusDraft
|
|
row.InspectionDate = dateOnlyUTCService(row.InspectionDate)
|
|
return s.repo.Create(ctx, row)
|
|
}
|
|
|
|
func (s *FlightInspectionService) Update(ctx context.Context, row *flightinspection.FlightInspection) error {
|
|
row.InspectionDate = dateOnlyUTCService(row.InspectionDate)
|
|
return s.repo.Update(ctx, row)
|
|
}
|
|
|
|
func (s *FlightInspectionService) GetByID(ctx context.Context, id []byte) (*flightinspection.FlightInspection, error) {
|
|
return s.repo.GetByID(ctx, id)
|
|
}
|
|
|
|
func (s *FlightInspectionService) List(ctx context.Context, sort string, limit, offset int) ([]flightinspection.FlightInspection, int64, error) {
|
|
return s.repo.List(ctx, normalizeFlightInspectionSort(sort), limit, offset)
|
|
}
|
|
|
|
func normalizeFlightInspectionSort(sort string) string {
|
|
if normalized := normalizeSortByRules(strings.TrimSpace(sort),
|
|
sortExpr("inspection_date ASC, created_at ASC", "inspection_date DESC, created_at DESC", "inspection_date"),
|
|
sortField("created_at"),
|
|
sortField("updated_at"),
|
|
); normalized != "" {
|
|
return normalized
|
|
}
|
|
return "inspection_date DESC, created_at DESC"
|
|
}
|
|
|
|
func dateOnlyUTCService(t time.Time) time.Time {
|
|
return time.Date(t.UTC().Year(), t.UTC().Month(), t.UTC().Day(), 0, 0, 0, 0, time.UTC)
|
|
}
|