ms-tester/internal/contests/delivery/grpc/handlers.go

84 lines
2.7 KiB
Go
Raw Normal View History

2024-10-09 18:55:16 +00:00
package grpc
2024-10-13 14:01:36 +00:00
import (
"context"
"git.sch9.ru/new_gate/ms-tester/internal/contests"
contestv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/contest/v1"
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)
type ContestHandlers struct {
contestv1.UnimplementedContestServiceServer
contestUC contests.ContestUseCase
}
func NewContestHandlers(gserver *grpc.Server, contestUC contests.ContestUseCase) {
handlers := &ContestHandlers{contestUC: contestUC}
contestv1.RegisterContestServiceServer(gserver, handlers)
}
func (h *ContestHandlers) CreateContest(ctx context.Context, req *contestv1.CreateContestRequest) (*contestv1.CreateContestResponse, error) {
id, err := h.contestUC.CreateContest(ctx, req.GetTitle())
if err != nil {
return nil, err
}
return &contestv1.CreateContestResponse{Id: id}, nil
}
func (h *ContestHandlers) ReadContest(ctx context.Context, req *contestv1.ReadContestRequest) (*contestv1.ReadContestResponse, error) {
contest, err := h.contestUC.ReadContestById(ctx, req.GetId())
if err != nil {
return nil, err
}
return &contestv1.ReadContestResponse{Contest: &contestv1.ReadContestResponse_Contest{
Id: *contest.Id,
Title: *contest.Title,
CreatedAt: utils.TimestampP(contest.CreatedAt),
UpdatedAt: utils.TimestampP(contest.UpdatedAt),
}}, nil
}
func (h *ContestHandlers) DeleteContest(ctx context.Context, req *contestv1.DeleteContestRequest) (*emptypb.Empty, error) {
err := h.contestUC.DeleteContest(ctx, req.GetId())
if err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}
func (h *ContestHandlers) AddTask(ctx context.Context, req *contestv1.AddTaskRequest) (*contestv1.AddTaskResponse, error) {
id, err := h.contestUC.AddTask(ctx, req.GetContestId(), req.GetProblemId())
if err != nil {
return nil, err
}
return &contestv1.AddTaskResponse{Id: id}, nil
}
func (h *ContestHandlers) DeleteTask(ctx context.Context, req *contestv1.DeleteTaskRequest) (*emptypb.Empty, error) {
err := h.contestUC.DeleteTask(ctx, req.GetTaskId())
if err != nil {
return nil, err
}
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
}