feat: implement currency rates tracking with models, migrations and views
هذا الالتزام موجود في:
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\City;
|
||||
use App\Models\Currency;
|
||||
use App\Models\Rate;
|
||||
use App\Services\CurrencyService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -14,28 +17,50 @@ class FetchCurrencyCommand extends Command
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(CurrencyService $currencyService) : int
|
||||
public function handle(CurrencyService $currencyService): int
|
||||
{
|
||||
$this->info('Fetching currency data...');
|
||||
|
||||
try {
|
||||
if ($this->option('today')) {
|
||||
$this->fetchTodayData($currencyService);
|
||||
$data = $this->fetchTodayData($currencyService);
|
||||
} else {
|
||||
$this->fetchHistoricalData($currencyService);
|
||||
$data = $this->fetchHistoricalData($currencyService);
|
||||
}
|
||||
|
||||
foreach ($data as $item) {
|
||||
|
||||
$city = match ($item['city']) {
|
||||
'Sanaa' => City::where('name', 'sanaa')->first(),
|
||||
'Aden' => City::where('name', 'aden')->first(),
|
||||
};
|
||||
|
||||
$currency = match ($item['currency']) {
|
||||
'USD' => Currency::where('code', 'USD')->first(),
|
||||
'SAR' => Currency::where('code', 'SAR')->first(),
|
||||
};
|
||||
|
||||
$rate = Rate::updateOrCreate([
|
||||
'city_id' => $city->id,
|
||||
'currency_id' => $currency->id,
|
||||
'date' => $item['date'],
|
||||
], [
|
||||
'buy_price' => $item['price_buy'],
|
||||
'sell_price' => $item['price_sell'],
|
||||
]);
|
||||
}
|
||||
|
||||
$this->info('Currency data fetched successfully!');
|
||||
|
||||
return Command::SUCCESS;
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Failed to fetch currency data: ' . $e->getMessage());
|
||||
$this->error('Failed to fetch currency data: '.$e->getMessage());
|
||||
Log::error('Currency fetch command failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
'trace' => $e->getTraceAsString(),
|
||||
]);
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
@@ -43,14 +68,15 @@ class FetchCurrencyCommand extends Command
|
||||
/**
|
||||
* Fetch and display today's currency data.
|
||||
*
|
||||
* @return void
|
||||
* @return array
|
||||
*/
|
||||
protected function fetchTodayData(CurrencyService $currencyService)
|
||||
{
|
||||
$data = $currencyService->getTodayCurrencies();
|
||||
|
||||
|
||||
if (empty($data)) {
|
||||
$this->warn('No currency data available for today.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -64,23 +90,26 @@ class FetchCurrencyCommand extends Command
|
||||
$item['price_buy'],
|
||||
$item['price_sell'],
|
||||
$item['date'],
|
||||
$item['day']
|
||||
$item['day'],
|
||||
];
|
||||
}, $data)
|
||||
);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and display historical currency data.
|
||||
*
|
||||
* @return void
|
||||
* @return array
|
||||
*/
|
||||
protected function fetchHistoricalData(CurrencyService $currencyService)
|
||||
{
|
||||
$data = $currencyService->getLastTwentyDays();
|
||||
|
||||
|
||||
if (empty($data)) {
|
||||
$this->warn('No historical currency data available.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -94,9 +123,11 @@ class FetchCurrencyCommand extends Command
|
||||
$item['price_buy'],
|
||||
$item['price_sell'],
|
||||
$item['date'],
|
||||
$item['day']
|
||||
$item['day'],
|
||||
];
|
||||
}, $data)
|
||||
);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
34
app/Http/Controllers/RateController.php
Normal file
34
app/Http/Controllers/RateController.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\City;
|
||||
use App\Models\Rate;
|
||||
|
||||
class RateController extends Controller
|
||||
{
|
||||
public function __invoke()
|
||||
{
|
||||
|
||||
$supportedCities = City::query()
|
||||
->whereIn('name', City::supportedCities())
|
||||
->get()
|
||||
->sortByDesc(fn ($city) => $city->name === 'sanaa')
|
||||
->values();
|
||||
|
||||
$rates = Rate::query()
|
||||
->with('currency', 'city')
|
||||
->orderBy('city_id', 'asc')
|
||||
->orderBy('currency_id', 'asc')
|
||||
->orderBy('date', 'desc')
|
||||
->get()
|
||||
->groupBy(fn ($rate) => $rate->city_id.'-'.$rate->currency_id)
|
||||
->map(fn ($group) => $group->first())
|
||||
->groupBy('city_id');
|
||||
|
||||
return view('rates', [
|
||||
'rates' => $rates,
|
||||
'supportedCities' => $supportedCities,
|
||||
]);
|
||||
}
|
||||
}
|
||||
32
app/Models/City.php
Normal file
32
app/Models/City.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class City extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\CityFactory> */
|
||||
use HasFactory;
|
||||
|
||||
const SANAA = 'sanaa';
|
||||
|
||||
const ADEN = 'aden';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'label',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get all supported cities
|
||||
*/
|
||||
public static function supportedCities(): array
|
||||
{
|
||||
return [
|
||||
self::SANAA,
|
||||
self::ADEN,
|
||||
];
|
||||
}
|
||||
}
|
||||
36
app/Models/Currency.php
Normal file
36
app/Models/Currency.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Currency extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\CurrencyFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The supported currency codes.
|
||||
*/
|
||||
const USD = 'USD';
|
||||
|
||||
const SAR = 'SAR';
|
||||
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
'symbol',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get all supported currency codes
|
||||
*/
|
||||
public static function supportedCurrencies(): array
|
||||
{
|
||||
return [
|
||||
self::USD,
|
||||
self::SAR,
|
||||
];
|
||||
}
|
||||
}
|
||||
33
app/Models/Rate.php
Normal file
33
app/Models/Rate.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Rate extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'currency_id',
|
||||
'city_id',
|
||||
'buy_price',
|
||||
'sell_price',
|
||||
'date',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'date' => 'date:Y-m-d',
|
||||
];
|
||||
|
||||
public function currency()
|
||||
{
|
||||
return $this->belongsTo(Currency::class);
|
||||
}
|
||||
|
||||
public function city()
|
||||
{
|
||||
return $this->belongsTo(City::class);
|
||||
}
|
||||
}
|
||||
المرجع في مشكلة جديدة
حظر مستخدم