Added Tasks to API.

This commit is contained in:
abdulmohsen
2024-03-05 16:11:18 +03:00
parent e157622793
commit 165bcef1ed
6 changed files with 123 additions and 29 deletions

View File

@@ -287,16 +287,6 @@ final class Index
'data' => [],
];
if (0 === $stmt->rowCount()) {
$response['error'] = [
'message' => 'No Results.',
];
if (true === count($response['filters']) >= 1) {
$response['error']['message'] .= ' Probably invalid filters values were used.';
}
}
while ($row = $stmt->fetch()) {
$entity = Container::get(iState::class)->fromArray($row);
$item = $entity->getAll();

63
src/API/Tasks/Index.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace App\API\Tasks;
use App\Commands\System\TasksCommand;
use App\Libs\Attributes\Route\Get;
use App\Libs\HTTP_STATUS;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
#[Get(self::URL . '[/]', name: 'tasks.index')]
final class Index
{
public const URL = '%{api.prefix}/tasks';
public function __invoke(ServerRequestInterface $request, array $args = []): ResponseInterface
{
$response = [
'data' => [],
];
$apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme('');
$urlPath = rtrim($request->getUri()->getPath(), '/');
foreach (TasksCommand::getTasks() as $task) {
$response['data'][] = [
'@self' => (string)$apiUrl->withPath($urlPath . '/' . ag($task, 'name')),
...array_filter(
self::formatTask($task),
fn($k) => false === in_array($k, ['command', 'args']),
ARRAY_FILTER_USE_KEY
)
];
}
return api_response($response, HTTP_STATUS::HTTP_OK, []);
}
public static function formatTask(array $task): array
{
$isEnabled = (bool)ag($task, 'enabled', false);
$item = [
'name' => ag($task, 'name'),
'description' => ag($task, 'description'),
'enabled' => $isEnabled,
'timer' => ag($task, 'timer')->getexpression(),
'next_run' => null,
'prev_run' => null,
'command' => ag($task, 'command'),
'args' => ag($task, 'args'),
];
if ($isEnabled) {
$item['next_run'] = makeDate(ag($task, 'timer')->getNextRunDate());
$item['prev_run'] = makeDate(ag($task, 'timer')->getPreviousRunDate());
}
return $item;
}
}

35
src/API/Tasks/View.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace App\API\Tasks;
use App\Commands\System\TasksCommand;
use App\Libs\Attributes\Route\Get;
use App\Libs\HTTP_STATUS;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
#[Get(Index::URL . '/{id:[a-zA-Z0-9_-]+}[/]', name: 'tasks.view')]
final class View
{
public function __invoke(ServerRequestInterface $request, array $args = []): ResponseInterface
{
if (null === ($id = ag($args, 'id'))) {
return api_error('No id was given.', HTTP_STATUS::HTTP_BAD_REQUEST);
}
$task = TasksCommand::getTasks($id);
if (empty($task)) {
return api_error('Task not found.', HTTP_STATUS::HTTP_NOT_FOUND);
}
$response = [
'@self' => parseConfigValue(Index::URL . '/' . $id),
...Index::formatTask($task)
];
return api_response($response, HTTP_STATUS::HTTP_OK);
}
}