diff --git a/internal/contests/usecase/participants.go b/internal/contests/usecase/participants.go index 13351ae..9ada21d 100644 --- a/internal/contests/usecase/participants.go +++ b/internal/contests/usecase/participants.go @@ -3,7 +3,7 @@ package usecase import ( "context" "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 { @@ -30,28 +30,28 @@ func NewParticipantService( func (service *ParticipantService) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) { 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) } func (service *ParticipantService) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) { 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) } func (service *ParticipantService) UpdateParticipant(ctx context.Context, participant *models.Participant) error { 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) } func (service *ParticipantService) DeleteParticipant(ctx context.Context, id int32) error { 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) } diff --git a/internal/contests/usecase/permission.go b/internal/contests/usecase/permission.go index 59af1f8..ae3855f 100644 --- a/internal/contests/usecase/permission.go +++ b/internal/contests/usecase/permission.go @@ -1,4 +1,4 @@ -package services +package usecase import ( "context" diff --git a/internal/contests/usecase/solution.go b/internal/contests/usecase/solution.go index 73335b1..482e403 100644 --- a/internal/contests/usecase/solution.go +++ b/internal/contests/usecase/solution.go @@ -3,7 +3,7 @@ package usecase import ( "context" "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 { @@ -30,28 +30,28 @@ func NewSolutionService( func (service *SolutionService) CreateSolution(ctx context.Context, solution models.Solution) (int32, error) { 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) } func (service *SolutionService) ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) { 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) } func (service *SolutionService) RejudgeSolution(ctx context.Context, id int32) error { 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) } func (service *SolutionService) DeleteSolution(ctx context.Context, id int32) error { 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) } diff --git a/internal/contests/usecase/usecase.go b/internal/contests/usecase/usecase.go index 8c9af4e..6b0e586 100644 --- a/internal/contests/usecase/usecase.go +++ b/internal/contests/usecase/usecase.go @@ -4,6 +4,7 @@ import ( "context" "git.sch9.ru/new_gate/models" "git.sch9.ru/new_gate/ms-tester/internal/contests" + "git.sch9.ru/new_gate/ms-tester/pkg/utils" ) type ContestService struct { @@ -23,28 +24,28 @@ func NewContestService( func (service *ContestService) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) { 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) } func (service *ContestService) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) { 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) } func (service *ContestService) UpdateContest(ctx context.Context, contest *models.Contest) error { 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) } func (service *ContestService) DeleteContest(ctx context.Context, id int32) error { 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) } diff --git a/internal/languages/usecase/permission.go b/internal/languages/usecase/permission.go index 59af1f8..ae3855f 100644 --- a/internal/languages/usecase/permission.go +++ b/internal/languages/usecase/permission.go @@ -1,4 +1,4 @@ -package services +package usecase import ( "context" diff --git a/internal/languages/usecase/usecase.go b/internal/languages/usecase/usecase.go index 9ce1f82..a8a322a 100644 --- a/internal/languages/usecase/usecase.go +++ b/internal/languages/usecase/usecase.go @@ -10,16 +10,16 @@ type LanguageUseCase struct { languageRepo languages.LanguageRepository } -func NewLanguageService( - languageStorage LanguageStorage, -) *LanguageService { - return &LanguageService{ - languageStorage: languageStorage, +func NewLanguageUseCase( + languageRepo languages.LanguageRepository, +) *LanguageUseCase { + return &LanguageUseCase{ + 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) panic("access control is not implemented yet") - return service.languageStorage.ReadLanguageById(ctx, id) + return uc.languageRepo.ReadLanguageById(ctx, id) } diff --git a/internal/problems/delivery/grpc/handlers.go b/internal/problems/delivery/grpc/handlers.go index d7e7bbe..1aef2dc 100644 --- a/internal/problems/delivery/grpc/handlers.go +++ b/internal/problems/delivery/grpc/handlers.go @@ -3,7 +3,6 @@ package grpc import ( "context" "git.sch9.ru/new_gate/models" - "git.sch9.ru/new_gate/ms-tester/internal/lib" "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" @@ -29,18 +28,18 @@ func (h *problemHandlers) CreateProblem(server problemv1.ProblemService_CreatePr req, err := server.Recv() // receive problem 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() if problem == nil { - return lib.TransportError(nil, lib.ErrBadInput, "empty problem") + return utils.TransportError(nil, utils.ErrBadInput, "empty problem") } p := &models.Problem{ - Name: lib.AsStringP(problem.Name), - Description: lib.AsStringP(problem.Description), - TimeLimit: lib.AsInt32P(problem.TimeLimit), - MemoryLimit: lib.AsInt32P(problem.MemoryLimit), + Name: utils.AsStringP(problem.Name), + Description: utils.AsStringP(problem.Description), + TimeLimit: utils.AsInt32P(problem.TimeLimit), + MemoryLimit: utils.AsInt32P(problem.MemoryLimit), } ch := readChunks(ctx, server) @@ -59,7 +58,7 @@ func (h *problemHandlers) CreateProblem(server problemv1.ProblemService_CreatePr Id: id, }) 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 @@ -147,7 +146,7 @@ func (h *problemHandlers) ReadProblem(ctx context.Context, req *problemv1.ReadPr // err := s.problemService.UpdateProblem( // ctx, // &models.Problem{ -// Id: lib.AsInt32P(problem.Id), +// Id: utils.AsInt32P(problem.Id), // Name: problem.Name, // Description: problem.Description, // TimeLimit: problem.TimeLimit, diff --git a/internal/problems/usecase/permission.go b/internal/problems/usecase/permission.go index 59af1f8..ae3855f 100644 --- a/internal/problems/usecase/permission.go +++ b/internal/problems/usecase/permission.go @@ -1,4 +1,4 @@ -package services +package usecase import ( "context" diff --git a/internal/problems/usecase/usecase.go b/internal/problems/usecase/usecase.go index b46a988..074705c 100644 --- a/internal/problems/usecase/usecase.go +++ b/internal/problems/usecase/usecase.go @@ -3,8 +3,8 @@ package usecase import ( "context" "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/utils" ) type ProblemStorage interface { @@ -42,28 +42,28 @@ func extractUser(ctx context.Context) *models.User { func (service *ProblemUseCase) CanCreateProblem(ctx context.Context) error { 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 } func (service *ProblemUseCase) CanReadProblemById(ctx context.Context) error { 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 } func (service *ProblemUseCase) CanUpdateProblem(ctx context.Context) error { 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 } func (service *ProblemUseCase) CanDeleteProblem(ctx context.Context) error { 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 } diff --git a/internal/tester/usecase/permission.go b/internal/tester/usecase/permission.go index 59af1f8..ae3855f 100644 --- a/internal/tester/usecase/permission.go +++ b/internal/tester/usecase/permission.go @@ -1,4 +1,4 @@ -package services +package usecase import ( "context" diff --git a/internal/transport/interceptors.go b/internal/transport/interceptors.go index fae667b..b83747f 100644 --- a/internal/transport/interceptors.go +++ b/internal/transport/interceptors.go @@ -4,8 +4,8 @@ import ( "context" "errors" "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" + "git.sch9.ru/new_gate/ms-tester/pkg/utils" "google.golang.org/grpc" "google.golang.org/grpc/codes" "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! if err != nil { - if errors.Is(err, lib.ErrNotFound) { + if errors.Is(err, utils.ErrNotFound) { user = &models.User{ - UserId: lib.AsInt32P(userId.GetUserId()), + UserId: utils.AsInt32P(userId.GetUserId()), Role: models.RoleParticipant.AsPointer(), } err = s.userService.CreateUser(ctx, user) @@ -113,27 +113,27 @@ func ToGrpcError(err error) error { // should I use map instead? switch { - case errors.Is(err, lib.ErrValidationFailed): + case errors.Is(err, utils.ErrValidationFailed): 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()) - case errors.Is(err, lib.ErrExternal): + case errors.Is(err, utils.ErrExternal): 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()) - case errors.Is(err, lib.ErrUnknown): + case errors.Is(err, utils.ErrUnknown): 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()) - case errors.Is(err, lib.ErrNotFound): + case errors.Is(err, utils.ErrNotFound): 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()) - case errors.Is(err, lib.ErrConflict): + case errors.Is(err, utils.ErrConflict): 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()) - case errors.Is(err, lib.ErrUnauthenticated): + case errors.Is(err, utils.ErrUnauthenticated): return status.Error(codes.Unauthenticated, err.Error()) default: return status.Error(codes.Unknown, err.Error())