9409a4dbd9
- Reordered the dispatching of jobs in the runAllFetchs method to ensure proper execution sequence. - Updated API response messages from Turkish to English for consistency and clarity. - Enhanced error messages for data retrieval failures to provide clearer feedback to users. - Improved documentation comments to reflect the changes in functionality. These changes collectively enhance the usability and clarity of the CurrencyController in the Truncgil Finance application.
245 lines
8.3 KiB
PHP
Executable File
245 lines
8.3 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Jobs\FetchCurrencyRates;
|
||
use App\Jobs\FetchGoldRates;
|
||
use App\Jobs\FetchCryptoCurrencyRates;
|
||
use App\Jobs\MergeCurrencyAndGoldRates;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use App\Http\Controllers\Controller;
|
||
|
||
|
||
/**
|
||
* 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.
|
||
* Rate limited to 2 requests per 30 seconds.
|
||
*
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function runAllFetchs()
|
||
{
|
||
try {
|
||
|
||
FetchCurrencyRates::dispatchSync();
|
||
FetchGoldRates::dispatchSync();
|
||
FetchCryptoCurrencyRates::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' => 'Kurlar güncellenirken bir hata oluştu: ' . $e->getMessage()
|
||
], 429);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 🪙 💵 ₿ Retrieves all gold and currency rates
|
||
*
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function getAllRates()
|
||
{
|
||
$jsonFile = 'merged/rates.json';
|
||
|
||
if (Storage::exists($jsonFile)) {
|
||
$data = json_decode(Storage::get($jsonFile), true);
|
||
$currentDate = now();
|
||
$updateDate = isset($data['Update_Date']) ? \Carbon\Carbon::parse($data['Update_Date']) : null;
|
||
|
||
if ($updateDate) {
|
||
$minutesAgo = round($currentDate->diffInMinutes($updateDate), 2);
|
||
$metaData = [
|
||
'Minutes_Ago' => $minutesAgo,
|
||
'Current_Date' => $currentDate->toDateTimeString(),
|
||
'Update_Date' => $updateDate->toDateTimeString(),
|
||
];
|
||
unset($data['Update_Date']);
|
||
$data = [
|
||
'Meta_Data' => $metaData,
|
||
'Rates' => $data
|
||
];
|
||
|
||
return response()->json($data);
|
||
}
|
||
}
|
||
|
||
return response()->json(['error' => 'Data not found'], 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)) {
|
||
$data = json_decode(Storage::get($jsonFile), true);
|
||
$currentDate = now(); // Şu anki tarih
|
||
$updateDate = isset($data['Update_Date']) ? \Carbon\Carbon::parse($data['Update_Date']) : null;
|
||
|
||
if ($updateDate) {
|
||
$minutesAgo = round($currentDate->diffInMinutes($updateDate), 2); // Kaç dakika önce alındığı
|
||
$metaData = [ // Tarih bilgileri için alt dizi
|
||
'Minutes_Ago' => $minutesAgo,
|
||
'Current_Date' => $currentDate->toDateTimeString(),
|
||
'Update_Date' => $updateDate->toDateTimeString(), // Update_Date burada kalacak
|
||
];
|
||
unset($data['Update_Date']); // Rates içerisindeki Update_Date elemanını kaldır
|
||
$data = [
|
||
'Meta_Data' => $metaData, // Tarih bilgileri alt dizisi
|
||
'Rates' => $data, // Kur bilgileri alt dizisi
|
||
];
|
||
}
|
||
|
||
return response()->json($data);
|
||
}
|
||
|
||
return response()->json(['error' => 'Data not found'], 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)) {
|
||
$data = json_decode(Storage::get($jsonFile), true);
|
||
$currentDate = now(); // Şu anki tarih
|
||
$updateDate = isset($data['Update_Date']) ? \Carbon\Carbon::parse($data['Update_Date']) : null;
|
||
|
||
if ($updateDate) {
|
||
$minutesAgo = round($currentDate->diffInMinutes($updateDate), 2); // Kaç dakika önce alındığı
|
||
$metaData = [ // Tarih bilgileri için alt dizi
|
||
'Minutes_Ago' => $minutesAgo,
|
||
'Current_Date' => $currentDate->toDateTimeString(),
|
||
'Update_Date' => $updateDate->toDateTimeString(), // Update_Date burada kalacak
|
||
];
|
||
unset($data['Update_Date']); // Rates içerisindeki Update_Date elemanını kaldır
|
||
$data = [
|
||
'Meta_Data' => $metaData, // Tarih bilgileri alt dizisi
|
||
'Rates' => $data, // Kur bilgileri alt dizisi
|
||
];
|
||
}
|
||
|
||
return response()->json($data);
|
||
}
|
||
|
||
return response()->json(['error' => 'Data not found'], 404);
|
||
}
|
||
|
||
/**
|
||
* ₿ Retrieves the current crypto currency rates
|
||
*
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function getCryptoCurrencyRates()
|
||
{
|
||
$jsonFile = 'crypto/today.json';
|
||
if (Storage::exists($jsonFile)) {
|
||
$data = json_decode(Storage::get($jsonFile), true);
|
||
$currentDate = now();
|
||
$updateDate = isset($data['Update_Date']) ? \Carbon\Carbon::parse($data['Update_Date']) : null;
|
||
|
||
if ($updateDate) {
|
||
$minutesAgo = round($currentDate->diffInMinutes($updateDate), 2);
|
||
$metaData = [
|
||
'Minutes_Ago' => $minutesAgo,
|
||
'Current_Date' => $currentDate->toDateTimeString(),
|
||
'Update_Date' => $updateDate->toDateTimeString(),
|
||
];
|
||
unset($data['Update_Date']);
|
||
$data = [
|
||
'Meta_Data' => $metaData,
|
||
'Rates' => $data,
|
||
];
|
||
}
|
||
|
||
return response()->json($data);
|
||
}
|
||
|
||
return response()->json(['error' => 'Data not found'], 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);
|
||
}
|
||
|
||
/**
|
||
* 🟰 ₿ Retrieves the crypto currency rate by its name
|
||
*
|
||
* @param string $cryptoCurrencyName
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function getCryptoCurrencyRateByName($cryptoCurrencyName)
|
||
{
|
||
$jsonFile = 'crypto/today.json';
|
||
if (Storage::exists($jsonFile)) {
|
||
$data = json_decode(Storage::get($jsonFile), true);
|
||
if (isset($data[$cryptoCurrencyName])) {
|
||
return response()->json([$cryptoCurrencyName => $data[$cryptoCurrencyName]]);
|
||
}
|
||
return response()->json(['error' => 'Cryptocurrency not found'], 404);
|
||
}
|
||
|
||
return response()->json(['error' => 'Data not found'], 404);
|
||
}
|
||
} |