From fabe6d298757d171b2808712a26ba1fd147d3337 Mon Sep 17 00:00:00 2001 From: Christopher Schirner Date: Fri, 15 Jul 2022 12:26:28 +0200 Subject: [PATCH] Fix transaction route not accepting a username --- src/Controller/Api/TransactionController.php | 9 +++++++-- src/Controller/Api/UserController.php | 2 -- src/Service/TransactionService.php | 14 ++++---------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Controller/Api/TransactionController.php b/src/Controller/Api/TransactionController.php index 0f52ef4..03b88b3 100644 --- a/src/Controller/Api/TransactionController.php +++ b/src/Controller/Api/TransactionController.php @@ -49,7 +49,7 @@ function list(Request $request, EntityManagerInterface $entityManager) { /** * @Route("/user/{userId}/transaction", methods="POST") */ - function createUserTransactions($userId, Request $request, TransactionService $transactionService) { + function createUserTransactions($userId, Request $request, TransactionService $transactionService, EntityManagerInterface $entityManager) { $amount = $request->request->get('amount'); $quantity = $request->request->get('quantity'); $comment = $request->request->get('comment'); @@ -60,7 +60,12 @@ function createUserTransactions($userId, Request $request, TransactionService $t throw new ParameterInvalidException('comment'); } - $transaction = $transactionService->doTransaction($userId, $amount, $comment, $quantity, $articleId, $recipientId); + $user = $entityManager->getRepository(User::class)->find($userId); + if (!$user) { + throw new UserNotFoundException($userId); + } + + $transaction = $transactionService->doTransaction($user, $amount, $comment, $quantity, $articleId, $recipientId); return $this->json([ 'transaction' => $this->transactionSerializer->serialize($transaction), diff --git a/src/Controller/Api/UserController.php b/src/Controller/Api/UserController.php index 969df7a..6f32e35 100644 --- a/src/Controller/Api/UserController.php +++ b/src/Controller/Api/UserController.php @@ -127,7 +127,6 @@ function search(Request $request, EntityManagerInterface $entityManager) { */ function user($userId, EntityManagerInterface $entityManager) { $user = $entityManager->getRepository(User::class)->findByIdentifier($userId); - if (!$user) { throw new UserNotFoundException($userId); } @@ -142,7 +141,6 @@ function user($userId, EntityManagerInterface $entityManager) { */ function updateUser($userId, Request $request, EntityManagerInterface $entityManager) { $user = $entityManager->getRepository(User::class)->findByIdentifier($userId); - if (!$user) { throw new UserNotFoundException($userId); } diff --git a/src/Service/TransactionService.php b/src/Service/TransactionService.php index d111590..e446a1f 100644 --- a/src/Service/TransactionService.php +++ b/src/Service/TransactionService.php @@ -57,7 +57,7 @@ function isDeletable(Transaction $transaction): bool { } /** - * @param int $userId + * @param User $user * @param int|null $amount * @param null|string $comment * @param int|null $quantity @@ -69,20 +69,14 @@ function isDeletable(Transaction $transaction): bool { * @throws ParameterNotFoundException * @return Transaction */ - function doTransaction(int $userId, ?int $amount, string $comment = null, ?int $quantity = 1, ?int $articleId = null, ?int $recipientId = null): Transaction { + function doTransaction(User $user, ?int $amount, string $comment = null, ?int $quantity = 1, ?int $articleId = null, ?int $recipientId = null): Transaction { if (($recipientId || $articleId) && $amount > 0) { throw new TransactionInvalidException('Amount can\'t be positive when sending money or buying an article'); } - return $this->entityManager->transactional(function () use ($userId, $amount, $comment, $quantity, $articleId, $recipientId) { + return $this->entityManager->transactional(function () use ($user, $amount, $comment, $quantity, $articleId, $recipientId) { $transaction = new Transaction(); - - $user = $this->entityManager->getRepository(User::class)->find($userId, LockMode::PESSIMISTIC_WRITE); - if (!$user) { - throw new UserNotFoundException($userId); - } - $transaction->setUser($user); $transaction->setComment($comment); @@ -247,4 +241,4 @@ private function checkAccountBalanceBoundary(User $user) { throw new AccountBalanceBoundaryException($user, $balance, $lower); } } -} \ No newline at end of file +}