Skip to content

Commit

Permalink
Add removeAction
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojdylak committed Sep 10, 2022
1 parent 1446e06 commit 141c1fe
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ services:

account.add.handler:
class: App\Account\Application\AddAccountHandler
arguments: ['@account.repository']
arguments: ['@account.repository']

account.remove.handler:
class: App\Account\Application\RemoveAccountHandler
arguments: [ '@account.repository', '@account.query' ]
20 changes: 20 additions & 0 deletions src/Account/Application/RemoveAccountCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Account\Application;

use App\Shared\Application\CommandInterface;

final class RemoveAccountCommand implements CommandInterface
{
private int $id;

public function __construct(int $id)
{
$this->id = $id;
}

public function getId(): int
{
return $this->id;
}
}
31 changes: 31 additions & 0 deletions src/Account/Application/RemoveAccountHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Account\Application;

use App\Account\Domain\AccountRepositoryInterface;
use App\Shared\Application\HandlerInterface;

final class RemoveAccountHandler implements HandlerInterface
{

private AccountRepositoryInterface $accountRepository;
private AccountQueryInterface $accountQuery;

public function __construct(AccountRepositoryInterface $accountRepository, AccountQueryInterface $accountQuery)
{
$this->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);
}
}
2 changes: 2 additions & 0 deletions src/Account/Domain/AccountRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ interface AccountRepositoryInterface
public function findById(int $id): Account;

public function add(Account $account);

public function remove(Account $account);
}
9 changes: 9 additions & 0 deletions src/Account/Infrastructure/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
12 changes: 12 additions & 0 deletions src/Account/Infrastructure/DoctrineOrmAccountRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

0 comments on commit 141c1fe

Please sign in to comment.