From 4d40159772923282973b8591a96c52d93fa6b751 Mon Sep 17 00:00:00 2001 From: OXYgen Date: Mon, 11 Nov 2024 16:41:51 +0500 Subject: [PATCH] Added AddTask and DeleteTask. Without db tests --- internal/contests/delivery.go | 2 ++ internal/contests/delivery/grpc/handlers.go | 16 ++++++++++ internal/contests/pg_repository.go | 2 ++ internal/contests/repository/pg_repository.go | 29 +++++++++++++++++++ internal/contests/usecase.go | 2 ++ internal/contests/usecase/usecase.go | 8 +++++ 6 files changed, 59 insertions(+) diff --git a/internal/contests/delivery.go b/internal/contests/delivery.go index bbb81f3..4314ccd 100644 --- a/internal/contests/delivery.go +++ b/internal/contests/delivery.go @@ -10,4 +10,6 @@ type ContestHandlers interface { CreateContest(ctx context.Context, req *contestv1.CreateContestRequest) (*contestv1.CreateContestResponse, error) ReadContest(ctx context.Context, req *contestv1.ReadContestRequest) (*contestv1.ReadContestResponse, error) 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) } diff --git a/internal/contests/delivery/grpc/handlers.go b/internal/contests/delivery/grpc/handlers.go index 67141e4..ceb381f 100644 --- a/internal/contests/delivery/grpc/handlers.go +++ b/internal/contests/delivery/grpc/handlers.go @@ -49,3 +49,19 @@ func (h *ContestHandlers) DeleteContest(ctx context.Context, req *contestv1.Dele } return &emptypb.Empty{}, nil } + +func (h *ContestHandlers) AddTask(ctx context.Context, req *contestv1.AddTaskRequest) (*contestv1.AddTaskResponse, error) { + id, err := h.contestUC.AddTask(ctx, req.GetContestId(), req.GetProblemId()) + if err != nil { + return nil, err + } + return &contestv1.AddTaskResponse{Id: id}, nil +} + +func (h *ContestHandlers) DeleteTask(ctx context.Context, req *contestv1.DeleteTaskRequest) (*emptypb.Empty, error) { + err := h.contestUC.DeleteTask(ctx, req.GetTaskId()) + 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 1dc1368..ccfdc24 100644 --- a/internal/contests/pg_repository.go +++ b/internal/contests/pg_repository.go @@ -9,4 +9,6 @@ type ContestRepository interface { CreateContest(ctx context.Context, title string) (int32, error) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) DeleteContest(ctx context.Context, id int32) error + AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) + DeleteTask(ctx context.Context, taskId int32) error } diff --git a/internal/contests/repository/pg_repository.go b/internal/contests/repository/pg_repository.go index e3f1c82..abd9013 100644 --- a/internal/contests/repository/pg_repository.go +++ b/internal/contests/repository/pg_repository.go @@ -68,6 +68,35 @@ 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" + +func (r *ContestRepository) AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) { + query := r.db.Rebind(addTaskQuery) + rows, err := r.db.QueryxContext(ctx, query, taskId, contestId) + if err != nil { + return 0, handlePgErr(err) + } + defer rows.Close() + var id int32 + rows.Next() + err = rows.Scan(&id) + if err != nil { + return 0, handlePgErr(err) + } + return id, nil +} + +const deleteTaskQuery = "DELETE FROM task WHERE id=?" + +func (r *ContestRepository) DeleteTask(ctx context.Context, taskId int32) error { + query := r.db.Rebind(deleteTaskQuery) + _, err := r.db.ExecContext(ctx, query, taskId) + 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 c076721..18d230a 100644 --- a/internal/contests/usecase.go +++ b/internal/contests/usecase.go @@ -9,4 +9,6 @@ type ContestUseCase interface { CreateContest(ctx context.Context, title string) (int32, error) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) DeleteContest(ctx context.Context, id int32) error + AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) + DeleteTask(ctx context.Context, taskId int32) error } diff --git a/internal/contests/usecase/usecase.go b/internal/contests/usecase/usecase.go index 5437e17..5da3654 100644 --- a/internal/contests/usecase/usecase.go +++ b/internal/contests/usecase/usecase.go @@ -29,3 +29,11 @@ func (uc *ContestUseCase) ReadContestById(ctx context.Context, id int32) (*model func (uc *ContestUseCase) DeleteContest(ctx context.Context, id int32) error { return uc.contestRepo.DeleteContest(ctx, id) } + +func (uc *ContestUseCase) AddTask(ctx context.Context, contestId int32, taskId int32) (id int32, err error) { + return uc.contestRepo.AddTask(ctx, contestId, taskId) +} + +func (uc *ContestUseCase) DeleteTask(ctx context.Context, taskId int32) error { + return uc.contestRepo.DeleteTask(ctx, taskId) +}