Files
Datarush/internal/user/service/service.go
T
2025-12-19 21:04:12 +03:00

68 lines
1.8 KiB
Go

package service
import (
"context"
"errors"
"google.golang.org/protobuf/types/known/emptypb"
"datarush/internal/user/middleware"
pb "datarush/pkg/api/user"
)
type UserRepository interface {
GetProfile(ctx context.Context, userID string) (*pb.User, error)
RegisterForCompetition(ctx context.Context, userID, competitionID string) error
UnregisterFromCompetition(ctx context.Context, userID, competitionID string) error
ListUserCompetitions(ctx context.Context, userID string) ([]string, error)
}
type UserService struct {
repo UserRepository
}
func NewUserService(repo UserRepository) *UserService {
return &UserService{repo: repo}
}
func (s *UserService) GetProfile(ctx context.Context, req *pb.GetProfileRequest) (*pb.User, error) {
return s.repo.GetProfile(ctx, req.UserId)
}
func (s *UserService) RegisterForCompetition(
ctx context.Context,
req *pb.RegisterForCompetitionRequest,
) (*emptypb.Empty, error) {
userID, ok := ctx.Value(middleware.UserIDKey{}).(string)
if !ok {
return nil, errors.New("user ID not found in context")
}
err := s.repo.RegisterForCompetition(ctx, userID, req.CompetitionId)
return &emptypb.Empty{}, err
}
func (s *UserService) UnregisterFromCompetition(
ctx context.Context,
req *pb.UnregisterFromCompetitionRequest,
) (*emptypb.Empty, error) {
userID, ok := ctx.Value(middleware.UserIDKey{}).(string)
if !ok {
return nil, errors.New("user ID not found in context")
}
err := s.repo.UnregisterFromCompetition(ctx, userID, req.CompetitionId)
return &emptypb.Empty{}, err
}
func (s *UserService) ListUserCompetitions(
ctx context.Context,
req *pb.ListUserCompetitionsRequest,
) (*pb.ListUserCompetitionsResponse, error) {
competitionIDs, err := s.repo.ListUserCompetitions(ctx, req.UserId)
if err != nil {
return nil, err
}
return &pb.ListUserCompetitionsResponse{CompetitionIds: competitionIDs}, nil
}