add Post entity, dto, repository interface
Post: id, userId (fk -> User), title, body, createdAt as DateTimeImmutable. CreatePostDto readonly with explicit createdAt (use case supplies it via Clock; entity remains pure). PostRepository: create, find, findByUserId, findRecent (limit), delete.
This commit is contained in:
parent
9ca58f3a9d
commit
73a3acd39f
3 changed files with 78 additions and 0 deletions
15
backend/app/Post/CreatePostDto.php
Normal file
15
backend/app/Post/CreatePostDto.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Post;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
readonly class CreatePostDto
|
||||
{
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public string $title,
|
||||
public string $body,
|
||||
public DateTimeImmutable $createdAt,
|
||||
) {}
|
||||
}
|
||||
41
backend/app/Post/Post.php
Normal file
41
backend/app/Post/Post.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace App\Post;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class Post
|
||||
{
|
||||
public function __construct(
|
||||
private int $id,
|
||||
private int $userId,
|
||||
private string $title,
|
||||
private string $body,
|
||||
private DateTimeImmutable $createdAt,
|
||||
) {}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUserId(): int
|
||||
{
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
}
|
||||
22
backend/app/Post/PostRepository.php
Normal file
22
backend/app/Post/PostRepository.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Post;
|
||||
|
||||
interface PostRepository
|
||||
{
|
||||
public function create(CreatePostDto $dto): Post;
|
||||
|
||||
public function find(int $id): ?Post;
|
||||
|
||||
/**
|
||||
* @return Post[]
|
||||
*/
|
||||
public function findByUserId(int $userId): array;
|
||||
|
||||
/**
|
||||
* @return Post[]
|
||||
*/
|
||||
public function findRecent(int $limit): array;
|
||||
|
||||
public function delete(int $id): void;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue