ms-tester/internal/contests/usecase/solution.go
Vyacheslav1557 d62ae666d5 refactor:
2024-10-09 23:55:16 +05:00

58 lines
2 KiB
Go

package usecase
import (
"context"
"git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
)
type SolutionStorage interface {
CreateSolution(ctx context.Context, solution models.Solution) (int32, error)
ReadSolutionById(ctx context.Context, id int32) (models.Solution, error)
RejudgeSolution(ctx context.Context, id int32) error
DeleteSolution(ctx context.Context, id int32) error
}
type SolutionService struct {
solutionStorage SolutionStorage
permissionService IPermissionService
}
func NewSolutionService(
solutionStorage SolutionStorage,
permissionService IPermissionService,
) *SolutionService {
return &SolutionService{
solutionStorage: solutionStorage,
permissionService: permissionService,
}
}
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 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 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 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 service.solutionStorage.DeleteSolution(ctx, id)
}