Fixed bug in AddTask, fixed finding new task position
This commit is contained in:
parent
e201b12db5
commit
a1ebd51404
2 changed files with 3 additions and 3 deletions
|
@ -68,11 +68,11 @@ 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"
|
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) {
|
func (r *ContestRepository) AddTask(ctx context.Context, contestId int32, problem_id int32) (int32, error) {
|
||||||
query := r.db.Rebind(addTaskQuery)
|
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 {
|
if err != nil {
|
||||||
return 0, handlePgErr(err)
|
return 0, handlePgErr(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ func TestContestRepository_AddTask(t *testing.T) {
|
||||||
|
|
||||||
rows := sqlmock.NewRows([]string{"id"}).AddRow(1)
|
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)
|
id, err := contestRepo.AddTask(context.Background(), contestId, taskId)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue