This commit is contained in:
Vyacheslav1557 2024-10-10 00:04:37 +05:00
parent d62ae666d5
commit 4cdd751b16
11 changed files with 53 additions and 53 deletions

View file

@ -3,7 +3,7 @@ package usecase
import ( import (
"context" "context"
"git.sch9.ru/new_gate/models" "git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/internal/lib" "git.sch9.ru/new_gate/ms-tester/pkg/utils"
) )
type ParticipantStorage interface { type ParticipantStorage interface {
@ -30,28 +30,28 @@ func NewParticipantService(
func (service *ParticipantService) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) { func (service *ParticipantService) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.participantStorage.CreateParticipant(ctx, participant) return service.participantStorage.CreateParticipant(ctx, participant)
} }
func (service *ParticipantService) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) { func (service *ParticipantService) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
return nil, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return nil, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.participantStorage.ReadParticipantById(ctx, id) return service.participantStorage.ReadParticipantById(ctx, id)
} }
func (service *ParticipantService) UpdateParticipant(ctx context.Context, participant *models.Participant) error { func (service *ParticipantService) UpdateParticipant(ctx context.Context, participant *models.Participant) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.participantStorage.UpdateParticipant(ctx, participant) return service.participantStorage.UpdateParticipant(ctx, participant)
} }
func (service *ParticipantService) DeleteParticipant(ctx context.Context, id int32) error { func (service *ParticipantService) DeleteParticipant(ctx context.Context, id int32) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.participantStorage.DeleteParticipant(ctx, id) return service.participantStorage.DeleteParticipant(ctx, id)
} }

View file

@ -1,4 +1,4 @@
package services package usecase
import ( import (
"context" "context"

View file

@ -3,7 +3,7 @@ package usecase
import ( import (
"context" "context"
"git.sch9.ru/new_gate/models" "git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/internal/lib" "git.sch9.ru/new_gate/ms-tester/pkg/utils"
) )
type SolutionStorage interface { type SolutionStorage interface {
@ -30,28 +30,28 @@ func NewSolutionService(
func (service *SolutionService) CreateSolution(ctx context.Context, solution models.Solution) (int32, error) { func (service *SolutionService) CreateSolution(ctx context.Context, solution models.Solution) (int32, error) {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.solutionStorage.CreateSolution(ctx, solution) return service.solutionStorage.CreateSolution(ctx, solution)
} }
func (service *SolutionService) ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) { func (service *SolutionService) ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
return models.Solution{}, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return models.Solution{}, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.solutionStorage.ReadSolutionById(ctx, id) return service.solutionStorage.ReadSolutionById(ctx, id)
} }
func (service *SolutionService) RejudgeSolution(ctx context.Context, id int32) error { func (service *SolutionService) RejudgeSolution(ctx context.Context, id int32) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.solutionStorage.RejudgeSolution(ctx, id) return service.solutionStorage.RejudgeSolution(ctx, id)
} }
func (service *SolutionService) DeleteSolution(ctx context.Context, id int32) error { func (service *SolutionService) DeleteSolution(ctx context.Context, id int32) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.solutionStorage.DeleteSolution(ctx, id) return service.solutionStorage.DeleteSolution(ctx, id)
} }

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"git.sch9.ru/new_gate/models" "git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/internal/contests" "git.sch9.ru/new_gate/ms-tester/internal/contests"
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
) )
type ContestService struct { type ContestService struct {
@ -23,28 +24,28 @@ func NewContestService(
func (service *ContestService) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) { func (service *ContestService) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.contestStorage.CreateContest(ctx, contest) return service.contestStorage.CreateContest(ctx, contest)
} }
func (service *ContestService) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) { func (service *ContestService) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
return nil, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return nil, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.contestStorage.ReadContestById(ctx, id) return service.contestStorage.ReadContestById(ctx, id)
} }
func (service *ContestService) UpdateContest(ctx context.Context, contest *models.Contest) error { func (service *ContestService) UpdateContest(ctx context.Context, contest *models.Contest) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.contestStorage.UpdateContest(ctx, contest) return service.contestStorage.UpdateContest(ctx, contest)
} }
func (service *ContestService) DeleteContest(ctx context.Context, id int32) error { func (service *ContestService) DeleteContest(ctx context.Context, id int32) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return service.contestStorage.DeleteContest(ctx, id) return service.contestStorage.DeleteContest(ctx, id)
} }

View file

