ms-tester/internal/services/task.go

42 lines
1.1 KiB
Go
Raw Normal View History

2024-08-16 17:02:13 +00:00
package services
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-08-23 09:56:52 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/lib"
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
}