diff --git a/internal/contests/repository/pg_repository.go b/internal/contests/repository/pg_repository.go index 2777562..48ff720 100644 --- a/internal/contests/repository/pg_repository.go +++ b/internal/contests/repository/pg_repository.go @@ -68,11 +68,11 @@ 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 (?, ?,COALESCE(SELECT MAX(position) FROM task WHERE contest_id = ? ,0) + 1) RETURNING id" func (r *ContestRepository) AddTask(ctx context.Context, contestId int32, problem_id int32) (int32, error) { query := r.db.Rebind(addTaskQuery) - rows, err := r.db.QueryxContext(ctx, query, problem_id, contestId) + rows, err := r.db.QueryxContext(ctx, query, problem_id, contestId, contestId) if err != nil { return 0, handlePgErr(err) } diff --git a/internal/contests/repository/pg_repository_test.go b/internal/contests/repository/pg_repository_test.go index 2440713..be2cccd 100644 --- a/internal/contests/repository/pg_repository_test.go +++ b/internal/contests/repository/pg_repository_test.go @@ -75,7 +75,7 @@ func TestContestRepository_AddTask(t *testing.T) { rows := sqlmock.NewRows([]string{"id"}).AddRow(1) - mock.ExpectQuery(sqlxDB.Rebind(addTaskQuery)).WithArgs(taskId, contestId).WillReturnRows(rows) + mock.ExpectQuery(sqlxDB.Rebind(addTaskQuery)).WithArgs(taskId, contestId, contestId).WillReturnRows(rows) id, err := contestRepo.AddTask(context.Background(), contestId, taskId)