63aa362100
- Wrapped job dispatching calls in a try-catch block to handle potential exceptions during the fetching of currency and gold rates. - Added JSON response for both success and error scenarios, improving the API's feedback mechanism for clients. - Updated method documentation to reflect the return type of the method. These changes improve the robustness and user experience of the currency and gold rates update process.
137 lines
3.8 KiB
PHP
137 lines
3.8 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
|
||
*
|
||
* This controller handles the fetching and returning of currency and gold rates.
|
||
*/
|
||
class CurrencyController extends Controller
|
||
{
|
||
/**
|
||
* Dispatches all jobs to fetch currency and gold rates synchronously.
|
||
*
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function runAllFetchs()
|
||
{
|
||
try {
|
||
FetchCurrencyRates::dispatchSync();
|
||
FetchGoldRates::dispatchSync();
|
||
MergeCurrencyAndGoldRates::dispatchSync();
|
||
|
||
return response()->json([
|
||
'status' => 'success',
|
||
'message' => 'All currency and gold rates have been successfully updated'
|
||
]);
|
||
} catch (\Exception $e) {
|
||
return response()->json([
|
||
'status' => 'error',
|
||
'message' => 'An error occurred while updating rates: ' . $e->getMessage()
|
||
], 500);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Retrieves all gold and currency rates
|
||
*
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function getAllRates()
|
||
{
|
||
|
||
// 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);
|
||
}
|
||
|
||
/**
|
||
* Retrieves the current currency rates
|
||
*
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
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);
|
||
}
|
||
|
||
/**
|
||
* Retrieves the current gold rates
|
||
*
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
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);
|
||
}
|
||
|
||
/**
|
||
* Retrieves the currency rate by its name
|
||
*
|
||
* @param string $currencyName
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
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);
|
||
}
|
||
|
||
/**
|
||
* Retrieves the gold rate by its name
|
||
*
|
||
* @param string $goldName
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
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);
|
||
}
|
||
} |