2024-08-16 17:02:13 +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-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") {
|
|
|
|
return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
|
|
|
|
}
|
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") {
|
|
|
|
return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
|
|
|
|
}
|
2024-08-20 22:54:46 +00:00
|
|
|
return service.taskStorage.DeleteTask(ctx, id)
|
2024-08-16 17:02:13 +00:00
|
|
|
}
|