ms-tester/internal/transport/problem.go

141 lines
3.6 KiB
Go
Raw Normal View History

2024-08-18 00:07:31 +00:00
package transport
import (
"context"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"git.sch9.ru/new_gate/ms-tester/internal/models"
problemv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/problem/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"io"
)
func (s *TesterServer) CreateProblem(server problemv1.ProblemService_CreateProblemServer) error {
ctx := server.Context()
2024-08-20 11:18:23 +00:00
if !s.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return status.Errorf(codes.PermissionDenied, "") // FIXME
2024-08-18 00:07:31 +00:00
}
2024-08-20 11:18:23 +00:00
req, err := server.Recv() // receive problem
2024-08-18 00:07:31 +00:00
if err != nil {
return err // FIXME
}
problem := req.GetProblem()
if problem == nil {
return status.Errorf(codes.Unknown, "") // FIXME
}
p := &models.Problem{
Name: lib.AsStringP(problem.Name),
Description: lib.AsStringP(problem.Description),
TimeLimit: lib.AsInt32P(problem.TimeLimit),
MemoryLimit: lib.AsInt32P(problem.MemoryLimit),
}
ch := readChunks(ctx, server)
id, err := s.problemService.CreateProblem(ctx, p, ch)
if err != nil {
return status.Errorf(codes.Unknown, "") // FIXME
}
err = server.SendAndClose(&problemv1.CreateProblemResponse{
Id: id,
})
if err != nil {
return err // FIXME
}
return nil
}
func readChunks(ctx context.Context, server problemv1.ProblemService_CreateProblemServer) <-chan []byte {
ch := make(chan []byte)
go func() {
defer close(ch)
for {
select {
case <-ctx.Done():
return // FIXME
default:
req, err := server.Recv()
if err != nil {
if err == io.EOF {
return // FIXME
}
if status.Code(err) == codes.Canceled {
return // FIXME
}
continue
}
test := req.GetTest()
if test == nil {
return // FIXME
}
ch <- test.Chunk
}
}
}()
return ch
}
func (s *TesterServer) ReadProblem(ctx context.Context, req *problemv1.ReadProblemRequest) (*problemv1.ReadProblemResponse, error) {
2024-08-20 11:18:23 +00:00
if !s.permissionService.Allowed(ctx, extractUser(ctx), "read") {
return nil, status.Errorf(codes.PermissionDenied, "") // FIXME
}
2024-08-18 00:07:31 +00:00
problem, err := s.problemService.ReadProblemById(ctx, req.GetId())
if err != nil {
return nil, status.Errorf(codes.Unknown, err.Error()) // FIXME
}
return &problemv1.ReadProblemResponse{
Problem: &problemv1.ReadProblemResponse_Problem{
Id: *problem.Id,
Name: *problem.Name,
Description: *problem.Description,
TimeLimit: *problem.TimeLimit,
MemoryLimit: *problem.MemoryLimit,
CreatedAt: AsTimestampP(problem.CreatedAt),
UpdatedAt: AsTimestampP(problem.UpdatedAt),
},
}, nil
}
//func (s *TesterServer) UpdateProblem(ctx context.Context, req *problemv1.UpdateProblemRequest) (*emptypb.Empty, error) {
// problem := req.GetProblem()
// if problem == nil {
// return nil, status.Errorf(codes.Unknown, "") // FIXME
// }
// err := s.problemService.UpdateProblem(
// ctx,
// &models.Problem{
// Id: lib.AsInt32P(problem.Id),
// Name: problem.Name,
// Description: problem.Description,
// TimeLimit: problem.TimeLimit,
// MemoryLimit: problem.MemoryLimit,
// },
// )
// if err != nil {
// return nil, status.Errorf(codes.Unknown, err.Error()) // FIXME
// }
//
// return &emptypb.Empty{}, nil
//}
func (s *TesterServer) DeleteProblem(ctx context.Context, req *problemv1.DeleteProblemRequest) (*emptypb.Empty, error) {
2024-08-20 11:18:23 +00:00
if !s.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return nil, status.Errorf(codes.PermissionDenied, "") // FIXME
}
2024-08-18 00:07:31 +00:00
err := s.problemService.DeleteProblem(ctx, req.GetId())
if err != nil {
return nil, status.Errorf(codes.Unknown, err.Error()) // FIXME
}
return &emptypb.Empty{}, nil
}