From 3a95db3bc3cf513b28f8dad2442af4e96bb10e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Drunen?= Date: Wed, 27 Dec 2023 00:11:25 +0100 Subject: [PATCH] Get rid of CurrencyRepository --- .../Controllers/TransactionController.php | 16 ++++-- app/Jobs/FetchConversionRate.php | 21 +++----- app/Jobs/FetchConversionRates.php | 10 ++-- app/Repositories/CurrencyRepository.php | 32 ------------ .../Repositories/CurrencyRepositoryTest.php | 49 ------------------- 5 files changed, 22 insertions(+), 106 deletions(-) delete mode 100644 app/Repositories/CurrencyRepository.php delete mode 100644 tests/Unit/Repositories/CurrencyRepositoryTest.php diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index d1d878c3..b8ae1b5a 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -2,9 +2,9 @@ namespace App\Http\Controllers; +use App\Models\Currency; use App\Models\Space; use App\Models\Tag; -use App\Repositories\CurrencyRepository; use App\Repositories\RecurringRepository; use App\Repositories\TransactionRepository; use Illuminate\Http\Request; @@ -12,16 +12,13 @@ class TransactionController extends Controller { - private $currencyRepository; private $recurringRepository; public function __construct( TransactionRepository $transactionRepository, - CurrencyRepository $currencyRepository, RecurringRepository $recurringRepository ) { $this->repository = $transactionRepository; - $this->currencyRepository = $currencyRepository; $this->recurringRepository = $recurringRepository; } @@ -50,9 +47,18 @@ public function create() ]; } + $spaceCurrencyId = Space::find(session('space_id'))->currency_id; + + $currencies = Currency::select('currencies.*') + ->leftJoin('conversion_rates AS cr', 'cr.base_currency_id', '=', 'currencies.id') + ->where('cr.target_currency_id', $spaceCurrencyId) + ->orWhere('currencies.id', $spaceCurrencyId) + ->groupBy('currencies.id') + ->get(); + return view('transactions.create', [ 'tags' => $tags, - 'currencies' => $this->currencyRepository->getIfConversionRatePresent(), + 'currencies' => $currencies, 'defaultTransactionType' => Auth::user()->default_transaction_type, 'firstDayOfWeek' => Auth::user()->first_day_of_week, 'defaultCurrencyId' => Space::find(session('space_id'))->currency_id, diff --git a/app/Jobs/FetchConversionRate.php b/app/Jobs/FetchConversionRate.php index 35e69024..9fe51617 100644 --- a/app/Jobs/FetchConversionRate.php +++ b/app/Jobs/FetchConversionRate.php @@ -2,8 +2,8 @@ namespace App\Jobs; +use App\Models\Currency; use App\Repositories\ConversionRateRepository; -use App\Repositories\CurrencyRepository; use Exception; use GuzzleHttp\Client; use Illuminate\Bus\Queueable; @@ -19,24 +19,19 @@ class FetchConversionRate implements ShouldQueue use Queueable; use SerializesModels; - protected $baseCurrencyId; - - private $currencyRepository; private $conversionRateRepository; - public function __construct(int $baseCurrencyId) + public function __construct(protected int $baseCurrencyId) { - $this->baseCurrencyId = $baseCurrencyId; + // } - public function handle( - CurrencyRepository $currencyRepository, - ConversionRateRepository $conversionRateRepository - ): void { - $this->currencyRepository = $currencyRepository; + public function handle(ConversionRateRepository $conversionRateRepository): void + { $this->conversionRateRepository = $conversionRateRepository; - $baseCurrency = $this->currencyRepository->getById($this->baseCurrencyId); + /** @var Currency $baseCurrency */ + $baseCurrency = Currency::query()->find($this->baseCurrencyId); if (!$baseCurrency || !$baseCurrency->iso) { return; @@ -44,7 +39,7 @@ public function handle( $client = new Client(); - foreach ($this->currencyRepository->getAll() as $targetCurrency) { + foreach (Currency::query()->get() as $targetCurrency) { if (!$targetCurrency->iso || $baseCurrency->iso === $targetCurrency->iso) { continue; } diff --git a/app/Jobs/FetchConversionRates.php b/app/Jobs/FetchConversionRates.php index 9bad5d31..eebd8f56 100644 --- a/app/Jobs/FetchConversionRates.php +++ b/app/Jobs/FetchConversionRates.php @@ -2,7 +2,7 @@ namespace App\Jobs; -use App\Repositories\CurrencyRepository; +use App\Models\Currency; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -16,13 +16,9 @@ class FetchConversionRates implements ShouldQueue use Queueable; use SerializesModels; - private $currencyRepository; - - public function handle(CurrencyRepository $currencyRepository): void + public function handle(): void { - $this->currencyRepository = $currencyRepository; - - foreach ($this->currencyRepository->getAll() as $currency) { + foreach (Currency::query()->get() as $currency) { FetchConversionRate::dispatch($currency->id); } } diff --git a/app/Repositories/CurrencyRepository.php b/app/Repositories/CurrencyRepository.php deleted file mode 100644 index 3fb40aec..00000000 --- a/app/Repositories/CurrencyRepository.php +++ /dev/null @@ -1,32 +0,0 @@ -currency_id]; - - return Currency::select('currencies.*') - ->leftJoin('conversion_rates AS cr', 'cr.base_currency_id', '=', 'currencies.id') - ->where('cr.target_currency_id', $spaceCurrencyId) - ->orWhere('currencies.id', $spaceCurrencyId) - ->groupBy('currencies.id') - ->get(); - } -} diff --git a/tests/Unit/Repositories/CurrencyRepositoryTest.php b/tests/Unit/Repositories/CurrencyRepositoryTest.php deleted file mode 100644 index 30be674d..00000000 --- a/tests/Unit/Repositories/CurrencyRepositoryTest.php +++ /dev/null @@ -1,49 +0,0 @@ -currencyRepository = new CurrencyRepository(); - } - - public function testGetIfConversionRatePresentMethod(): void - { - // Prepare - $firstCurrency = Currency::factory()->create(); - $secondCurrency = Currency::factory()->create(); - - $firstSpace = Space::factory()->create(['currency_id' => $firstCurrency->id]); - $secondSpace = Space::factory()->create(['currency_id' => $secondCurrency->id]); - - // Select first currency, assuming there are no conversion rates yet, the method should return 1 currency - $this->session(['space_id' => $firstSpace->id]); - - $this->assertCount(1, $this->currencyRepository->getIfConversionRatePresent()); - - // Create conversion rate, the method should return 2 currencies - ConversionRate::factory()->create([ - 'base_currency_id' => $secondCurrency, - 'target_currency_id' => $firstCurrency - ]); - - $this->assertCount(2, $this->currencyRepository->getIfConversionRatePresent()); - - // Switch to the other currency, the method should return 1 currency - $this->session(['space_id' => $secondSpace->id]); - - $this->assertCount(1, $this->currencyRepository->getIfConversionRatePresent()); - } -}