read user from request in plan controller

This commit is contained in:
Yisroel Baum 2026-04-24 13:32:04 +03:00
parent 05374991c5
commit 5a24f5bde4
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -5,6 +5,7 @@ namespace App\Plan;
use App\Exceptions\BadRequestException;
use App\Plan\UseCases\CreatePlan;
use App\Plan\UseCases\CreatePlanRequest;
use App\User\User;
use DomainException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
@ -16,9 +17,17 @@ class PlanController
Response $response,
CreatePlan $createPlanUseCase,
): 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');
}
$data = json_decode((string) $request->getBody(), true) ?? [];
$userId = isset($data['userId']) ? (int) $data['userId'] : null;
$textId = isset($data['textId']) ? (int) $data['textId'] : null;
$name = $data['name'] ?? null;
$dateStart = $data['dateStart'] ?? null;
@ -26,7 +35,7 @@ class PlanController
try {
$plan = $createPlanUseCase->execute(new CreatePlanRequest(
userId: $userId,
userId: $user->getId(),
textId: $textId,
name: $name,
dateStart: $dateStart,