Added AddParticipant and DeleteParticipant

This commit is contained in:
OXYgen 2024-11-14 17:09:52 +05:00
parent 4d40159772
commit f7dd1bc806
6 changed files with 60 additions and 1 deletions

View file

@ -68,7 +68,7 @@ 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 (?, ?,(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)
@ -97,6 +97,35 @@ func (r *ContestRepository) DeleteTask(ctx context.Context, taskId int32) error
return nil
}
const addParticipantQuery = "INSERT INTO participant (user_id ,contest_id, name) VALUES (?, ?, ?) RETURNING id"
func (r *ContestRepository) AddParticipant(ctx context.Context, contestId int32, userId int32) (int32, error) {
query := r.db.Rebind(addParticipantQuery)
name := ""
rows, err := r.db.QueryxContext(ctx, query, contestId, userId, name)
if err != nil {
return 0, handlePgErr(err)
}
defer rows.Close()
var id int32
err = rows.Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
const deleteParticipantQuery = "DELETE FROM participant WHERE id=?"
func (r *ContestRepository) DeleteParticipant(ctx context.Context, participantId int32) error {
query := r.db.Rebind(deleteParticipantQuery)
_, err := r.db.ExecContext(ctx, query, participantId)
if err != nil {
return handlePgErr(err)
}
return nil
}
func handlePgErr(err error) error {
var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) {