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

59 lines
1.8 KiB
Go
Raw Normal View History

2024-10-09 18:55:16 +00:00
package grpc
2024-08-18 00:07:31 +00:00
import (
"context"
2024-10-09 18:55:16 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/problems"
2024-08-18 00:07:31 +00:00
problemv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/problem/v1"
2024-10-09 18:55:16 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
2024-10-13 14:01:36 +00:00
"google.golang.org/grpc"
2024-08-18 00:07:31 +00:00
"google.golang.org/protobuf/types/known/emptypb"
)
2024-10-09 18:55:16 +00:00
type problemHandlers struct {
problemv1.UnimplementedProblemServiceServer
problemUC problems.ProblemUseCase
}
2024-10-13 14:01:36 +00:00
func NewProblemHandlers(gserver *grpc.Server, problemUC problems.ProblemUseCase) {
handlers := &problemHandlers{problemUC: problemUC}
2024-08-18 00:07:31 +00:00
2024-10-13 14:01:36 +00:00
problemv1.RegisterProblemServiceServer(gserver, handlers)
2024-08-18 00:07:31 +00:00
}
2024-10-13 14:01:36 +00:00
func (h *problemHandlers) CreateProblem(ctx context.Context, req *problemv1.CreateProblemRequest) (*problemv1.CreateProblemResponse, error) {
id, err := h.problemUC.CreateProblem(ctx, req.GetTitle())
2024-08-20 22:01:06 +00:00
if err != nil {
2024-10-13 14:01:36 +00:00
return nil, err
2024-08-20 22:01:06 +00:00
}
2024-10-13 14:01:36 +00:00
return &problemv1.CreateProblemResponse{Id: id}, nil
2024-08-18 00:07:31 +00:00
}
2024-10-09 18:55:16 +00:00
func (h *problemHandlers) ReadProblem(ctx context.Context, req *problemv1.ReadProblemRequest) (*problemv1.ReadProblemResponse, error) {
problem, err := h.problemUC.ReadProblemById(ctx, req.GetId())
2024-08-18 00:07:31 +00:00
if err != nil {
2024-08-22 22:56:03 +00:00
return nil, err
2024-08-18 00:07:31 +00:00
}
return &problemv1.ReadProblemResponse{
Problem: &problemv1.ReadProblemResponse_Problem{
2024-10-13 14:01:36 +00:00
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),
2024-08-18 00:07:31 +00:00
},
}, nil
}
2024-10-09 18:55:16 +00:00
func (h *problemHandlers) DeleteProblem(ctx context.Context, req *problemv1.DeleteProblemRequest) (*emptypb.Empty, error) {
err := h.problemUC.DeleteProblem(ctx, req.GetId())
2024-08-18 00:07:31 +00:00
if err != nil {
2024-08-22 22:56:03 +00:00
return nil, err
2024-08-18 00:07:31 +00:00
}
return &emptypb.Empty{}, nil
}