read user from request in plan controller
This commit is contained in:
parent
05374991c5
commit
5a24f5bde4
1 changed files with 11 additions and 2 deletions
|
|
@ -5,6 +5,7 @@ namespace App\Plan;
|
||||||
use App\Exceptions\BadRequestException;
|
use App\Exceptions\BadRequestException;
|
||||||
use App\Plan\UseCases\CreatePlan;
|
use App\Plan\UseCases\CreatePlan;
|
||||||
use App\Plan\UseCases\CreatePlanRequest;
|
use App\Plan\UseCases\CreatePlanRequest;
|
||||||
|
use App\User\User;
|
||||||
use DomainException;
|
use DomainException;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
|
@ -16,9 +17,17 @@ class PlanController
|
||||||
Response $response,
|
Response $response,
|
||||||
CreatePlan $createPlanUseCase,
|
CreatePlan $createPlanUseCase,
|
||||||
): Response {
|
): 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) ?? [];
|
$data = json_decode((string) $request->getBody(), true) ?? [];
|
||||||
|
|
||||||
$userId = isset($data['userId']) ? (int) $data['userId'] : null;
|
|
||||||
$textId = isset($data['textId']) ? (int) $data['textId'] : null;
|
$textId = isset($data['textId']) ? (int) $data['textId'] : null;
|
||||||
$name = $data['name'] ?? null;
|
$name = $data['name'] ?? null;
|
||||||
$dateStart = $data['dateStart'] ?? null;
|
$dateStart = $data['dateStart'] ?? null;
|
||||||
|
|
@ -26,7 +35,7 @@ class PlanController
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$plan = $createPlanUseCase->execute(new CreatePlanRequest(
|
$plan = $createPlanUseCase->execute(new CreatePlanRequest(
|
||||||
userId: $userId,
|
userId: $user->getId(),
|
||||||
textId: $textId,
|
textId: $textId,
|
||||||
name: $name,
|
name: $name,
|
||||||
dateStart: $dateStart,
|
dateStart: $dateStart,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue