Refactor CurrencyController to enhance rate fetching and introduce new methods

- Replaced the `getCurrentRates` method with `runAllFetchs` to dispatch fetching jobs for currency and gold rates synchronously.
- Added `getAllRates`, `getGoldRates`, and `getGoldRateByName` methods to improve data retrieval from respective JSON files.
- Updated error messages for clarity and consistency.
- Streamlined JSON file handling by using variables for file paths, enhancing maintainability.

These changes improve the CurrencyController's functionality and organization, facilitating better management of financial data.
This commit is contained in:
Ümit Tunç
2025-01-17 23:28:29 +03:00
parent 7f09c56967
commit a26622c550
+58 -7
View File
@@ -9,7 +9,15 @@ use Illuminate\Support\Facades\Storage;
class CurrencyController extends Controller class CurrencyController extends Controller
{ {
public function getCurrentRates()
public function runAllFetchs()
{
FetchCurrencyRates::dispatchSync();
FetchGoldRates::dispatchSync();
MergeCurrencyAndGoldRates::dispatchSync();
}
public function getAllRates()
{ {
// Job'ı çalıştır // Job'ı çalıştır
/* /*
@@ -18,9 +26,36 @@ class CurrencyController extends Controller
MergeCurrencyAndGoldRates::dispatchSync(); MergeCurrencyAndGoldRates::dispatchSync();
*/ */
// JSON dosyasından oku // JSON dosyasından oku
if (Storage::exists('merged/rates.json')) { $jsonFile = 'merged/rates.json';
if (Storage::exists($jsonFile)) {
return response()->json( return response()->json(
json_decode(Storage::get('merged/rates.json'), true) json_decode(Storage::get($jsonFile), true)
);
}
return response()->json(['error' => 'Veri bulunamadı'], 404);
}
public function getCurrentRates()
{
// JSON dosyasından oku
$jsonFile = 'currency/today.json';
if (Storage::exists($jsonFile)) {
return response()->json(
json_decode(Storage::get($jsonFile), true)
);
}
return response()->json(['error' => 'Veri bulunamadı'], 404);
}
public function getGoldRates()
{
// JSON dosyasından oku
$jsonFile = 'gold/today.json';
if (Storage::exists($jsonFile)) {
return response()->json(
json_decode(Storage::get($jsonFile), true)
); );
} }
@@ -30,14 +65,30 @@ class CurrencyController extends Controller
public function getCurrencyRateByName($currencyName) public function getCurrencyRateByName($currencyName)
{ {
// JSON dosyasından oku // JSON dosyasından oku
if (Storage::exists('currency/today.json')) { $jsonFile = 'currency/today.json';
$data = json_decode(Storage::get('currency/today.json'), true); if (Storage::exists($jsonFile)) {
$data = json_decode(Storage::get($jsonFile), true);
if (isset($data[$currencyName])) { if (isset($data[$currencyName])) {
return response()->json([$currencyName => $data[$currencyName]]); return response()->json([$currencyName => $data[$currencyName]]);
} }
return response()->json(['error' => 'Para birimi bulunamadı'], 404); return response()->json(['error' => 'Currency not found'], 404);
} }
return response()->json(['error' => 'Veri bulunamadı'], 404); return response()->json(['error' => 'Data not found'], 404);
}
public function getGoldRateByName($goldName)
{
// JSON dosyasından oku
$jsonFile = 'gold/today.json';
if (Storage::exists($jsonFile)) {
$data = json_decode(Storage::get($jsonFile), true);
if (isset($data[$goldName])) {
return response()->json([$goldName => $data[$goldName]]);
}
return response()->json(['error' => 'Currency not found'], 404);
}
return response()->json(['error' => 'Data not found'], 404);
} }
} }