Files
Datarush/internal/competition/service/service.go
T
2025-12-17 12:41:03 +03:00

91 lines
2.2 KiB
Go

package service
import (
"context"
"datarush/internal/competition/repository"
pb "datarush/pkg/api/competition"
"github.com/google/uuid"
"google.golang.org/protobuf/types/known/emptypb"
)
type CompetitionService struct {
repo repository.CompetitionRepository
}
func NewCompetitionService(repo repository.CompetitionRepository) *CompetitionService {
return &CompetitionService{repo: repo}
}
func (s *CompetitionService) CreateCompetition(ctx context.Context, req *pb.Competition) (*pb.Competition, error) {
return s.repo.Create(ctx, req)
}
func (s *CompetitionService) GetCompetition(
ctx context.Context,
req *pb.GetCompetitionRequest,
) (*pb.Competition, error) {
id, err := uuid.Parse(req.CompetitionId)
if err != nil {
return nil, err
}
return s.repo.Get(ctx, id)
}
func (s *CompetitionService) EditCompetition(ctx context.Context, req *pb.Competition) (*pb.Competition, error) {
return s.repo.Update(ctx, req)
}
func (s *CompetitionService) DeleteCompetition(
ctx context.Context,
req *pb.DeleteCompetitionRequest,
) (*emptypb.Empty, error) {
id, err := uuid.Parse(req.CompetitionId)
if err != nil {
return nil, err
}
err = s.repo.Delete(ctx, id)
return &emptypb.Empty{}, err
}
func (s *CompetitionService) ListCompetitions(
ctx context.Context,
req *pb.ListCompetitionsRequest,
) (*pb.ListCompetitionsResponse, error) {
opts := repository.ListCompetitionsOptions{
Page: int(req.PageToken),
PageSize: int(req.PageSize),
State: req.State,
IsParticipating: req.IsParticipating,
SearchQuery: req.SearchQuery,
}
competitions, total, err := s.repo.List(ctx, opts)
if err != nil {
return nil, err
}
var nextPageToken int32
if (opts.Page+1)*opts.PageSize < total {
nextPageToken = int32(opts.Page + 1)
}
return &pb.ListCompetitionsResponse{
Competitions: competitions,
TotalCount: int32(total),
NextPageToken: nextPageToken,
}, nil
}
func (s *CompetitionService) ChangeCompetitionState(
ctx context.Context,
req *pb.ChangeCompetitionStateRequest,
) (*pb.Competition, error) {
id, err := uuid.Parse(req.CompetitionId)
if err != nil {
return nil, err
}
return s.repo.ChangeState(ctx, id, req.State)
}