Skip to content

Commit

Permalink
Fix transaction route not accepting a username
Browse files Browse the repository at this point in the history
  • Loading branch information
schinken committed Jul 15, 2022
1 parent b08300c commit fabe6d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/Controller/Api/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions src/Controller/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
14 changes: 4 additions & 10 deletions src/Service/TransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -247,4 +241,4 @@ private function checkAccountBalanceBoundary(User $user) {
throw new AccountBalanceBoundaryException($user, $balance, $lower);
}
}
}
}

0 comments on commit fabe6d2

Please sign in to comment.