76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"datarush/internal/competition/domain"
|
|
"datarush/internal/competition/repository"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Service struct {
|
|
repo repository.CompetitionRepository
|
|
}
|
|
|
|
func NewService(repo repository.CompetitionRepository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) CreateCompetition(ctx context.Context, comp *domain.Competition) (*domain.Competition, error) {
|
|
now := time.Now()
|
|
if comp.ID == uuid.Nil {
|
|
comp.ID = uuid.New()
|
|
}
|
|
comp.CreatedAt = now
|
|
comp.UpdatedAt = now
|
|
|
|
if err := s.repo.Create(ctx, comp); err != nil {
|
|
return nil, err
|
|
}
|
|
return comp, nil
|
|
}
|
|
|
|
func (s *Service) GetCompetition(ctx context.Context, id uuid.UUID) (*domain.Competition, error) {
|
|
return s.repo.Get(ctx, id)
|
|
}
|
|
|
|
func (s *Service) EditCompetition(ctx context.Context, comp *domain.Competition) (*domain.Competition, error) {
|
|
comp.UpdatedAt = time.Now()
|
|
if err := s.repo.Update(ctx, comp); err != nil {
|
|
return nil, err
|
|
}
|
|
return s.repo.Get(ctx, comp.ID)
|
|
}
|
|
|
|
func (s *Service) DeleteCompetition(ctx context.Context, id uuid.UUID) error {
|
|
return s.repo.Delete(ctx, id)
|
|
}
|
|
|
|
func (s *Service) ListCompetitions(ctx context.Context, pageSize int32, pageToken int32, state *domain.CompetitionState, isParticipating *bool, searchQuery *string) ([]domain.Competition, int32, int32, error) {
|
|
opts := repository.ListCompetitionsOptions{
|
|
Page: int(pageToken),
|
|
PageSize: int(pageSize),
|
|
State: state,
|
|
IsParticipating: isParticipating,
|
|
SearchQuery: searchQuery,
|
|
}
|
|
|
|
competitions, total, err := s.repo.List(ctx, opts)
|
|
if err != nil {
|
|
return nil, 0, 0, err
|
|
}
|
|
|
|
var nextPageToken int32
|
|
if (opts.Page+1)*opts.PageSize < total {
|
|
nextPageToken = int32(opts.Page + 1)
|
|
}
|
|
|
|
return competitions, int32(total), nextPageToken, nil
|
|
}
|
|
|
|
func (s *Service) ChangeCompetitionState(ctx context.Context, id uuid.UUID, state domain.CompetitionState) (*domain.Competition, error) {
|
|
return s.repo.ChangeState(ctx, id, state)
|
|
}
|