seed available sets
This commit is contained in:
parent
4dfea4ebb3
commit
41c0e7bebe
2 changed files with 46 additions and 0 deletions
|
|
@ -12,5 +12,6 @@ class DatabaseSeeder extends Seeder
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
$this->call(UserSeeder::class);
|
$this->call(UserSeeder::class);
|
||||||
|
$this->call(SetSeeder::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
45
backend/database/seeders/SetSeeder.php
Normal file
45
backend/database/seeders/SetSeeder.php
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Set\CreateSetDto;
|
||||||
|
use App\Set\SetRepository;
|
||||||
|
use App\Shared\ValueObject\EmailAddress;
|
||||||
|
use App\User\UserRepository;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class SetSeeder extends Seeder
|
||||||
|
{
|
||||||
|
private const array NAMES = [
|
||||||
|
'Bible',
|
||||||
|
'Course',
|
||||||
|
'Fitness Program',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$user = app(UserRepository::class)->findByEmail(
|
||||||
|
new EmailAddress(UserSeeder::EMAIL),
|
||||||
|
);
|
||||||
|
if ($user === null) {
|
||||||
|
throw new RuntimeException('seeded user not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$repository = app(SetRepository::class);
|
||||||
|
$existingNames = array_map(function ($set): string {
|
||||||
|
return $set->getName();
|
||||||
|
}, $repository->all());
|
||||||
|
|
||||||
|
foreach (self::NAMES as $name) {
|
||||||
|
if (in_array($name, $existingNames, true)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$repository->create(new CreateSetDto(
|
||||||
|
name: $name,
|
||||||
|
creator: $user,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue