Fixed bug in AddTask, fixed finding new task position

This commit is contained in:
OXYgen 2024-11-15 13:39:22 +05:00
parent e201b12db5
commit a1ebd51404
2 changed files with 3 additions and 3 deletions

View file

@ -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)
}

View file

@ -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)