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
53
backend/database/seeders/CommentSeeder.php
Normal file
53
backend/database/seeders/CommentSeeder.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Comment\CommentRepository;
|
||||
use App\Comment\CreateCommentDto;
|
||||
use App\Post\PostRepository;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\UserRepository;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class CommentSeeder extends Seeder
|
||||
{
|
||||
public function __construct(
|
||||
private CommentRepository $commentRepo,
|
||||
private PostRepository $postRepo,
|
||||
private UserRepository $userRepo,
|
||||
) {}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
|
||||
$bob = $this->userRepo->findByEmail(
|
||||
new EmailAddress('bob@example.com'),
|
||||
);
|
||||
$carol = $this->userRepo->findByEmail(
|
||||
new EmailAddress('carol@example.com'),
|
||||
);
|
||||
if ($bob === null || $carol === null) {
|
||||
return;
|
||||
}
|
||||
$posts = $this->postRepo->findRecent(10);
|
||||
if (count($posts) === 0) {
|
||||
return;
|
||||
}
|
||||
$firstPost = $posts[0];
|
||||
|
||||
$this->commentRepo->create(new CreateCommentDto(
|
||||
postId: $firstPost->getId(),
|
||||
userId: $bob->getId(),
|
||||
body: 'Welcome aboard!',
|
||||
createdAt: $now,
|
||||
));
|
||||
$this->commentRepo->create(new CreateCommentDto(
|
||||
postId: $firstPost->getId(),
|
||||
userId: $carol->getId(),
|
||||
body: 'Looking forward to more.',
|
||||
createdAt: $now->modify('+1 minute'),
|
||||
));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue