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

68 lines
2.1 KiB
Go

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