add plan controller

This commit is contained in:
Yisroel Baum 2026-04-24 10:25:53 +03:00
parent 73f3cab813
commit 0e57b90509
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,55 @@
<?php
namespace App\Plan;
use App\Exceptions\BadRequestException;
use App\Plan\UseCases\CreatePlan;
use App\Plan\UseCases\CreatePlanRequest;
use DomainException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class PlanController
{
public function createPlan(
Request $request,
Response $response,
CreatePlan $createPlanUseCase,
): Response {
$data = $request->getParsedBody();
$userId = $data['userId'] ?? null;
$textId = $data['textId'] ?? null;
$name = $data['name'] ?? null;
$dateStart = $data['dateStart'] ?? null;
$dateEnd = $data['dateEnd'] ?? null;
try {
$plan = $createPlanUseCase->execute(new CreatePlanRequest(
userId: $userId,
textId: $textId,
name: $name,
dateStart: $dateStart,
dateEnd: $dateEnd,
));
} 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');
}
$response->getBody()->write(json_encode([
'id' => $plan->getId(),
'name' => $plan->getName(),
]));
return $response->withStatus(201)
->withHeader('Content-Type', 'application/json');
}
}