lint competition service
This commit is contained in:
@@ -81,4 +81,4 @@ func (c Config) BuildPostgresConnStr() string {
|
||||
func (c Config) BuildPostgresDSN() string {
|
||||
return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable",
|
||||
c.DBUser, c.DBPassword, net.JoinHostPort(c.DBHost, strconv.Itoa(c.DBPort)), c.DBName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@ package grpc
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "datarush/pkg/api/competition"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
pb "datarush/pkg/api/competition"
|
||||
)
|
||||
|
||||
type CompetitionService interface {
|
||||
@@ -29,7 +30,10 @@ func (h *CompetitionHandler) CreateCompetition(ctx context.Context, req *pb.Comp
|
||||
return h.service.CreateCompetition(ctx, req)
|
||||
}
|
||||
|
||||
func (h *CompetitionHandler) GetCompetition(ctx context.Context, req *pb.GetCompetitionRequest) (*pb.Competition, error) {
|
||||
func (h *CompetitionHandler) GetCompetition(
|
||||
ctx context.Context,
|
||||
req *pb.GetCompetitionRequest,
|
||||
) (*pb.Competition, error) {
|
||||
return h.service.GetCompetition(ctx, req)
|
||||
}
|
||||
|
||||
@@ -37,14 +41,23 @@ func (h *CompetitionHandler) EditCompetition(ctx context.Context, req *pb.Compet
|
||||
return h.service.EditCompetition(ctx, req)
|
||||
}
|
||||
|
||||
func (h *CompetitionHandler) DeleteCompetition(ctx context.Context, req *pb.DeleteCompetitionRequest) (*emptypb.Empty, error) {
|
||||
func (h *CompetitionHandler) DeleteCompetition(
|
||||
ctx context.Context,
|
||||
req *pb.DeleteCompetitionRequest,
|
||||
) (*emptypb.Empty, error) {
|
||||
return h.service.DeleteCompetition(ctx, req)
|
||||
}
|
||||
|
||||
func (h *CompetitionHandler) ListCompetitions(ctx context.Context, req *pb.ListCompetitionsRequest) (*pb.ListCompetitionsResponse, error) {
|
||||
func (h *CompetitionHandler) ListCompetitions(
|
||||
ctx context.Context,
|
||||
req *pb.ListCompetitionsRequest,
|
||||
) (*pb.ListCompetitionsResponse, error) {
|
||||
return h.service.ListCompetitions(ctx, req)
|
||||
}
|
||||
|
||||
func (h *CompetitionHandler) ChangeCompetitionState(ctx context.Context, req *pb.ChangeCompetitionStateRequest) (*pb.Competition, error) {
|
||||
func (h *CompetitionHandler) ChangeCompetitionState(
|
||||
ctx context.Context,
|
||||
req *pb.ChangeCompetitionStateRequest,
|
||||
) (*pb.Competition, error) {
|
||||
return h.service.ChangeCompetitionState(ctx, req)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,4 +64,4 @@ func (i *AuthInterceptor) Unary() grpc.UnaryServerInterceptor {
|
||||
|
||||
return handler(newCtx, req)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,4 @@ type CompetitionRepository interface {
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
List(ctx context.Context, opts ListCompetitionsOptions) ([]*pb.Competition, int, error)
|
||||
ChangeState(ctx context.Context, id uuid.UUID, state pb.CompetitionState) (*pb.Competition, error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,11 @@ type CompetitionRepository struct {
|
||||
cacheEnabled bool
|
||||
}
|
||||
|
||||
func NewCompetitionRepository(db *sqlx.DB, redisClient *redis.Client, cacheEnabled bool) repository.CompetitionRepository {
|
||||
func NewCompetitionRepository(
|
||||
db *sqlx.DB,
|
||||
redisClient *redis.Client,
|
||||
cacheEnabled bool,
|
||||
) repository.CompetitionRepository {
|
||||
return &CompetitionRepository{
|
||||
db: db,
|
||||
redisClient: redisClient,
|
||||
@@ -140,7 +144,10 @@ func (r *CompetitionRepository) Delete(ctx context.Context, id uuid.UUID) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCompetitionsOptions) ([]*pb.Competition, int, error) {
|
||||
func (r *CompetitionRepository) List(
|
||||
ctx context.Context,
|
||||
opts repository.ListCompetitionsOptions,
|
||||
) ([]*pb.Competition, int, error) {
|
||||
var args []interface{}
|
||||
var whereClauses []string
|
||||
argId := 1
|
||||
@@ -163,7 +170,10 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo
|
||||
}
|
||||
|
||||
if *opts.IsParticipating {
|
||||
whereClauses = append(whereClauses, fmt.Sprintf("id IN (SELECT competition_id FROM user_competitions WHERE user_id = $%d)", argId))
|
||||
whereClauses = append(
|
||||
whereClauses,
|
||||
fmt.Sprintf("id IN (SELECT competition_id FROM user_competitions WHERE user_id = $%d)", argId),
|
||||
)
|
||||
} else {
|
||||
whereClauses = append(whereClauses, fmt.Sprintf("id NOT IN (SELECT competition_id FROM user_competitions WHERE user_id = $%d)", argId))
|
||||
}
|
||||
@@ -182,7 +192,12 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`SELECT id, state, title, description, image_url, start_time, end_time, type, participation_type, created_at, updated_at FROM competitions %s ORDER BY created_at DESC LIMIT $%d OFFSET $%d`, where, argId, argId+1)
|
||||
query := fmt.Sprintf(
|
||||
`SELECT id, state, title, description, image_url, start_time, end_time, type, participation_type, created_at, updated_at FROM competitions %s ORDER BY created_at DESC LIMIT $%d OFFSET $%d`,
|
||||
where,
|
||||
argId,
|
||||
argId+1,
|
||||
)
|
||||
args = append(args, opts.PageSize, (opts.Page-1)*opts.PageSize)
|
||||
|
||||
var competitions []*pb.Competition
|
||||
@@ -194,7 +209,11 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo
|
||||
return competitions, total, nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) ChangeState(ctx context.Context, id uuid.UUID, state pb.CompetitionState) (*pb.Competition, error) {
|
||||
func (r *CompetitionRepository) ChangeState(
|
||||
ctx context.Context,
|
||||
id uuid.UUID,
|
||||
state pb.CompetitionState,
|
||||
) (*pb.Competition, error) {
|
||||
query := `UPDATE competitions SET state = $1, updated_at = $2 WHERE id = $3 RETURNING updated_at`
|
||||
now := time.Now()
|
||||
var competition pb.Competition
|
||||
@@ -208,4 +227,4 @@ func (r *CompetitionRepository) ChangeState(ctx context.Context, id uuid.UUID, s
|
||||
}
|
||||
|
||||
return r.Get(ctx, id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ const (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
grpcServer *grpc.Server
|
||||
grpcServer *grpc.Server
|
||||
config *config.Config
|
||||
db *sqlx.DB
|
||||
redisClient *redis.Client
|
||||
|
||||
@@ -22,7 +22,10 @@ func (s *CompetitionService) CreateCompetition(ctx context.Context, req *pb.Comp
|
||||
return s.repo.Create(ctx, req)
|
||||
}
|
||||
|
||||
func (s *CompetitionService) GetCompetition(ctx context.Context, req *pb.GetCompetitionRequest) (*pb.Competition, error) {
|
||||
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
|
||||
@@ -34,7 +37,10 @@ func (s *CompetitionService) EditCompetition(ctx context.Context, req *pb.Compet
|
||||
return s.repo.Update(ctx, req)
|
||||
}
|
||||
|
||||
func (s *CompetitionService) DeleteCompetition(ctx context.Context, req *pb.DeleteCompetitionRequest) (*emptypb.Empty, error) {
|
||||
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
|
||||
@@ -43,7 +49,10 @@ func (s *CompetitionService) DeleteCompetition(ctx context.Context, req *pb.Dele
|
||||
return &emptypb.Empty{}, err
|
||||
}
|
||||
|
||||
func (s *CompetitionService) ListCompetitions(ctx context.Context, req *pb.ListCompetitionsRequest) (*pb.ListCompetitionsResponse, error) {
|
||||
func (s *CompetitionService) ListCompetitions(
|
||||
ctx context.Context,
|
||||
req *pb.ListCompetitionsRequest,
|
||||
) (*pb.ListCompetitionsResponse, error) {
|
||||
opts := repository.ListCompetitionsOptions{
|
||||
Page: int(req.PageToken),
|
||||
PageSize: int(req.PageSize),
|
||||
@@ -69,7 +78,10 @@ func (s *CompetitionService) ListCompetitions(ctx context.Context, req *pb.ListC
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *CompetitionService) ChangeCompetitionState(ctx context.Context, req *pb.ChangeCompetitionStateRequest) (*pb.Competition, error) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user