@ -1,4 +1,4 @@
package services package usecase
import ( import (
"context" "context"

View file

@ -10,16 +10,16 @@ type LanguageUseCase struct {
languageRepo languages.LanguageRepository languageRepo languages.LanguageRepository
} }
func NewLanguageService( func NewLanguageUseCase(
languageStorage LanguageStorage, languageRepo languages.LanguageRepository,
) *LanguageService { ) *LanguageUseCase {
return &LanguageService{ return &LanguageUseCase{
languageStorage: languageStorage, languageRepo: languageRepo,
} }
} }
func (service *LanguageService) ReadLanguageById(ctx context.Context, id int32) (*models.Language, error) { func (uc *LanguageUseCase) ReadLanguageById(ctx context.Context, id int32) (*models.Language, error) {
//userId := ctx.Value("user_id").(int32) //userId := ctx.Value("user_id").(int32)
panic("access control is not implemented yet") panic("access control is not implemented yet")
return service.languageStorage.ReadLanguageById(ctx, id) return uc.languageRepo.ReadLanguageById(ctx, id)
} }

View file

@ -3,7 +3,6 @@ package grpc
import ( import (
"context" "context"
"git.sch9.ru/new_gate/models" "git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"git.sch9.ru/new_gate/ms-tester/internal/problems" "git.sch9.ru/new_gate/ms-tester/internal/problems"
problemv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/problem/v1" problemv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/problem/v1"
"git.sch9.ru/new_gate/ms-tester/pkg/utils" "git.sch9.ru/new_gate/ms-tester/pkg/utils"
@ -29,18 +28,18 @@ func (h *problemHandlers) CreateProblem(server problemv1.ProblemService_CreatePr
req, err := server.Recv() // receive problem req, err := server.Recv() // receive problem
if err != nil { if err != nil {
return lib.TransportError(err, lib.ErrBadInput, "can't receive problem") return utils.TransportError(err, utils.ErrBadInput, "can't receive problem")
} }
problem := req.GetProblem() problem := req.GetProblem()
if problem == nil { if problem == nil {
return lib.TransportError(nil, lib.ErrBadInput, "empty problem") return utils.TransportError(nil, utils.ErrBadInput, "empty problem")
} }
p := &models.Problem{ p := &models.Problem{
Name: lib.AsStringP(problem.Name), Name: utils.AsStringP(problem.Name),
Description: lib.AsStringP(problem.Description), Description: utils.AsStringP(problem.Description),
TimeLimit: lib.AsInt32P(problem.TimeLimit), TimeLimit: utils.AsInt32P(problem.TimeLimit),
MemoryLimit: lib.AsInt32P(problem.MemoryLimit), MemoryLimit: utils.AsInt32P(problem.MemoryLimit),
} }
ch := readChunks(ctx, server) ch := readChunks(ctx, server)
@ -59,7 +58,7 @@ func (h *problemHandlers) CreateProblem(server problemv1.ProblemService_CreatePr
Id: id, Id: id,
}) })
if err != nil { if err != nil {
return lib.TransportError(err, lib.ErrBadInput, "can't send response") return utils.TransportError(err, utils.ErrBadInput, "can't send response")
} }
return nil return nil
@ -147,7 +146,7 @@ func (h *problemHandlers) ReadProblem(ctx context.Context, req *problemv1.ReadPr
// err := s.problemService.UpdateProblem( // err := s.problemService.UpdateProblem(
// ctx, // ctx,
// &models.Problem{ // &models.Problem{
// Id: lib.AsInt32P(problem.Id), // Id: utils.AsInt32P(problem.Id),
// Name: problem.Name, // Name: problem.Name,
// Description: problem.Description, // Description: problem.Description,
// TimeLimit: problem.TimeLimit, // TimeLimit: problem.TimeLimit,

View file

@ -1,4 +1,4 @@
package services package usecase
import ( import (
"context" "context"

View file

@ -3,8 +3,8 @@ package usecase
import ( import (
"context" "context"
"git.sch9.ru/new_gate/models" "git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"git.sch9.ru/new_gate/ms-tester/pkg/external/pandoc" "git.sch9.ru/new_gate/ms-tester/pkg/external/pandoc"
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
) )
type ProblemStorage interface { type ProblemStorage interface {
@ -42,28 +42,28 @@ func extractUser(ctx context.Context) *models.User {
func (service *ProblemUseCase) CanCreateProblem(ctx context.Context) error { func (service *ProblemUseCase) CanCreateProblem(ctx context.Context) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return nil return nil
} }
func (service *ProblemUseCase) CanReadProblemById(ctx context.Context) error { func (service *ProblemUseCase) CanReadProblemById(ctx context.Context) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return nil return nil
} }
func (service *ProblemUseCase) CanUpdateProblem(ctx context.Context) error { func (service *ProblemUseCase) CanUpdateProblem(ctx context.Context) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return nil return nil
} }
func (service *ProblemUseCase) CanDeleteProblem(ctx context.Context) error { func (service *ProblemUseCase) CanDeleteProblem(ctx context.Context) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") { if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied") return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
} }
return nil return nil
} }

