ms-tester/internal/services/problem.go

103 lines
3.1 KiB
Go
Raw Normal View History

2024-08-16 15:54:23 +00:00
package services
import (
"context"
2024-08-22 22:56:03 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/lib"
2024-08-16 15:54:23 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/models"
)
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-18 00:07:31 +00:00
type PandocClient interface {
2024-08-20 22:54:46 +00:00
ConvertLatexToHtml5(ctx context.Context, text string) (string, error)
2024-08-18 00:07:31 +00:00
}
2024-08-22 22:56:03 +00:00
type IPermissionService interface {
Allowed(ctx context.Context, user *models.User, action string) bool
}
2024-08-16 15:54:23 +00:00
type ProblemService struct {
2024-08-22 22:56:03 +00:00
problemStorage ProblemStorage
pandocClient PandocClient
permissionService IPermissionService
2024-08-16 15:54:23 +00:00
}
func NewProblemService(
problemStorage ProblemStorage,
2024-08-18 00:07:31 +00:00
pandocClient PandocClient,
2024-08-16 15:54:23 +00:00
) *ProblemService {
return &ProblemService{
problemStorage: problemStorage,
2024-08-18 00:07:31 +00:00
pandocClient: pandocClient,
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)
}
func (service *ProblemService) CanCreateProblem(ctx context.Context) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return nil
}
func (service *ProblemService) CanReadProblemById(ctx context.Context) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return nil
}
func (service *ProblemService) CanUpdateProblem(ctx context.Context) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return nil
}
func (service *ProblemService) CanDeleteProblem(ctx context.Context) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return nil
}
func (service *ProblemService) CreateProblem(ctx context.Context, problem *models.Problem) (int32, error) {
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-08-16 17:04:49 +00:00
func (service *ProblemService) 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-08-16 17:04:49 +00:00
func (service *ProblemService) 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-08-16 17:04:49 +00:00
func (service *ProblemService) 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
}