feat: implement pagination and enhance UI with modern design
هذا الالتزام موجود في:
@@ -3,11 +3,13 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -31,5 +33,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
Model::shouldBeStrict(! app()->isProduction());
|
||||
|
||||
Date::use(CarbonImmutable::class);
|
||||
|
||||
Paginator::defaultSimpleView('vendor/pagination/simple-default');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,45 +7,56 @@ use App\Models\Rate;
|
||||
|
||||
class RateService
|
||||
{
|
||||
public function getFormattedRates(): array
|
||||
public function getFormattedRates()
|
||||
{
|
||||
$supportedCities = City::query()
|
||||
->whereIn('name', City::supportedCities()) // Filter only supported cities
|
||||
->get()
|
||||
->sortByDesc(fn($city) => $city->name === 'sanaa') // Sort to put 'sanaa' first
|
||||
->values(); // Reset array keys to be sequential
|
||||
|
||||
// Get the latest exchange rates for each currency in each city
|
||||
|
||||
$latestRates = Rate::query()
|
||||
->whereIn('city_id', $supportedCities->pluck('id'))
|
||||
->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');
|
||||
->orderBy('date', 'desc') // first sort by latest date
|
||||
->orderBy('city_id', 'asc') // then sort by city_id
|
||||
->orderBy('currency_id', 'asc') // then sort by currency_id
|
||||
->simplePaginate(4);
|
||||
|
||||
|
||||
// Format the rates
|
||||
$rates = $supportedCities->map(function ($city) use ($latestRates) {
|
||||
$cityRates = $latestRates->get($city->id, collect())->map(function ($rate) {
|
||||
$transformedRates = $latestRates->getCollection()
|
||||
->map(function($rate) {
|
||||
return [
|
||||
'city_name' => $rate->city->label,
|
||||
'currency_name' => $rate->currency->name,
|
||||
'buy_price' => $rate->buy_price,
|
||||
'sell_price' => $rate->sell_price,
|
||||
'date' => $rate->date->toDateString(),
|
||||
'day' => $rate->date->locale('ar')->isoFormat('dddd'),
|
||||
'is_today' => $rate->date->isToday(),
|
||||
'last_update' => $rate->updated_at->diffForHumans(),
|
||||
];
|
||||
})
|
||||
->groupBy('city_name')
|
||||
->map(function($cityRates, $cityName) {
|
||||
return [
|
||||
'currency' => $rate->currency->name,
|
||||
'buy_price' => $rate->buy_price,
|
||||
'sell_price' => $rate->sell_price,
|
||||
'date' => $rate->date->toDateString(),
|
||||
'day' => $rate->date->locale('ar')->isoFormat('dddd'),
|
||||
'last_update' => $rate->updated_at->diffForHumans(),
|
||||
'city' => $cityName,
|
||||
'rates' => $cityRates->map(function($rate) {
|
||||
return [
|
||||
'currency' => $rate['currency_name'],
|
||||
'buy_price' => $rate['buy_price'],
|
||||
'sell_price' => $rate['sell_price'],
|
||||
'date' => $rate['date'],
|
||||
'day' => $rate['day'],
|
||||
'is_today' => $rate['is_today'],
|
||||
'last_update' => $rate['last_update'],
|
||||
];
|
||||
})->values()->toArray()
|
||||
];
|
||||
})->values()->toArray();
|
||||
})
|
||||
->values(); // Reset keys to be sequential
|
||||
|
||||
return [
|
||||
'city' => $city->label ,
|
||||
'rates' => $cityRates,
|
||||
];
|
||||
})->toArray();
|
||||
$latestRates->setCollection($transformedRates);
|
||||
|
||||
return $rates;
|
||||
return $latestRates;
|
||||
}
|
||||
}
|
||||
|
||||
المرجع في مشكلة جديدة
حظر مستخدم