split seeders by domain entity
Replaces the laravel-default scaffold seeder with one file per domain (AdminUser, User, Post, Comment) plus a config/seed.php to source admin credentials from env. DatabaseSeeder is now a pure orchestrator that calls the sub-seeders in dependency order.
This commit is contained in:
parent
c55852ec12
commit
beb4ddd1e9
6 changed files with 213 additions and 9 deletions
55
backend/database/seeders/PostSeeder.php
Normal file
55
backend/database/seeders/PostSeeder.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Post\CreatePostDto;
|
||||
use App\Post\PostRepository;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\UserRepository;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class PostSeeder extends Seeder
|
||||
{
|
||||
public function __construct(
|
||||
private PostRepository $postRepo,
|
||||
private UserRepository $userRepo,
|
||||
) {}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
|
||||
$samples = [
|
||||
[
|
||||
'email' => 'alice@example.com',
|
||||
'title' => 'Hello World',
|
||||
'body' => 'My first post on TIDE.',
|
||||
],
|
||||
[
|
||||
'email' => 'bob@example.com',
|
||||
'title' => 'Riding the wave',
|
||||
'body' => 'Bob shares his thoughts.',
|
||||
],
|
||||
[
|
||||
'email' => 'carol@example.com',
|
||||
'title' => 'Notes from week one',
|
||||
'body' => 'Carol lays out her plan.',
|
||||
],
|
||||
];
|
||||
foreach ($samples as $index => $sample) {
|
||||
$author = $this->userRepo->findByEmail(
|
||||
new EmailAddress($sample['email']),
|
||||
);
|
||||
if ($author === null) {
|
||||
continue;
|
||||
}
|
||||
$this->postRepo->create(new CreatePostDto(
|
||||
userId: $author->getId(),
|
||||
title: $sample['title'],
|
||||
body: $sample['body'],
|
||||
createdAt: $now->modify("-{$index} hour"),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue