implement CreatePost use case
trims title and body, rejects empty (post-trim) values with BadRequest. supplies createdAt from injected Clock. persists through PostRepository->create and returns the resulting Post. 44 tests pass.
This commit is contained in:
parent
504554bf7f
commit
4a4e046de4
2 changed files with 52 additions and 0 deletions
40
backend/app/Post/UseCases/CreatePost/CreatePost.php
Normal file
40
backend/app/Post/UseCases/CreatePost/CreatePost.php
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Post\UseCases\CreatePost;
|
||||||
|
|
||||||
|
use App\Auth\Clock;
|
||||||
|
use App\Exceptions\BadRequestException;
|
||||||
|
use App\Post\CreatePostDto;
|
||||||
|
use App\Post\Post;
|
||||||
|
use App\Post\PostRepository;
|
||||||
|
|
||||||
|
class CreatePost
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private PostRepository $postRepo,
|
||||||
|
private Clock $clock,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws BadRequestException
|
||||||
|
*/
|
||||||
|
public function execute(CreatePostRequest $request): Post
|
||||||
|
{
|
||||||
|
$title = $request->title === null ? '' : trim($request->title);
|
||||||
|
$body = $request->body === null ? '' : trim($request->body);
|
||||||
|
|
||||||
|
if ($title === '') {
|
||||||
|
throw new BadRequestException('title is required');
|
||||||
|
}
|
||||||
|
if ($body === '') {
|
||||||
|
throw new BadRequestException('body is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->postRepo->create(new CreatePostDto(
|
||||||
|
userId: $request->userId,
|
||||||
|
title: $title,
|
||||||
|
body: $body,
|
||||||
|
createdAt: $this->clock->now(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
12
backend/app/Post/UseCases/CreatePost/CreatePostRequest.php
Normal file
12
backend/app/Post/UseCases/CreatePost/CreatePostRequest.php
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Post\UseCases\CreatePost;
|
||||||
|
|
||||||
|
class CreatePostRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $userId,
|
||||||
|
public ?string $title,
|
||||||
|
public ?string $body,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue