implement ListRecentPosts use case
validates limit > 0 (zero or negative -> BadRequest), then delegates to PostRepository->findRecent. 49 tests pass.
This commit is contained in:
parent
e9779d8459
commit
7ec46aa8f9
2 changed files with 38 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace App\Post\UseCases\ListRecentPosts;
|
||||
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Post\Post;
|
||||
use App\Post\PostRepository;
|
||||
|
||||
class ListRecentPosts
|
||||
{
|
||||
public function __construct(
|
||||
private PostRepository $postRepo,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return Post[]
|
||||
*
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
public function execute(ListRecentPostsRequest $request): array
|
||||
{
|
||||
if ($request->limit <= 0) {
|
||||
throw new BadRequestException('limit must be positive');
|
||||
}
|
||||
|
||||
return $this->postRepo->findRecent($request->limit);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Post\UseCases\ListRecentPosts;
|
||||
|
||||
class ListRecentPostsRequest
|
||||
{
|
||||
public function __construct(
|
||||
public int $limit,
|
||||
) {}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue