ms-tester/internal/contests/usecase/solution.go
Vyacheslav1557 4cdd751b16 fix:
2024-10-10 00:04:37 +05:00

58 lines
2 KiB
Go

package usecase
import (
"context"
"git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
)
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, 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{}, 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 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 utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
}
return service.solutionStorage.DeleteSolution(ctx, id)
}