-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement multiple tags filtering per wallet endpoints (#149)
- Loading branch information
Showing
14 changed files
with
933 additions
and
1,118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller\Wallets; | ||
|
||
use App\Database\Charge; | ||
use App\Database\Wallet; | ||
use App\Repository\ChargeRepository; | ||
use App\Repository\WalletRepository; | ||
use App\Service\ChargeWalletService; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Spiral\Auth\AuthScope; | ||
use Spiral\Http\Request\InputManager; | ||
use Spiral\Http\ResponseWrapper; | ||
use Spiral\Router\Annotation\Route; | ||
|
||
final class TotalController extends Controller | ||
{ | ||
public function __construct( | ||
AuthScope $auth, | ||
private ResponseWrapper $response, | ||
private WalletRepository $walletRepository, | ||
private ChargeRepository $chargeRepository, | ||
private ChargeWalletService $chargeWalletService, | ||
) { | ||
parent::__construct($auth); | ||
} | ||
|
||
#[Route(route: '/wallets/<id>/total', name: 'wallet.index.total', methods: 'GET', group: 'auth')] | ||
public function total(string $id, InputManager $input): ResponseInterface | ||
{ | ||
$wallet = $this->walletRepository->findByPKByUserPK((int) $id, (int) $this->user->id); | ||
|
||
if (! $wallet instanceof Wallet) { | ||
return $this->response->create(404); | ||
} | ||
|
||
$this->chargeRepository->filter($input->query->fetch(['date-from', 'date-to'])); | ||
|
||
$income = $this->chargeRepository->totalByWalletPK((int) $wallet->id, Charge::TYPE_INCOME); | ||
$expense = $this->chargeRepository->totalByWalletPK((int) $wallet->id, Charge::TYPE_EXPENSE); | ||
|
||
$response = [ | ||
'totalAmount' => $this->chargeWalletService->totalByIncomeAndExpense($income, $expense), | ||
'totalIncomeAmount' => $income, | ||
'totalExpenseAmount' => $expense, | ||
]; | ||
|
||
$tagIDs = $this->fetchFilteredTagIDs($input); | ||
|
||
if (count($tagIDs) > 0) { | ||
$incomePerTag = $this->chargeRepository->totalByWalletPKGroupByTagPKs((int) $wallet->id, $tagIDs, Charge::TYPE_INCOME); | ||
$expensePerTag = $this->chargeRepository->totalByWalletPKGroupByTagPKs((int) $wallet->id, $tagIDs, Charge::TYPE_EXPENSE); | ||
$response['tags'] = $this->mergeGroupedTotal($tagIDs, $incomePerTag, $expensePerTag); | ||
} | ||
|
||
return $this->response->json([ | ||
'data' => $response, | ||
]); | ||
} | ||
|
||
private function mergeGroupedTotal(array $tagIDs, array $income, array $expense): array | ||
{ | ||
$data = []; | ||
|
||
foreach ($tagIDs as $id) { | ||
if (!array_key_exists($id, $income) && !array_key_exists($id, $expense)) { | ||
continue; | ||
} | ||
|
||
$data[] = [ | ||
'tagId' => $id, | ||
'totalIncomeAmount' => $income[$id] ?? 0.0, | ||
'totalExpenseAmount' => $expense[$id] ?? 0.0, | ||
]; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
Oops, something went wrong.