style: reformatted all files
This commit is contained in:
@@ -3,15 +3,19 @@ package grpc
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "datarush/pkg/api/user"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
pb "datarush/pkg/api/user"
|
||||
)
|
||||
|
||||
type UserService interface {
|
||||
GetProfile(ctx context.Context, req *pb.GetProfileRequest) (*pb.User, error)
|
||||
RegisterForCompetition(ctx context.Context, req *pb.RegisterForCompetitionRequest) (*emptypb.Empty, error)
|
||||
UnregisterFromCompetition(ctx context.Context, req *pb.UnregisterFromCompetitionRequest) (*emptypb.Empty, error)
|
||||
ListUserCompetitions(ctx context.Context, req *pb.ListUserCompetitionsRequest) (*pb.ListUserCompetitionsResponse, error)
|
||||
ListUserCompetitions(
|
||||
ctx context.Context,
|
||||
req *pb.ListUserCompetitionsRequest,
|
||||
) (*pb.ListUserCompetitionsResponse, error)
|
||||
}
|
||||
|
||||
type UserHandler struct {
|
||||
@@ -27,14 +31,23 @@ func (h *UserHandler) GetProfile(ctx context.Context, req *pb.GetProfileRequest)
|
||||
return h.service.GetProfile(ctx, req)
|
||||
}
|
||||
|
||||
func (h *UserHandler) RegisterForCompetition(ctx context.Context, req *pb.RegisterForCompetitionRequest) (*emptypb.Empty, error) {
|
||||
func (h *UserHandler) RegisterForCompetition(
|
||||
ctx context.Context,
|
||||
req *pb.RegisterForCompetitionRequest,
|
||||
) (*emptypb.Empty, error) {
|
||||
return h.service.RegisterForCompetition(ctx, req)
|
||||
}
|
||||
|
||||
func (h *UserHandler) UnregisterFromCompetition(ctx context.Context, req *pb.UnregisterFromCompetitionRequest) (*emptypb.Empty, error) {
|
||||
func (h *UserHandler) UnregisterFromCompetition(
|
||||
ctx context.Context,
|
||||
req *pb.UnregisterFromCompetitionRequest,
|
||||
) (*emptypb.Empty, error) {
|
||||
return h.service.UnregisterFromCompetition(ctx, req)
|
||||
}
|
||||
|
||||
func (h *UserHandler) ListUserCompetitions(ctx context.Context, req *pb.ListUserCompetitionsRequest) (*pb.ListUserCompetitionsResponse, error) {
|
||||
func (h *UserHandler) ListUserCompetitions(
|
||||
ctx context.Context,
|
||||
req *pb.ListUserCompetitionsRequest,
|
||||
) (*pb.ListUserCompetitionsResponse, error) {
|
||||
return h.service.ListUserCompetitions(ctx, req)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,4 +49,4 @@ func AuthInterceptor(jwtSecret string) grpc.UnaryServerInterceptor {
|
||||
|
||||
return nil, status.Errorf(codes.Unauthenticated, "invalid token")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@ package postgres
|
||||
import (
|
||||
"context"
|
||||
|
||||
"datarush/pkg/api/user"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"datarush/pkg/api/user"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
@@ -17,22 +18,42 @@ func NewUserRepository(db *sqlx.DB) *UserRepository {
|
||||
|
||||
func (r *UserRepository) GetProfile(ctx context.Context, userID string) (*user.User, error) {
|
||||
var u user.User
|
||||
err := r.db.GetContext(ctx, &u, "SELECT id, username, email, full_name, avatar_url FROM users WHERE id = $1", userID)
|
||||
err := r.db.GetContext(
|
||||
ctx,
|
||||
&u,
|
||||
"SELECT id, username, email, full_name, avatar_url FROM users WHERE id = $1",
|
||||
userID,
|
||||
)
|
||||
return &u, err
|
||||
}
|
||||
|
||||
func (r *UserRepository) RegisterForCompetition(ctx context.Context, userID, competitionID string) error {
|
||||
_, err := r.db.ExecContext(ctx, "INSERT INTO user_competitions (user_id, competition_id) VALUES ($1, $2)", userID, competitionID)
|
||||
_, err := r.db.ExecContext(
|
||||
ctx,
|
||||
"INSERT INTO user_competitions (user_id, competition_id) VALUES ($1, $2)",
|
||||
userID,
|
||||
competitionID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *UserRepository) UnregisterFromCompetition(ctx context.Context, userID, competitionID string) error {
|
||||
_, err := r.db.ExecContext(ctx, "DELETE FROM user_competitions WHERE user_id = $1 AND competition_id = $2", userID, competitionID)
|
||||
_, err := r.db.ExecContext(
|
||||
ctx,
|
||||
"DELETE FROM user_competitions WHERE user_id = $1 AND competition_id = $2",
|
||||
userID,
|
||||
competitionID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *UserRepository) ListUserCompetitions(ctx context.Context, userID string) ([]string, error) {
|
||||
var competitionIDs []string
|
||||
err := r.db.SelectContext(ctx, &competitionIDs, "SELECT competition_id FROM user_competitions WHERE user_id = $1", userID)
|
||||
err := r.db.SelectContext(
|
||||
ctx,
|
||||
&competitionIDs,
|
||||
"SELECT competition_id FROM user_competitions WHERE user_id = $1",
|
||||
userID,
|
||||
)
|
||||
return competitionIDs, err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
"datarush/internal/user/middleware"
|
||||
pb "datarush/pkg/api/user"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
type UserRepository interface {
|
||||
@@ -28,7 +29,10 @@ func (s *UserService) GetProfile(ctx context.Context, req *pb.GetProfileRequest)
|
||||
return s.repo.GetProfile(ctx, req.UserId)
|
||||
}
|
||||
|
||||
func (s *UserService) RegisterForCompetition(ctx context.Context, req *pb.RegisterForCompetitionRequest) (*emptypb.Empty, error) {
|
||||
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")
|
||||
@@ -38,7 +42,10 @@ func (s *UserService) RegisterForCompetition(ctx context.Context, req *pb.Regist
|
||||
return &emptypb.Empty{}, err
|
||||
}
|
||||
|
||||
func (s *UserService) UnregisterFromCompetition(ctx context.Context, req *pb.UnregisterFromCompetitionRequest) (*emptypb.Empty, error) {
|
||||
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")
|
||||
@@ -48,10 +55,13 @@ func (s *UserService) UnregisterFromCompetition(ctx context.Context, req *pb.Unr
|
||||
return &emptypb.Empty{}, err
|
||||
}
|
||||
|
||||
func (s *UserService) ListUserCompetitions(ctx context.Context, req *pb.ListUserCompetitionsRequest) (*pb.ListUserCompetitionsResponse, error) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"datarush/internal/user/service/mocks"
|
||||
pb "datarush/pkg/api/user"
|
||||
|
||||
"go.uber.org/mock/gomock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
func TestUserService(t *testing.T) {
|
||||
@@ -98,4 +98,4 @@ func TestUserService(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedError, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user