73 lines
2.5 KiB
PHP
73 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\ScheduledNode;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\ScheduledNode\UseCases\GetTodaysSchedule;
|
|
use App\ScheduledNode\UseCases\GetTodaysScheduleRequest;
|
|
use App\User\User;
|
|
use DomainException;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
class ScheduledNodeController
|
|
{
|
|
public function getScheduledNodes(
|
|
Request $request,
|
|
Response $response,
|
|
GetTodaysSchedule $getTodaysSchedule,
|
|
): Response {
|
|
$user = $request->getAttribute('user');
|
|
if (!$user instanceof User) {
|
|
$response->getBody()->write(
|
|
json_encode(['error' => 'unauthenticated'])
|
|
);
|
|
return $response->withStatus(401)
|
|
->withHeader('Content-Type', 'application/json');
|
|
}
|
|
|
|
$queryParams = $request->getQueryParams();
|
|
$date = $queryParams['date'] ?? null;
|
|
if ($date === '') {
|
|
$date = null;
|
|
}
|
|
|
|
try {
|
|
$scheduledNodes = $getTodaysSchedule->execute(
|
|
new GetTodaysScheduleRequest(
|
|
date: $date,
|
|
userId: $user->getId(),
|
|
)
|
|
);
|
|
} catch (BadRequestException $exception) {
|
|
$response->getBody()->write(
|
|
json_encode(['error' => $exception->getMessage()])
|
|
);
|
|
return $response->withStatus(400)
|
|
->withHeader('Content-Type', 'application/json');
|
|
} catch (DomainException $exception) {
|
|
$response->getBody()->write(
|
|
json_encode(['error' => $exception->getMessage()])
|
|
);
|
|
return $response->withStatus(404)
|
|
->withHeader('Content-Type', 'application/json');
|
|
}
|
|
|
|
$data = array_values(array_map(
|
|
function (ScheduledNode $scheduledNode) {
|
|
return [
|
|
'id' => $scheduledNode->getId(),
|
|
'date' => $scheduledNode->getDate()->format('Y-m-d'),
|
|
'planName' => $scheduledNode->getPlan()->getName(),
|
|
'nodeTitle' => $scheduledNode->getNode()->getTitle(),
|
|
'completed' => $scheduledNode->getCompleted(),
|
|
];
|
|
},
|
|
$scheduledNodes,
|
|
));
|
|
|
|
$response->getBody()->write(json_encode($data));
|
|
return $response->withStatus(200)
|
|
->withHeader('Content-Type', 'application/json');
|
|
}
|
|
}
|