2024-07-27 07:31:04 +00:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-16 11:05:29 +00:00
|
|
|
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
2024-08-18 00:07:31 +00:00
|
|
|
problemv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/problem/v1"
|
2024-08-16 17:09:03 +00:00
|
|
|
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/session/v1"
|
2024-07-27 07:31:04 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2024-08-16 11:05:29 +00:00
|
|
|
type ProblemService interface {
|
2024-08-18 00:07:31 +00:00
|
|
|
CreateProblem(ctx context.Context, problem *models.Problem, ch <-chan []byte) (int32, error) // FIXME: specify chan type
|
|
|
|
ReadProblemById(ctx context.Context, id int32) (*models.Problem, error)
|
2024-08-16 11:05:29 +00:00
|
|
|
UpdateProblem(ctx context.Context, problem *models.Problem) error
|
|
|
|
DeleteProblem(ctx context.Context, id int32) error
|
2024-07-27 07:31:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 15:01:08 +00:00
|
|
|
type SessionClient interface {
|
|
|
|
Read(ctx context.Context,
|
|
|
|
in *sessionv1.ReadSessionRequest,
|
|
|
|
opts ...grpc.CallOption,
|
|
|
|
) (*sessionv1.ReadSessionResponse, error)
|
|
|
|
}
|
|
|
|
|
2024-08-16 11:05:29 +00:00
|
|
|
type TesterServer struct {
|
2024-08-18 00:07:31 +00:00
|
|
|
problemv1.UnimplementedProblemServiceServer
|
2024-08-16 11:05:29 +00:00
|
|
|
problemService ProblemService
|
2024-07-27 07:31:04 +00:00
|
|
|
|
2024-08-16 15:01:08 +00:00
|
|
|
sessionClient SessionClient
|
|
|
|
|
|
|
|
grpcServer *grpc.Server
|
2024-07-27 07:31:04 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2024-08-16 15:01:08 +00:00
|
|
|
func NewTesterServer(
|
|
|
|
problemService ProblemService,
|
2024-08-16 15:03:56 +00:00
|
|
|
sessionClient SessionClient,
|
2024-08-16 15:01:08 +00:00
|
|
|
logger *zap.Logger,
|
|
|
|
) *TesterServer {
|
2024-08-16 11:05:29 +00:00
|
|
|
server := &TesterServer{
|
|
|
|
problemService: problemService,
|
2024-08-16 15:03:56 +00:00
|
|
|
sessionClient: sessionClient,
|
2024-07-27 07:31:04 +00:00
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
2024-08-16 15:01:08 +00:00
|
|
|
grpcServer := grpc.NewServer(
|
|
|
|
grpc.UnaryInterceptor(server.AuthInterceptor()),
|
|
|
|
)
|
|
|
|
|
2024-08-18 00:07:31 +00:00
|
|
|
problemv1.RegisterProblemServiceServer(grpcServer, server)
|
2024-07-27 07:31:04 +00:00
|
|
|
|
2024-08-16 11:05:29 +00:00
|
|
|
return server
|
2024-07-27 07:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AsTimeP(t *timestamppb.Timestamp) *time.Time {
|
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tt := t.AsTime()
|
|
|
|
return &tt
|
|
|
|
}
|
|
|
|
|
|
|
|
func AsTimestampP(t *time.Time) *timestamppb.Timestamp {
|
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return timestamppb.New(*t)
|
|
|
|
}
|