feat: implement currency rates tracking with models, migrations and views
هذا الالتزام موجود في:
40
database/factories/CityFactory.php
Normal file
40
database/factories/CityFactory.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\City;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\City>
|
||||
*/
|
||||
class CityFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'name' => fake()->randomElement(City::supportedCities()),
|
||||
];
|
||||
}
|
||||
|
||||
public function sanaa()
|
||||
{
|
||||
return $this->state([
|
||||
'name' => City::SANAA,
|
||||
'label' => 'صنعاء',
|
||||
]);
|
||||
}
|
||||
|
||||
public function aden()
|
||||
{
|
||||
return $this->state([
|
||||
'name' => City::ADEN,
|
||||
'label' => 'عدن',
|
||||
]);
|
||||
}
|
||||
}
|
||||
56
database/factories/CurrencyFactory.php
Normal file
56
database/factories/CurrencyFactory.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Currency;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Currency>
|
||||
*/
|
||||
class CurrencyFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$code = fake()->randomElement(Currency::supportedCurrencies());
|
||||
|
||||
$name = match ($code) {
|
||||
Currency::USD => 'دولار أمريكي',
|
||||
Currency::SAR => 'ريال سعودي',
|
||||
};
|
||||
|
||||
$symbol = match ($code) {
|
||||
Currency::USD => '$',
|
||||
Currency::SAR => '﷼',
|
||||
};
|
||||
|
||||
return [
|
||||
'code' => $code,
|
||||
'name' => $name,
|
||||
'symbol' => $symbol,
|
||||
];
|
||||
}
|
||||
|
||||
public function saudiRial()
|
||||
{
|
||||
return $this->state([
|
||||
'code' => Currency::SAR,
|
||||
'name' => 'ريال سعودي',
|
||||
'symbol' => '﷼',
|
||||
]);
|
||||
}
|
||||
|
||||
public function usdDollar()
|
||||
{
|
||||
return $this->state([
|
||||
'code' => Currency::USD,
|
||||
'name' => 'دولار أمريكي',
|
||||
'symbol' => '$',
|
||||
]);
|
||||
}
|
||||
}
|
||||
63
database/factories/RateFactory.php
Normal file
63
database/factories/RateFactory.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\City;
|
||||
use App\Models\Currency;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Rate>
|
||||
*/
|
||||
class RateFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'currency_id' => Currency::factory(),
|
||||
'city_id' => City::factory(),
|
||||
|
||||
'buy_price' => fn ($attributes) => $attributes['currency_id'] == Currency::SAR
|
||||
? fake()->randomFloat(2, 1, 300)
|
||||
: fake()->randomFloat(2, 500, 1000),
|
||||
'sell_price' => fn ($attributes) => $attributes['currency_id'] == Currency::SAR
|
||||
? fake()->randomFloat(2, 1, 300)
|
||||
: fake()->randomFloat(2, 500, 1000),
|
||||
|
||||
'date' => fake()->dateTimeBetween('-20 days', 'today'),
|
||||
];
|
||||
}
|
||||
|
||||
public function withDate($date)
|
||||
{
|
||||
return $this->state([
|
||||
'date' => $date,
|
||||
]);
|
||||
}
|
||||
|
||||
public function today()
|
||||
{
|
||||
return $this->state([
|
||||
'date' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function yesterday()
|
||||
{
|
||||
return $this->state([
|
||||
'date' => now()->subDay(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function last20Days()
|
||||
{
|
||||
return $this->state([
|
||||
'date' => fake()->unique()->dateTimeBetween('-20 days', 'yesterday'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('currencies', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('code');
|
||||
$table->string('name');
|
||||
$table->string('symbol');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('currencies');
|
||||
}
|
||||
};
|
||||
34
database/migrations/2025_05_23_235710_create_rates_table.php
Normal file
34
database/migrations/2025_05_23_235710_create_rates_table.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use App\Models\City;
|
||||
use App\Models\Currency;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('rates', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(Currency::class);
|
||||
$table->foreignIdFor(City::class);
|
||||
$table->decimal('buy_price');
|
||||
$table->decimal('sell_price');
|
||||
$table->date('date');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('rates');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('cities', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('label');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('cities');
|
||||
}
|
||||
};
|
||||
18
database/seeders/CitySeeder.php
Normal file
18
database/seeders/CitySeeder.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\City;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class CitySeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
City::factory()->sanaa()->create();
|
||||
City::factory()->aden()->create();
|
||||
}
|
||||
}
|
||||
18
database/seeders/CurrencySeeder.php
Normal file
18
database/seeders/CurrencySeeder.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Currency;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class CurrencySeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Currency::factory()->saudiRial()->create();
|
||||
Currency::factory()->usdDollar()->create();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\City;
|
||||
use App\Models\Currency;
|
||||
use App\Models\Rate;
|
||||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
@@ -13,11 +15,19 @@ class DatabaseSeeder extends Seeder
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// User::factory(10)->create();
|
||||
// currencies
|
||||
$saudiRial = Currency::factory()->saudiRial()->create();
|
||||
$usdDollar = Currency::factory()->usdDollar()->create();
|
||||
|
||||
User::factory()->create([
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
// cities
|
||||
$sanaa = City::factory()->sanaa()->create();
|
||||
$aden = City::factory()->aden()->create();
|
||||
|
||||
// rates
|
||||
Rate::factory()->recycle($usdDollar)->recycle($sanaa)->create();
|
||||
Rate::factory()->recycle($usdDollar)->recycle($aden)->create();
|
||||
|
||||
Rate::factory()->recycle($saudiRial)->recycle($sanaa)->create();
|
||||
Rate::factory()->recycle($saudiRial)->recycle($aden)->create();
|
||||
}
|
||||
}
|
||||
|
||||
18
database/seeders/RateSeeder.php
Normal file
18
database/seeders/RateSeeder.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Rate;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class RateSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Rate::factory()->today()->create();
|
||||
Rate::factory()->last20Days()->count(5)->create();
|
||||
}
|
||||
}
|
||||
المرجع في مشكلة جديدة
حظر مستخدم