ms-tester/internal/problems/delivery/grpc/handlers.go
2024-10-17 00:34:43 +05:00

62 lines
1.9 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,
Legend: *problem.Legend,
InputFormat: *problem.InputFormat,
OutputFormat: *problem.OutputFormat,
Notes: *problem.Notes,
Tutorial: *problem.Tutorial,
LatexSummary: *problem.LatexSummary,
TimeLimit: *problem.TimeLimit,
MemoryLimit: *problem.MemoryLimit,
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
}