Added AddTask and DeleteTask. Without db tests

This commit is contained in:
OXYgen 2024-11-11 16:41:51 +05:00
parent 568ccea09a
commit 4d40159772
6 changed files with 59 additions and 0 deletions

View file

@ -10,4 +10,6 @@ type ContestHandlers interface {
CreateContest(ctx context.Context, req *contestv1.CreateContestRequest) (*contestv1.CreateContestResponse, error) CreateContest(ctx context.Context, req *contestv1.CreateContestRequest) (*contestv1.CreateContestResponse, error)
ReadContest(ctx context.Context, req *contestv1.ReadContestRequest) (*contestv1.ReadContestResponse, error) ReadContest(ctx context.Context, req *contestv1.ReadContestRequest) (*contestv1.ReadContestResponse, error)
DeleteContest(ctx context.Context, req *contestv1.DeleteContestRequest) (*emptypb.Empty, 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)
} }

View file

@ -49,3 +49,19 @@ func (h *ContestHandlers) DeleteContest(ctx context.Context, req *contestv1.Dele
} }
return &emptypb.Empty{}, nil 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
}

View file

@ -9,4 +9,6 @@ type ContestRepository interface {
CreateContest(ctx context.Context, title string) (int32, error) CreateContest(ctx context.Context, title string) (int32, error)
ReadContestById(ctx context.Context, id int32) (*models.Contest, error) ReadContestById(ctx context.Context, id int32) (*models.Contest, error)
DeleteContest(ctx context.Context, id int32) 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
} }

View file

@ -68,6 +68,35 @@ func (r *ContestRepository) DeleteContest(ctx context.Context, id int32) error {
return nil 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 { func handlePgErr(err error) error {
var pgErr *pgconn.PgError var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) { if !errors.As(err, &pgErr) {

View file

@ -9,4 +9,6 @@ type ContestUseCase interface {
CreateContest(ctx context.Context, title string) (int32, error) CreateContest(ctx context.Context, title string) (int32, error)
ReadContestById(ctx context.Context, id int32) (*models.Contest, error) ReadContestById(ctx context.Context, id int32) (*models.Contest, error)
DeleteContest(ctx context.Context, id int32) 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
} }

View file

@ -29,3 +29,11 @@ func (uc *ContestUseCase) ReadContestById(ctx context.Context, id int32) (*model
func (uc *ContestUseCase) DeleteContest(ctx context.Context, id int32) error { func (uc *ContestUseCase) DeleteContest(ctx context.Context, id int32) error {
return uc.contestRepo.DeleteContest(ctx, id) 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)
}