View file

@ -1,4 +1,4 @@
package services package usecase
import ( import (
"context" "context"

View file

@ -4,8 +4,8 @@ import (
"context" "context"
"errors" "errors"
"git.sch9.ru/new_gate/models" "git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/session/v1" sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/session/v1"
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
@ -42,9 +42,9 @@ func (s *TesterServer) readSessionAndReadUser(ctx context.Context, token string)
user, err := s.userService.ReadUserById(ctx, userId.GetUserId()) // FIXME: must be cached! user, err := s.userService.ReadUserById(ctx, userId.GetUserId()) // FIXME: must be cached!
if err != nil { if err != nil {
if errors.Is(err, lib.ErrNotFound) { if errors.Is(err, utils.ErrNotFound) {
user = &models.User{ user = &models.User{
UserId: lib.AsInt32P(userId.GetUserId()), UserId: utils.AsInt32P(userId.GetUserId()),
Role: models.RoleParticipant.AsPointer(), Role: models.RoleParticipant.AsPointer(),
} }
err = s.userService.CreateUser(ctx, user) err = s.userService.CreateUser(ctx, user)
@ -113,27 +113,27 @@ func ToGrpcError(err error) error {
// should I use map instead? // should I use map instead?
switch { switch {
case errors.Is(err, lib.ErrValidationFailed): case errors.Is(err, utils.ErrValidationFailed):
return status.Error(codes.InvalidArgument, err.Error()) return status.Error(codes.InvalidArgument, err.Error())
case errors.Is(err, lib.ErrInternal): case errors.Is(err, utils.ErrInternal):
return status.Error(codes.Internal, err.Error()) return status.Error(codes.Internal, err.Error())
case errors.Is(err, lib.ErrExternal): case errors.Is(err, utils.ErrExternal):
return status.Error(codes.Unavailable, err.Error()) return status.Error(codes.Unavailable, err.Error())
case errors.Is(err, lib.ErrNoPermission): case errors.Is(err, utils.ErrNoPermission):
return status.Error(codes.PermissionDenied, err.Error()) return status.Error(codes.PermissionDenied, err.Error())
case errors.Is(err, lib.ErrUnknown): case errors.Is(err, utils.ErrUnknown):
return status.Error(codes.Unknown, err.Error()) return status.Error(codes.Unknown, err.Error())
case errors.Is(err, lib.ErrDeadlineExceeded): case errors.Is(err, utils.ErrDeadlineExceeded):
return status.Error(codes.DeadlineExceeded, err.Error()) return status.Error(codes.DeadlineExceeded, err.Error())
case errors.Is(err, lib.ErrNotFound): case errors.Is(err, utils.ErrNotFound):
return status.Error(codes.NotFound, err.Error()) return status.Error(codes.NotFound, err.Error())
case errors.Is(err, lib.ErrAlreadyExists): case errors.Is(err, utils.ErrAlreadyExists):
return status.Error(codes.AlreadyExists, err.Error()) return status.Error(codes.AlreadyExists, err.Error())
case errors.Is(err, lib.ErrConflict): case errors.Is(err, utils.ErrConflict):
return status.Error(codes.Unimplemented, err.Error()) return status.Error(codes.Unimplemented, err.Error())
case errors.Is(err, lib.ErrUnimplemented): case errors.Is(err, utils.ErrUnimplemented):
return status.Error(codes.Unimplemented, err.Error()) return status.Error(codes.Unimplemented, err.Error())
case errors.Is(err, lib.ErrUnauthenticated): case errors.Is(err, utils.ErrUnauthenticated):
return status.Error(codes.Unauthenticated, err.Error()) return status.Error(codes.Unauthenticated, err.Error())
default: default:
return status.Error(codes.Unknown, err.Error()) return status.Error(codes.Unknown, err.Error())