From 74d2e3e60201c9f35dc705a55e0385d1d00f2128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Tue, 21 Jan 2025 11:06:42 +0300 Subject: [PATCH] - Updated CurrencyController to enhance API responses for currency and gold rates. - Added metadata to the responses, including timestamps and the time since the last update, improving data context for users. - Enhanced method descriptions with relevant emojis for better readability and understanding. - Refactored response handling to ensure consistent JSON structure across all rate retrieval methods. - These changes improve the clarity and usability of the API, providing users with more informative responses. --- app/Http/Controllers/CurrencyController.php | 79 ++++++++++++++++----- 1 file changed, 63 insertions(+), 16 deletions(-) mode change 100644 => 100755 app/Http/Controllers/CurrencyController.php diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php old mode 100644 new mode 100755 index 2a6a0e3..0a6f1e9 --- a/app/Http/Controllers/CurrencyController.php +++ b/app/Http/Controllers/CurrencyController.php @@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Storage; class CurrencyController extends Controller { /** - * Dispatches all jobs to fetch currency and gold rates synchronously. + * ⚙️ Dispatches all jobs to fetch currency and gold rates synchronously. * * @return \Illuminate\Http\JsonResponse */ @@ -39,26 +39,41 @@ class CurrencyController extends Controller } /** - * Retrieves all gold and currency rates + * 🪙 💵 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) - ); + $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' => 'Veri bulunamadı'], 404); } /** - * Retrieves the current currency rates + * 💵 Retrieves the current currency rates * * @return \Illuminate\Http\JsonResponse */ @@ -67,16 +82,32 @@ class CurrencyController extends Controller // JSON dosyasından oku $jsonFile = 'currency/today.json'; if (Storage::exists($jsonFile)) { - return response()->json( - json_decode(Storage::get($jsonFile), true) - ); + $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' => 'Veri bulunamadı'], 404); } /** - * Retrieves the current gold rates + * 🪙 Retrieves the current gold rates * * @return \Illuminate\Http\JsonResponse */ @@ -85,16 +116,32 @@ class CurrencyController extends Controller // JSON dosyasından oku $jsonFile = 'gold/today.json'; if (Storage::exists($jsonFile)) { - return response()->json( - json_decode(Storage::get($jsonFile), true) - ); + $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' => 'Veri bulunamadı'], 404); } /** - * Retrieves the currency rate by its name + * 🟰 💵 Retrieves the currency rate by its name * * @param string $currencyName * @return \Illuminate\Http\JsonResponse @@ -115,7 +162,7 @@ class CurrencyController extends Controller } /** - * Retrieves the gold rate by its name + * 🟰 🪙 Retrieves the gold rate by its name * * @param string $goldName * @return \Illuminate\Http\JsonResponse