feat(models): add Country and NonWorkingDays models

This commit is contained in:
Ondrej Vlach 2024-08-07 12:45:56 +02:00
parent 5633ff78c6
commit 82f3ef39c9
No known key found for this signature in database
GPG Key ID: 7F141CDACEDEE2DE
5 changed files with 113 additions and 0 deletions

12
app/Models/Country.php Normal file
View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
protected $fillable = ['country_code', 'name'];
}

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class NonWorkingDays extends Model
{
protected $fillable = ['country_id', 'non_working_date'];
}

View File

@ -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('countries', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('country_code', 2)->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('countries');
}
};

View File

@ -0,0 +1,31 @@
<?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('non_working_days', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('country_id');
$table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
$table->date('non_working_date');
$table->timestamps();
$table->unique(['country_id', 'non_working_date']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('non_working_days');
}
};

View File

@ -0,0 +1,29 @@
<?php
namespace Database\Seeders;
use App\Models\Country;
use Illuminate\Database\Seeder;
use Illuminate\Database\UniqueConstraintViolationException;
class CountrySeeder extends Seeder
{
private const array COUNTRIES = [
'CZ' => 'Czech Republic',
];
/**
* Run the database seeds.
*/
public function run(): void
{
foreach (static::COUNTRIES as $countryCode => $countryName) {
try {
Country::create(['country_code' => $countryCode, 'name' => $countryName]);
} catch (UniqueConstraintViolationException $e) {
// safe to ignore duplicate entries
}
}
//
}
}