Skip to content

Commit df24db6

Browse files
committed
Edit like/tweet entity definition
Include likes into tweets response
1 parent 7fe44d2 commit df24db6

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

backend/app/Entity/Like.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ final class Like extends Model
3737
'likeable_type'
3838
];
3939

40-
protected $with = ['user'];
41-
4240
public function likeable(): MorphTo
4341
{
4442
return $this->morphTo();
@@ -56,7 +54,7 @@ public function getId(): int
5654

5755
public function getCreatedAt(): Carbon
5856
{
59-
return $this->created_at;
57+
return Carbon::createFromTimeString($this->created_at);
6058
}
6159

6260
public function getUserId(): int

backend/app/Entity/Tweet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ final class Tweet extends Model
2929
protected $table = 'tweets';
3030

3131
// Relations to eager load on every query.
32-
protected $with = ['author'];
32+
protected $with = ['author', 'likes'];
3333

34-
// Eager load related comments count each time.
34+
// Eager load related entities count each time.
3535
protected $withCount = ['comments', 'likes'];
3636

3737
protected $fillable = [
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Presenter;
6+
7+
use App\Entity\Like;
8+
use Illuminate\Support\Collection;
9+
10+
final class LikeArrayPresenter implements CollectionAsArrayPresenter
11+
{
12+
public function present(Like $like): array
13+
{
14+
return [
15+
'user_id' => $like->getUserId(),
16+
'created_at' => $like->getCreatedAt()->toDateTimeString()
17+
];
18+
}
19+
20+
public function presentCollection(Collection $collection): array
21+
{
22+
return $collection
23+
->map(
24+
function (Like $like) {
25+
return $this->present($like);
26+
}
27+
)
28+
->all();
29+
}
30+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66

77
use App\Entity\Tweet;
88
use App\Http\Presenter\CollectionAsArrayPresenter;
9+
use App\Http\Presenter\LikeArrayPresenter;
910
use Illuminate\Support\Collection;
1011
use App\Http\Presenter\User\UserArrayPresenter;
1112

1213
final class TweetArrayPresenter implements CollectionAsArrayPresenter
1314
{
1415
private $userPresenter;
16+
private $likeArrayPresenter;
1517

16-
public function __construct(UserArrayPresenter $userPresenter)
18+
public function __construct(UserArrayPresenter $userPresenter, LikeArrayPresenter $likeArrayPresenter)
1719
{
1820
$this->userPresenter = $userPresenter;
21+
$this->likeArrayPresenter = $likeArrayPresenter;
1922
}
2023

2124
public function present(Tweet $tweet): array
@@ -27,7 +30,8 @@ public function present(Tweet $tweet): array
2730
'created_at' => $tweet->getCreatedAt()->toDateTimeString(),
2831
'author' => $this->userPresenter->present($tweet->getAuthor()),
2932
'comments_count' => $tweet->getCommentsCount(),
30-
'likes_count' => $tweet->getLikesCount()
33+
'likes_count' => $tweet->getLikesCount(),
34+
'likes' => $this->likeArrayPresenter->presentCollection($tweet->likes)
3135
];
3236
}
3337

0 commit comments

Comments
 (0)