add scheduled nodes endpoint
This commit is contained in:
parent
636d2dc517
commit
4294521dfc
2 changed files with 67 additions and 0 deletions
61
app/ScheduledNode/ScheduledNodeController.php
Normal file
61
app/ScheduledNode/ScheduledNodeController.php
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace App\ScheduledNode;
|
||||
|
||||
use App\ScheduledNode\UseCases\GetTodaysSchedule;
|
||||
use App\ScheduledNode\UseCases\GetTodaysScheduleRequest;
|
||||
use App\User\User;
|
||||
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 === null || $date === '') {
|
||||
$response->getBody()->write(
|
||||
json_encode(['error' => 'date is required'])
|
||||
);
|
||||
return $response->withStatus(400)
|
||||
->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
$scheduledNodes = $getTodaysSchedule->execute(
|
||||
new GetTodaysScheduleRequest(
|
||||
date: $date,
|
||||
userId: $user->getId(),
|
||||
)
|
||||
);
|
||||
|
||||
$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');
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ use App\View\ViewController;
|
|||
use App\Text\TextController;
|
||||
use App\Node\NodeController;
|
||||
use App\Plan\PlanController;
|
||||
use App\ScheduledNode\ScheduledNodeController;
|
||||
|
||||
$container = require __DIR__ . '/container.php';
|
||||
$app = Bridge::create($container);
|
||||
|
|
@ -44,6 +45,11 @@ $app->group('', function (RouteCollectorProxy $group) {
|
|||
);
|
||||
|
||||
$group->post('/api/plans', [PlanController::class, 'createPlan']);
|
||||
|
||||
$group->get(
|
||||
'/api/scheduled-nodes',
|
||||
[ScheduledNodeController::class, 'getScheduledNodes']
|
||||
);
|
||||
})->add(AuthMiddleware::class);
|
||||
|
||||
// Admin-only routes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue