From f7dd1bc8068b81ad6b0ec4aa9031e43e9a5afda6 Mon Sep 17 00:00:00 2001 From: OXYgen Date: Thu, 14 Nov 2024 17:09:52 +0500 Subject: [PATCH] Added AddParticipant and DeleteParticipant --- internal/contests/delivery.go | 2 ++ internal/contests/delivery/grpc/handlers.go | 16 ++++++++++ internal/contests/pg_repository.go | 2 ++ internal/contests/repository/pg_repository.go | 31 ++++++++++++++++++- internal/contests/usecase.go | 2 ++ internal/contests/usecase/usecase.go | 8 +++++ 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/internal/contests/delivery.go b/internal/contests/delivery.go index 4314ccd..874d361 100644 --- a/internal/contests/delivery.go +++ b/internal/contests/delivery.go @@ -12,4 +12,6 @@ type ContestHandlers interface { DeleteContest(ctx context.Context, req *contestv1.DeleteContestRequest) (*emptypb.Empty, error) AddTask(ctx context.Context, req *contestv1.AddTaskRequest) (*contestv1.AddTaskResponse, error) DeleteTask(ctx context.Context, req *contestv1.DeleteTaskRequest) (*emptypb.Empty, error) + AddParticipant(ctx context.Context, req *contestv1.AddParticipantRequest) (*contestv1.AddParticipantResponse, error) + DeleteParticipant(ctx context.Context, req *contestv1.DeleteParticipantRequest) (*emptypb.Empty, error) } diff --git a/internal/contests/delivery/grpc/handlers.go b/internal/contests/delivery/grpc/handlers.go index ceb381f..36047a4 100644 --- a/internal/contests/delivery/grpc/handlers.go +++ b/internal/contests/delivery/grpc/handlers.go @@ -65,3 +65,19 @@ func (h *ContestHandlers) DeleteTask(ctx context.Context, req *contestv1.DeleteT } return &emptypb.Empty{}, nil } + +func (h *ContestHandlers) AddParticipant(ctx context.Context, req *contestv1.AddParticipantRequest) (*contestv1.AddParticipantResponse, error) { + id, err := h.contestUC.AddParticipant(ctx, req.GetContestId(), req.GetUserId()) + if err != nil { + return nil, err + } + return &contestv1.AddParticipantResponse{Id: id}, nil +} + +func (h *ContestHandlers) DeleteParticipant(ctx context.Context, req *contestv1.DeleteParticipantRequest) (*emptypb.Empty, error) { + err := h.contestUC.DeleteParticipant(ctx, req.GetParticipantId()) + if err != nil { + return nil, err + } + return &emptypb.Empty{}, nil +} diff --git a/internal/contests/pg_repository.go b/internal/contests/pg_repository.go index ccfdc24..0443e32 100644 --- a/internal/contests/pg_repository.go +++ b/internal/contests/pg_repository.go @@ -11,4 +11,6 @@ type ContestRepository interface { DeleteContest(ctx context.Context, id int32) error AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) DeleteTask(ctx context.Context, taskId int32) error + AddParticipant(ctx context.Context, contestId int32, userId int32) (int32, error) + DeleteParticipant(ctx context.Context, participantId int32) error } diff --git a/internal/contests/repository/pg_repository.go b/internal/contests/repository/pg_repository.go index abd9013..8d28144 100644 --- a/internal/contests/repository/pg_repository.go +++ b/internal/contests/repository/pg_repository.go @@ -68,7 +68,7 @@ func (r *ContestRepository) DeleteContest(ctx context.Context, id int32) error { return nil } -const addTaskQuery = "INSERT INTO task (problem_id, contest_id,position) VALUES (?, ?,(SELECT COALESCE(MAX(position),0) + 1 FROM task) ) RETURNING id" +const addTaskQuery = "INSERT INTO task (problem_id, contest_id,position) VALUES (?, ?,(SELECT COALESCE(MAX(position),0) + 1 FROM task)) RETURNING id" func (r *ContestRepository) AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) { query := r.db.Rebind(addTaskQuery) @@ -97,6 +97,35 @@ func (r *ContestRepository) DeleteTask(ctx context.Context, taskId int32) error return nil } +const addParticipantQuery = "INSERT INTO participant (user_id ,contest_id, name) VALUES (?, ?, ?) RETURNING id" + +func (r *ContestRepository) AddParticipant(ctx context.Context, contestId int32, userId int32) (int32, error) { + query := r.db.Rebind(addParticipantQuery) + name := "" + rows, err := r.db.QueryxContext(ctx, query, contestId, userId, name) + if err != nil { + return 0, handlePgErr(err) + } + defer rows.Close() + var id int32 + err = rows.Scan(&id) + if err != nil { + return 0, err + } + return id, nil +} + +const deleteParticipantQuery = "DELETE FROM participant WHERE id=?" + +func (r *ContestRepository) DeleteParticipant(ctx context.Context, participantId int32) error { + query := r.db.Rebind(deleteParticipantQuery) + _, err := r.db.ExecContext(ctx, query, participantId) + if err != nil { + return handlePgErr(err) + } + return nil +} + func handlePgErr(err error) error { var pgErr *pgconn.PgError if !errors.As(err, &pgErr) { diff --git a/internal/contests/usecase.go b/internal/contests/usecase.go index 18d230a..7892518 100644 --- a/internal/contests/usecase.go +++ b/internal/contests/usecase.go @@ -11,4 +11,6 @@ type ContestUseCase interface { DeleteContest(ctx context.Context, id int32) error AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) DeleteTask(ctx context.Context, taskId int32) error + AddParticipant(ctx context.Context, contestId int32, userId int32) (int32, error) + DeleteParticipant(ctx context.Context, participantId int32) error } diff --git a/internal/contests/usecase/usecase.go b/internal/contests/usecase/usecase.go index 5da3654..633bab3 100644 --- a/internal/contests/usecase/usecase.go +++ b/internal/contests/usecase/usecase.go @@ -37,3 +37,11 @@ func (uc *ContestUseCase) AddTask(ctx context.Context, contestId int32, taskId i func (uc *ContestUseCase) DeleteTask(ctx context.Context, taskId int32) error { return uc.contestRepo.DeleteTask(ctx, taskId) } + +func (uc *ContestUseCase) AddParticipant(ctx context.Context, contestId int32, userId int32) (id int32, err error) { + return uc.contestRepo.AddParticipant(ctx, contestId, userId) +} + +func (uc *ContestUseCase) DeleteParticipant(ctx context.Context, participantId int32) error { + return uc.contestRepo.DeleteParticipant(ctx, participantId) +}