|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Modules\Comments\Domain\Logic; |
|
4
|
|
|
|
|
5
|
|
|
use App\Infrastructure\Pagination\Page; |
|
6
|
|
|
use App\Modules\Comments\Api\Query\FindCommentsByPostIdQuery; |
|
7
|
|
|
use App\Modules\Comments\Api\Query\FindLatestCommentsQuery; |
|
8
|
|
|
use App\Modules\Comments\Api\Query\Response\FindCommentsByPostIdQueryResponse; |
|
9
|
|
|
use App\Modules\Comments\Api\Query\Response\FindLatestCommentsQueryResponse; |
|
10
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsFindingRepositoryInterface; |
|
11
|
|
|
use DusanKasan\Knapsack\Collection; |
|
12
|
|
|
|
|
13
|
|
|
trait CommentsFinder |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param CommentsFindingRepositoryInterface $commentsFindingRepository |
|
17
|
|
|
*/ |
|
18
|
12 |
|
public function __construct( |
|
19
|
|
|
private CommentsFindingRepositoryInterface $commentsFindingRepository |
|
20
|
|
|
) |
|
21
|
|
|
{ |
|
22
|
12 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param FindCommentsByPostIdQuery $query |
|
27
|
|
|
* @return array<FindCommentsByPostIdQueryResponse> |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function findCommentsForPost(FindCommentsByPostIdQuery $query): array |
|
30
|
|
|
{ |
|
31
|
4 |
|
return Collection::from( |
|
32
|
4 |
|
$this->commentsFindingRepository->findCommentsByPostId($query->getPostId()) |
|
33
|
4 |
|
)->map(function ($comment) { |
|
34
|
4 |
|
return new FindCommentsByPostIdQueryResponse( |
|
35
|
4 |
|
$comment->getId(), |
|
36
|
4 |
|
$comment->getAuthor(), |
|
37
|
4 |
|
$comment->getBody(), |
|
38
|
4 |
|
$comment->getParentId(), |
|
39
|
4 |
|
$comment->getCreatedAt(), |
|
40
|
|
|
); |
|
41
|
4 |
|
}) |
|
42
|
4 |
|
->toArray(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param FindLatestCommentsQuery $query |
|
47
|
|
|
* @return Page<FindLatestCommentsQueryResponse> |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function findLatestComments(FindLatestCommentsQuery $query): Page |
|
50
|
|
|
{ |
|
51
|
2 |
|
$result = $this->commentsFindingRepository->findLatestComments($query->getPageNo()); |
|
52
|
2 |
|
$data = Collection::from( |
|
53
|
2 |
|
$result->getData() |
|
54
|
2 |
|
)->map(function ($comment) { |
|
55
|
2 |
|
return new FindLatestCommentsQueryResponse( |
|
56
|
2 |
|
$comment->getId(), |
|
57
|
2 |
|
$comment->getAuthor(), |
|
58
|
2 |
|
$comment->getBody(), |
|
59
|
2 |
|
$comment->getParentId(), |
|
60
|
2 |
|
$comment->getCreatedAt(), |
|
61
|
2 |
|
$comment->getPostId(), |
|
62
|
2 |
|
$comment->getPostTitle(), |
|
63
|
2 |
|
$comment->getPostTags() |
|
64
|
2 |
|
); |
|
65
|
2 |
|
}) |
|
66
|
|
|
->toArray(); |
|
67
|
2 |
|
|
|
68
|
2 |
|
return new Page($data, $result->getCount(), $result->getPageNo(), $result->getPageSize()); |
|
69
|
|
|
} |
|
70
|
|
|
} |