Skip to content

Commit 7f4511f

Browse files
committed
Fix tweet array presenter, add comments count
1 parent 76a0996 commit 7f4511f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

backend/app/Entity/Tweet.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @property int $author_id
2020
* @property Carbon $created_at
2121
* @property Carbon $updated_at
22+
* @property int $comments_count
23+
* @property User $author
2224
*/
2325
final class Tweet extends Model
2426
{
@@ -71,6 +73,16 @@ public function getAuthorId(): int
7173
return $this->author_id;
7274
}
7375

76+
public function getAuthor(): User
77+
{
78+
return $this->author;
79+
}
80+
81+
public function getCommentsCount(): int
82+
{
83+
return $this->comments_count;
84+
}
85+
7486
public function changeContent(string $text): void
7587
{
7688
if (empty($text)) {

backend/app/Http/Presenter/Tweet/TweetArrayPresenter.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@
88
use App\Http\Presenter\CollectionAsArrayPresenter;
99
use Illuminate\Support\Collection;
1010
use App\Http\Presenter\User\UserArrayPresenter;
11-
use App\Http\Presenter\Comment\CommentAsArrayPresenter;
1211

1312
final class TweetArrayPresenter implements CollectionAsArrayPresenter
1413
{
1514
private $userPresenter;
16-
private $commentPresenter;
1715

18-
public function __construct(
19-
UserArrayPresenter $userPresenter,
20-
CommentAsArrayPresenter $commentPresenter
21-
) {
16+
public function __construct(UserArrayPresenter $userPresenter)
17+
{
2218
$this->userPresenter = $userPresenter;
23-
$this->commentPresenter = $commentPresenter;
2419
}
2520

2621
public function present(Tweet $tweet): array
@@ -30,8 +25,8 @@ public function present(Tweet $tweet): array
3025
'text' => $tweet->getText(),
3126
'image_url' => $tweet->getImageUrl(),
3227
'created_at' => $tweet->getCreatedAt()->toDateTimeString(),
33-
'author' => $this->userPresenter->present($tweet->author),
34-
'comments' => $this->commentPresenter->presentCollection($tweet->comments)
28+
'author' => $this->userPresenter->present($tweet->getAuthor()),
29+
'comments_count' => $tweet->getCommentsCount()
3530
];
3631
}
3732

0 commit comments

Comments
 (0)