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