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

42 lines
1.1 KiB
Go

package usecase
import (
"context"
"git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
)
type TaskStorage interface {
CreateTask(ctx context.Context, task models.Task) (int32, error)
DeleteTask(ctx context.Context, id int32) error
}
type TaskService struct {
taskStorage TaskStorage
permissionService IPermissionService
}
func NewTaskService(
taskStorage TaskStorage,
permissionService IPermissionService,
) *TaskService {
return &TaskService{
taskStorage: taskStorage,
permissionService: permissionService,
}
}
func (service *TaskService) CreateTask(ctx context.Context, task models.Task) (int32, error) {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
}
return service.taskStorage.CreateTask(ctx, task)
}
func (service *TaskService) DeleteTask(ctx context.Context, id int32) error {
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
}
return service.taskStorage.DeleteTask(ctx, id)
}