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

@ -12,4 +12,6 @@ type ContestHandlers interface {
DeleteContest(ctx context.Context, req *contestv1.DeleteContestRequest) (*emptypb.Empty, error) DeleteContest(ctx context.Context, req *contestv1.DeleteContestRequest) (*emptypb.Empty, error)
AddTask(ctx context.Context, req *contestv1.AddTaskRequest) (*contestv1.AddTaskResponse, error) AddTask(ctx context.Context, req *contestv1.AddTaskRequest) (*contestv1.AddTaskResponse, error)
DeleteTask(ctx context.Context, req *contestv1.DeleteTaskRequest) (*emptypb.Empty, 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)
} }

View file

@ -65,3 +65,19 @@ func (h *ContestHandlers) DeleteTask(ctx context.Context, req *contestv1.DeleteT
} }
return &emptypb.Empty{}, nil 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
}

View file

@ -11,4 +11,6 @@ type ContestRepository interface {
DeleteContest(ctx context.Context, id int32) error DeleteContest(ctx context.Context, id int32) error
AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error)
DeleteTask(ctx context.Context, taskId 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
} }

View file

@ -97,6 +97,35 @@ func (r *ContestRepository) DeleteTask(ctx context.Context, taskId int32) error
return nil 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 { func handlePgErr(err error) error {
var pgErr *pgconn.PgError var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) { if !errors.As(err, &pgErr) {

View file

@ -11,4 +11,6 @@ type ContestUseCase interface {
DeleteContest(ctx context.Context, id int32) error DeleteContest(ctx context.Context, id int32) error
AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error) AddTask(ctx context.Context, contestId int32, taskId int32) (int32, error)
DeleteTask(ctx context.Context, taskId 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
} }

View file

@ -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 { func (uc *ContestUseCase) DeleteTask(ctx context.Context, taskId int32) error {
return uc.contestRepo.DeleteTask(ctx, taskId) 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)
}