ms-tester/internal/problems/usecase/usecase.go

102 lines
3.1 KiB
Go
Raw Normal View History

2024-10-09 18:55:16 +00:00
package usecase
2024-08-16 15:54:23 +00:00
import (
"context"
2024-10-05 16:05:29 +00:00
"git.sch9.ru/new_gate/models"
2024-10-09 18:55:16 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/external/pandoc"
2024-10-09 19:04:37 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
2024-08-16 15:54:23 +00:00
)
type ProblemStorage interface {
2024-08-20 22:54:46 +00:00
CreateProblem(ctx context.Context, problem *models.Problem, testGroupData []models.TestGroupData) (int32, error)
2024-08-16 15:54:23 +00:00
ReadProblemById(ctx context.Context, id int32) (*models.Problem, error)
UpdateProblem(ctx context.Context, problem *models.Problem) error
DeleteProblem(ctx context.Context, id int32) error
}
2024-08-22 22:56:03 +00:00
type IPermissionService interface {
Allowed(ctx context.Context, user *models.User, action string) bool
}
2024-10-09 18:55:16 +00:00
type ProblemUseCase struct {
2024-08-22 22:56:03 +00:00
problemStorage ProblemStorage
2024-10-09 18:55:16 +00:00
pandocClient pandoc.PandocClient
2024-08-22 22:56:03 +00:00
permissionService IPermissionService
2024-08-16 15:54:23 +00:00
}
2024-10-09 18:55:16 +00:00
func NewProblemUseCase(
2024-08-16 15:54:23 +00:00
problemStorage ProblemStorage,
2024-10-09 18:55:16 +00:00
pandocClient pandoc.PandocClient,
2024-10-05 16:05:29 +00:00
permissionService IPermissionService,
2024-10-09 18:55:16 +00:00
) *ProblemUseCase {
return &ProblemUseCase{
2024-08-25 10:03:00 +00:00
problemStorage: problemStorage,
pandocClient: pandocClient,
2024-10-05 16:05:29 +00:00
permissionService: permissionService,
2024-08-16 15:54:23 +00:00
}
}
2024-08-22 22:56:03 +00:00
func extractUser(ctx context.Context) *models.User {
return ctx.Value("user").(*models.User)
}
2024-10-09 18:55:16 +00:00
func (service *ProblemUseCase) CanCreateProblem(ctx context.Context) error {
2024-08-22 22:56:03 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
2024-10-09 19:04:37 +00:00
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
2024-08-22 22:56:03 +00:00
}
return nil
}
2024-10-09 18:55:16 +00:00
func (service *ProblemUseCase) CanReadProblemById(ctx context.Context) error {
2024-08-22 22:56:03 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
2024-10-09 19:04:37 +00:00
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
2024-08-22 22:56:03 +00:00
}
return nil
}
2024-10-09 18:55:16 +00:00
func (service *ProblemUseCase) CanUpdateProblem(ctx context.Context) error {
2024-08-22 22:56:03 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
2024-10-09 19:04:37 +00:00
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
2024-08-22 22:56:03 +00:00
}
return nil
}
2024-10-09 18:55:16 +00:00
func (service *ProblemUseCase) CanDeleteProblem(ctx context.Context) error {
2024-08-22 22:56:03 +00:00
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
2024-10-09 19:04:37 +00:00
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
2024-08-22 22:56:03 +00:00
}
return nil
}
2024-10-09 18:55:16 +00:00
func (service *ProblemUseCase) CreateProblem(ctx context.Context, problem *models.Problem) (int32, error) {
2024-08-22 22:56:03 +00:00
if err := service.CanCreateProblem(ctx); err != nil {
return 0, err
}
_, err := service.pandocClient.ConvertLatexToHtml5(ctx, *problem.Description)
if err != nil {
return 0, err
}
return service.problemStorage.CreateProblem(ctx, problem, nil)
2024-08-16 15:54:23 +00:00
}
2024-10-09 18:55:16 +00:00
func (service *ProblemUseCase) ReadProblemById(ctx context.Context, id int32) (*models.Problem, error) {
2024-08-22 22:56:03 +00:00
if err := service.CanReadProblemById(ctx); err != nil {
return nil, err
}
return service.problemStorage.ReadProblemById(ctx, id)
2024-08-16 15:54:23 +00:00
}
2024-10-09 18:55:16 +00:00
func (service *ProblemUseCase) UpdateProblem(ctx context.Context, problem *models.Problem) error {
2024-08-22 22:56:03 +00:00
if err := service.CanUpdateProblem(ctx); err != nil {
return err
}
return service.problemStorage.UpdateProblem(ctx, problem)
2024-08-16 15:54:23 +00:00
}
2024-10-09 18:55:16 +00:00
func (service *ProblemUseCase) DeleteProblem(ctx context.Context, id int32) error {
2024-08-22 22:56:03 +00:00
if err := service.CanDeleteProblem(ctx); err != nil {
return err
}
return service.problemStorage.DeleteProblem(ctx, id)
2024-08-16 15:54:23 +00:00
}