ms-tester/internal/services/solution.go

58 lines
2 KiB
Go
Raw Normal View History

2024-08-17 10:43:15 +00:00
package services
import (
2024-08-20 22:54:46 +00:00
"context"
2024-08-23 09:56:52 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/lib"
2024-08-25 16:52:00 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/models"
2024-08-17 10:43:15 +00:00
)
type SolutionStorage interface {
2024-08-20 22:54:46 +00:00
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
2024-08-17 10:43:15 +00:00
}
type SolutionService struct {
2024-08-23 09:56:52 +00:00
solutionStorage SolutionStorage
permissionService IPermissionService
2024-08-17 10:43:15 +00:00
}
func NewSolutionService(
2024-08-20 22:54:46 +00:00
solutionStorage SolutionStorage,
2024-08-23 09:56:52 +00:00
permissionService IPermissionService,
2024-08-17 10:43:15 +00:00
) *SolutionService {
2024-08-20 22:54:46 +00:00
return &SolutionService{
2024-08-23 09:56:52 +00:00
solutionStorage: solutionStorage,
permissionService: permissionService,
2024-08-20 22:54:46 +00:00
}
2024-08-17 10:43:15 +00:00
}
func (service *SolutionService) CreateSolution(ctx context.Context, solution models.Solution) (int32, error) {
2024-08-23 09:56:52 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
2024-08-20 22:54:46 +00:00
return service.solutionStorage.CreateSolution(ctx, solution)
2024-08-17 10:43:15 +00:00
}
func (service *SolutionService) ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) {
2024-08-23 09:56:52 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
return models.Solution{}, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
2024-08-20 22:54:46 +00:00
return service.solutionStorage.ReadSolutionById(ctx, id)
2024-08-17 10:43:15 +00:00
}
func (service *SolutionService) RejudgeSolution(ctx context.Context, id int32) error {
2024-08-23 09:56:52 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
2024-08-20 22:54:46 +00:00
return service.solutionStorage.RejudgeSolution(ctx, id)
2024-08-17 10:43:15 +00:00
}
func (service *SolutionService) DeleteSolution(ctx context.Context, id int32) error {
2024-08-23 09:56:52 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
2024-08-20 22:54:46 +00:00
return service.solutionStorage.DeleteSolution(ctx, id)
2024-08-17 10:43:15 +00:00
}