ms-tester/internal/services/task.go
Vyacheslav1557 81e75e5a9c fix:
2024-10-05 21:05:29 +05:00

42 lines
1.1 KiB
Go

package services
import (
"context"
"git.sch9.ru/new_gate/models"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
)
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, lib.ServiceError(nil, lib.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 lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.taskStorage.DeleteTask(ctx, id)
}