ms-tester/internal/problems/delivery/grpc/handlers.go
Vyacheslav1557 be25404852 feat:
2024-10-13 19:01:36 +05:00

59 lines
1.8 KiB
Go

package grpc
import (
"context"
"git.sch9.ru/new_gate/ms-tester/internal/problems"
problemv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/problem/v1"
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)
type problemHandlers struct {
problemv1.UnimplementedProblemServiceServer
problemUC problems.ProblemUseCase
}
func NewProblemHandlers(gserver *grpc.Server, problemUC problems.ProblemUseCase) {
handlers := &problemHandlers{problemUC: problemUC}
problemv1.RegisterProblemServiceServer(gserver, handlers)
}
func (h *problemHandlers) CreateProblem(ctx context.Context, req *problemv1.CreateProblemRequest) (*problemv1.CreateProblemResponse, error) {
id, err := h.problemUC.CreateProblem(ctx, req.GetTitle())
if err != nil {
return nil, err
}
return &problemv1.CreateProblemResponse{Id: id}, nil
}
func (h *problemHandlers) ReadProblem(ctx context.Context, req *problemv1.ReadProblemRequest) (*problemv1.ReadProblemResponse, error) {
problem, err := h.problemUC.ReadProblemById(ctx, req.GetId())
if err != nil {
return nil, err
}
return &problemv1.ReadProblemResponse{
Problem: &problemv1.ReadProblemResponse_Problem{
Id: *problem.Id,
Title: *problem.Title,
Content: *problem.Content,
TimeLimit: *problem.TimeLimit,
MemoryLimit: *problem.MemoryLimit,
TestingStrategy: *problem.TestingStrategy,
TestingOrder: *problem.TestingOrder,
CreatedAt: utils.TimestampP(problem.CreatedAt),
UpdatedAt: utils.TimestampP(problem.UpdatedAt),
},
}, nil
}
func (h *problemHandlers) DeleteProblem(ctx context.Context, req *problemv1.DeleteProblemRequest) (*emptypb.Empty, error) {
err := h.problemUC.DeleteProblem(ctx, req.GetId())
if err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}