rewrite competition service: remove http related things
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user