diff --git a/config/packages/security.yaml b/config/packages/security.yaml index ac96a76..cd4405c 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -17,4 +17,4 @@ security: access_control: - { path: '^/', roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: [GET] } - - { path: '^/', roles: ROLE_ADMIN, methods: [POST] } + - { path: '^/', roles: ROLE_ADMIN, methods: [PATCH, POST] } diff --git a/config/routes.yaml b/config/routes.yaml index 1ac273c..cb3cdc8 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -14,3 +14,10 @@ create_article: path: /articles methods: [POST] controller: App\Application\Controller\ArticleController::create + +publish_article: + path: /articles/{id}/publish + methods: [PATCH] + controller: App\Application\Controller\ArticleController::publish + requirements: + id: '[\d]+' diff --git a/config/services/bus.yaml b/config/services/bus.yaml index 715751a..f6408e2 100644 --- a/config/services/bus.yaml +++ b/config/services/bus.yaml @@ -5,3 +5,4 @@ services: - '@App\Domain\Command\Handler\ReadArticle' - '@App\Domain\Command\Handler\ListArticles' - '@App\Domain\Command\Handler\WriteArticle' + - '@App\Domain\Command\Handler\PublishArticle' diff --git a/config/services/command_handlers.yaml b/config/services/command_handlers.yaml index f6060b6..81e8308 100644 --- a/config/services/command_handlers.yaml +++ b/config/services/command_handlers.yaml @@ -12,5 +12,10 @@ services: App\Domain\Command\Handler\WriteArticle: arguments: - - '@doctrine.orm.entity_manager' + - '@Doctrine\ORM\EntityManagerInterface' - '@Ausi\SlugGenerator\SlugGenerator' + + App\Domain\Command\Handler\PublishArticle: + arguments: + - '@Doctrine\ORM\EntityManagerInterface' + - '@App\Application\Repository\ArticleRepository' diff --git a/config/services/endpoints.yaml b/config/services/endpoints.yaml index fb3150c..ea49a92 100644 --- a/config/services/endpoints.yaml +++ b/config/services/endpoints.yaml @@ -2,6 +2,6 @@ services: App\Application\Controller\ArticleController: arguments: - '@App\Domain\Command\Bus\SimpleBus' - - '@form.factory' + - '@Symfony\Component\Form\FormFactoryInterface' tags: - 'controller.service_arguments' diff --git a/src/Application/Controller/ArticleController.php b/src/Application/Controller/ArticleController.php index f52a60a..7c348ac 100644 --- a/src/Application/Controller/ArticleController.php +++ b/src/Application/Controller/ArticleController.php @@ -107,4 +107,20 @@ public function create(Request $request): JsonResponse Response::HTTP_CREATED ); } + + // @todo add swager doc here + public function publish(int $id): JsonResponse + { + try { + $article = $this->bus->executeCommand(new Command\PublishArticle($id)); + } + catch (Exception $exception) { + return new JsonResponse( + ["error" => $exception->getMessage()], + Response::HTTP_INTERNAL_SERVER_ERROR + ); + } + + return new JsonResponse(new ArticleDefinition($article)); + } } diff --git a/src/Application/Controller/Endpoint.php b/src/Application/Controller/Endpoint.php index ad21fcc..adcdbde 100644 --- a/src/Application/Controller/Endpoint.php +++ b/src/Application/Controller/Endpoint.php @@ -3,10 +3,13 @@ namespace App\Application\Controller; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; interface Endpoint { public function list(): JsonResponse; public function read(string $slug): JsonResponse; + + public function create(Request $request): JsonResponse; } diff --git a/src/Domain/Command/Handler/PublishArticle.php b/src/Domain/Command/Handler/PublishArticle.php new file mode 100644 index 0000000..b389819 --- /dev/null +++ b/src/Domain/Command/Handler/PublishArticle.php @@ -0,0 +1,49 @@ +manager = $manager; + $this->repository = $repository; + } + + /** + * @throws ResourceNotFoundException + */ + public function __invoke(PublishArticleCommand $command): Article + { + $article = $this->repository->find($command->getId()); + + if (null === $article) { + throw new ResourceNotFoundException(); + } + + $article->publish(); + + $this->manager->persist($article); + $this->manager->flush(); + + return $article; + } +} diff --git a/src/Domain/Command/PublishArticle.php b/src/Domain/Command/PublishArticle.php new file mode 100644 index 0000000..8d9c782 --- /dev/null +++ b/src/Domain/Command/PublishArticle.php @@ -0,0 +1,21 @@ +id = $id; + } + + public function getId(): int + { + return $this->id; + } +} diff --git a/src/Domain/Exception/ResourceNotFoundException.php b/src/Domain/Exception/ResourceNotFoundException.php new file mode 100644 index 0000000..bb926c7 --- /dev/null +++ b/src/Domain/Exception/ResourceNotFoundException.php @@ -0,0 +1,10 @@ +draft = false; + + return $this; + } + public function getId(): int { return $this->id;