add user service
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"datarush/internal/user/middleware"
|
||||
pb "datarush/pkg/api/user"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user