Added AddParticipant and DeleteParticipant
This commit is contained in:
parent
4d40159772
commit
f7dd1bc806
6 changed files with 60 additions and 1 deletions
|
@ -12,4 +12,6 @@ type ContestHandlers interface {
|
|||
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)
|
||||
AddParticipant(ctx context.Context, req *contestv1.AddParticipantRequest) (*contestv1.AddParticipantResponse, error)
|
||||
DeleteParticipant(ctx context.Context, req *contestv1.DeleteParticipantRequest) (*emptypb.Empty, error)
|
||||
}
|
||||
|
|
|
@ -65,3 +65,19 @@ func (h *ContestHandlers) DeleteTask(ctx context.Context, req *contestv1.DeleteT
|
|||
}
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (h *ContestHandlers) AddParticipant(ctx context.Context, req *contestv1.AddParticipantRequest) (*contestv1.AddParticipantResponse, error) {
|
||||
id, err := h.contestUC.AddParticipant(ctx, req.GetContestId(), req.GetUserId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &contestv1.AddParticipantResponse{Id: id}, nil
|
||||
}
|
||||
|
||||
func (h *ContestHandlers) DeleteParticipant(ctx context.Context, req *contestv1.DeleteParticipantRequest) (*emptypb.Empty, error) {
|
||||
err := h.contestUC.DeleteParticipant(ctx, req.GetParticipantId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
|
|
@ -11,4 +11,6 @@ type ContestRepository interface {
|
|||
DeleteContest(ctx context.Context, id int32) error
|
||||
AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error)
|
||||
DeleteTask(ctx context.Context, taskId int32) error
|
||||
AddParticipant(ctx context.Context, contestId int32, userId int32) (int32, error)
|
||||
DeleteParticipant(ctx context.Context, participantId int32) error
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -11,4 +11,6 @@ type ContestUseCase interface {
|
|||
DeleteContest(ctx context.Context, id int32) error
|
||||
AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error)
|
||||
DeleteTask(ctx context.Context, taskId int32) error
|
||||
AddParticipant(ctx context.Context, contestId int32, userId int32) (int32, error)
|
||||
DeleteParticipant(ctx context.Context, participantId int32) error
|
||||
}
|
||||
|
|
|
@ -37,3 +37,11 @@ func (uc *ContestUseCase) AddTask(ctx context.Context, contestId int32, taskId i
|
|||
func (uc *ContestUseCase) DeleteTask(ctx context.Context, taskId int32) error {
|
||||
return uc.contestRepo.DeleteTask(ctx, taskId)
|
||||
}
|
||||
|
||||
func (uc *ContestUseCase) AddParticipant(ctx context.Context, contestId int32, userId int32) (id int32, err error) {
|
||||
return uc.contestRepo.AddParticipant(ctx, contestId, userId)
|
||||
}
|
||||
|
||||
func (uc *ContestUseCase) DeleteParticipant(ctx context.Context, participantId int32) error {
|
||||
return uc.contestRepo.DeleteParticipant(ctx, participantId)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue