2024-10-09 18:55:16 +00:00
|
|
|
package usecase
|
2024-08-16 17:02:13 +00:00
|
|
|
|
|
|
|
import (
|
2024-08-20 22:54:46 +00:00
|
|
|
"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/utils"
|
2024-08-16 17:02:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TaskStorage interface {
|
2024-08-20 22:54:46 +00:00
|
|
|
CreateTask(ctx context.Context, task models.Task) (int32, error)
|
|
|
|
DeleteTask(ctx context.Context, id int32) error
|
2024-08-16 17:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TaskService struct {
|
2024-08-23 09:56:52 +00:00
|
|
|
taskStorage TaskStorage
|
|
|
|
permissionService IPermissionService
|
2024-08-16 17:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTaskService(
|
2024-08-20 22:54:46 +00:00
|
|
|
taskStorage TaskStorage,
|
2024-08-23 09:56:52 +00:00
|
|
|
permissionService IPermissionService,
|
2024-08-16 17:02:13 +00:00
|
|
|
) *TaskService {
|
2024-08-20 22:54:46 +00:00
|
|
|
return &TaskService{
|
2024-08-23 09:56:52 +00:00
|
|
|
taskStorage: taskStorage,
|
|
|
|
permissionService: permissionService,
|
2024-08-20 22:54:46 +00:00
|
|
|
}
|
2024-08-16 17:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (service *TaskService) CreateTask(ctx context.Context, task models.Task) (int32, error) {
|
2024-08-23 09:56:52 +00:00
|
|
|
if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
|
2024-10-09 18:55:16 +00:00
|
|
|
return 0, utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
2024-08-23 09:56:52 +00:00
|
|
|
}
|
2024-08-20 22:54:46 +00:00
|
|
|
return service.taskStorage.CreateTask(ctx, task)
|
2024-08-16 17:02:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 17:04:49 +00:00
|
|
|
func (service *TaskService) DeleteTask(ctx context.Context, id int32) error {
|
2024-08-23 09:56:52 +00:00
|
|
|
if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
|
2024-10-09 18:55:16 +00:00
|
|
|
return utils.ServiceError(nil, utils.ErrNoPermission, "permission denied")
|
2024-08-23 09:56:52 +00:00
|
|
|
}
|
2024-08-20 22:54:46 +00:00
|
|
|
return service.taskStorage.DeleteTask(ctx, id)
|
2024-08-16 17:02:13 +00:00
|
|
|
}
|