try to follow to HATEOAS.
This commit is contained in:
@@ -17,22 +17,28 @@ final class Index
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, array $args = []): ResponseInterface
|
||||
{
|
||||
$response = [
|
||||
'data' => [],
|
||||
];
|
||||
|
||||
$apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme('');
|
||||
$urlPath = rtrim($request->getUri()->getPath(), '/');
|
||||
|
||||
$response = [
|
||||
'data' => [],
|
||||
'links' => [
|
||||
'self' => (string)$apiUrl,
|
||||
],
|
||||
];
|
||||
|
||||
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
|
||||
)
|
||||
$task = array_filter(
|
||||
self::formatTask($task),
|
||||
fn($k) => false === in_array($k, ['command', 'args']),
|
||||
ARRAY_FILTER_USE_KEY
|
||||
);
|
||||
|
||||
$task['links'] = [
|
||||
'self' => (string)$apiUrl->withPath($urlPath . '/' . ag($task, 'name')),
|
||||
];
|
||||
|
||||
$response['data'][] = $task;
|
||||
}
|
||||
|
||||
return api_response($response, HTTP_STATUS::HTTP_OK, []);
|
||||
|
||||
@@ -19,12 +19,22 @@ final class View
|
||||
return api_error('No id was given.', HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme('');
|
||||
|
||||
$task = TasksCommand::getTasks($id);
|
||||
|
||||
if (empty($task)) {
|
||||
return api_error('Task not found.', HTTP_STATUS::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return api_response(Index::formatTask($task), HTTP_STATUS::HTTP_OK);
|
||||
$response = [
|
||||
...Index::formatTask($task),
|
||||
'links' => [
|
||||
'self' => (string)$apiUrl,
|
||||
'list' => (string)$apiUrl->withPath(parseConfigValue(Index::URL)),
|
||||
],
|
||||
];
|
||||
|
||||
return api_response($response, HTTP_STATUS::HTTP_OK);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user