a26622c550
- 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.
94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Jobs\FetchCurrencyRates;
|
||
use App\Jobs\FetchGoldRates;
|
||
use App\Jobs\MergeCurrencyAndGoldRates;
|
||
use Illuminate\Support\Facades\Storage;
|
||
|
||
class CurrencyController extends Controller
|
||
{
|
||
|
||
public function runAllFetchs()
|
||
{
|
||
FetchCurrencyRates::dispatchSync();
|
||
FetchGoldRates::dispatchSync();
|
||
MergeCurrencyAndGoldRates::dispatchSync();
|
||
}
|
||
|
||
public function getAllRates()
|
||
{
|
||
// Job'ı çalıştır
|
||
/*
|
||
FetchCurrencyRates::dispatchSync();
|
||
FetchGoldRates::dispatchSync();
|
||
MergeCurrencyAndGoldRates::dispatchSync();
|
||
*/
|
||
// JSON dosyasından oku
|
||
$jsonFile = 'merged/rates.json';
|
||
if (Storage::exists($jsonFile)) {
|
||
return response()->json(
|
||
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)
|
||
);
|
||
}
|
||
|
||
return response()->json(['error' => 'Veri bulunamadı'], 404);
|
||
}
|
||
|
||
public function getCurrencyRateByName($currencyName)
|
||
{
|
||
// JSON dosyasından oku
|
||
$jsonFile = 'currency/today.json';
|
||
if (Storage::exists($jsonFile)) {
|
||
$data = json_decode(Storage::get($jsonFile), true);
|
||
if (isset($data[$currencyName])) {
|
||
return response()->json([$currencyName => $data[$currencyName]]);
|
||
}
|
||
return response()->json(['error' => 'Currency not found'], 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);
|
||
}
|
||
} |