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
|
|
|
|
}
|