From 141c1fe8cc0a851c8c16f2a8013dbfe84c4d6bff Mon Sep 17 00:00:00 2001 From: wojdylak Date: Sat, 10 Sep 2022 12:22:02 +0200 Subject: [PATCH] Add removeAction --- config/routes.yaml | 4 +++ config/services.yaml | 6 +++- .../Application/RemoveAccountCommand.php | 20 ++++++++++++ .../Application/RemoveAccountHandler.php | 31 +++++++++++++++++++ .../Domain/AccountRepositoryInterface.php | 2 ++ .../Infrastructure/AccountController.php | 9 ++++++ .../DoctrineOrmAccountRepository.php | 12 +++++++ 7 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/Account/Application/RemoveAccountCommand.php create mode 100644 src/Account/Application/RemoveAccountHandler.php diff --git a/config/routes.yaml b/config/routes.yaml index 386475d..89e9b1b 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -8,3 +8,7 @@ account.add: path: /account/add controller: account.controller::addAction +account.remove: + methods: [ DELETE ] + path: /account/remove/{id<\d+>} + controller: account.controller::removeAction diff --git a/config/services.yaml b/config/services.yaml index 482cc40..16ab8b8 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -35,4 +35,8 @@ services: account.add.handler: class: App\Account\Application\AddAccountHandler - arguments: ['@account.repository'] \ No newline at end of file + arguments: ['@account.repository'] + + account.remove.handler: + class: App\Account\Application\RemoveAccountHandler + arguments: [ '@account.repository', '@account.query' ] \ No newline at end of file diff --git a/src/Account/Application/RemoveAccountCommand.php b/src/Account/Application/RemoveAccountCommand.php new file mode 100644 index 0000000..bdccaa2 --- /dev/null +++ b/src/Account/Application/RemoveAccountCommand.php @@ -0,0 +1,20 @@ +id = $id; + } + + public function getId(): int + { + return $this->id; + } +} \ No newline at end of file diff --git a/src/Account/Application/RemoveAccountHandler.php b/src/Account/Application/RemoveAccountHandler.php new file mode 100644 index 0000000..fb032ee --- /dev/null +++ b/src/Account/Application/RemoveAccountHandler.php @@ -0,0 +1,31 @@ +accountRepository = $accountRepository; + $this->accountQuery = $accountQuery; + } + + public static function getDefaultIndexName(): string + { + return RemoveAccountCommand::class; + } + + public function __invoke(RemoveAccountCommand $cmd): void + { + $account = $this->accountQuery->findById($cmd->getId()); + + $this->accountRepository->remove($account); + } +} \ No newline at end of file diff --git a/src/Account/Domain/AccountRepositoryInterface.php b/src/Account/Domain/AccountRepositoryInterface.php index 2065576..ff0f3b5 100644 --- a/src/Account/Domain/AccountRepositoryInterface.php +++ b/src/Account/Domain/AccountRepositoryInterface.php @@ -7,4 +7,6 @@ interface AccountRepositoryInterface public function findById(int $id): Account; public function add(Account $account); + + public function remove(Account $account); } \ No newline at end of file diff --git a/src/Account/Infrastructure/AccountController.php b/src/Account/Infrastructure/AccountController.php index 04d64ac..fc3ca41 100644 --- a/src/Account/Infrastructure/AccountController.php +++ b/src/Account/Infrastructure/AccountController.php @@ -4,6 +4,7 @@ use App\Account\Application\AccountQueryInterface; use App\Account\Application\AddAccountCommand; +use App\Account\Application\RemoveAccountCommand; use App\Account\Application\ViewAccountCommand; use App\Shared\Infrastructure\CommandBusInterface; use Symfony\Component\HttpFoundation\JsonResponse; @@ -35,4 +36,12 @@ public function addAction(Request $request): Response return new Response(null, Response::HTTP_CREATED); } + + public function deleteAction(int $id): Response + { + $command = new RemoveAccountCommand($id); + $this->bus->handle($command); + + return new Response(null); + } } \ No newline at end of file diff --git a/src/Account/Infrastructure/DoctrineOrmAccountRepository.php b/src/Account/Infrastructure/DoctrineOrmAccountRepository.php index fe58e8e..d3cc2fa 100644 --- a/src/Account/Infrastructure/DoctrineOrmAccountRepository.php +++ b/src/Account/Infrastructure/DoctrineOrmAccountRepository.php @@ -31,4 +31,16 @@ public function add(Account $account) $this->em->rollback(); } } + + public function remove(Account $account) + { + $this->em->beginTransaction(); + try { + $this->em->remove($account); + $this->em->flush(); + $this->em->commit(); + } catch (\Exception $exception) { + $this->em->rollback(); + } + } } \ No newline at end of file