Skip to content

Commit b4665b3

Browse files
authored
Merge pull request #54 from BinaryStudioAcademy/feature/comment-count
Feature/comment count
2 parents 59a0295 + 8d3f456 commit b4665b3

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

backend/app/Entity/Tweet.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1010
use Illuminate\Database\Eloquent\Relations\HasMany;
11+
use InvalidArgumentException;
1112

1213
/**
1314
* Class Tweet
@@ -18,13 +19,18 @@
1819
* @property int $author_id
1920
* @property Carbon $created_at
2021
* @property Carbon $updated_at
22+
* @property int $comments_count
23+
* @property User $author
2124
*/
2225
final class Tweet extends Model
2326
{
2427
protected $table = 'tweets';
25-
26-
// The relations to eager load on every query.
27-
protected $with = ['author', 'comments'];
28+
29+
// Relations to eager load on every query.
30+
protected $with = ['author'];
31+
32+
// Eager load related comments count each time.
33+
protected $withCount = ['comments'];
2834

2935
protected $fillable = [
3036
'text',
@@ -67,10 +73,21 @@ public function getAuthorId(): int
6773
return $this->author_id;
6874
}
6975

76+
public function getAuthor(): User
77+
{
78+
return $this->author;
79+
}
80+
81+
public function getCommentsCount(): int
82+
{
83+
// cast to int, because if tweet doesn't have comments null will be returned
84+
return (int)$this->comments_count;
85+
}
86+
7087
public function changeContent(string $text): void
7188
{
7289
if (empty($text)) {
73-
throw new \InvalidArgumentException('Tweet content cannot be empty.');
90+
throw new InvalidArgumentException('Tweet content cannot be empty.');
7491
}
7592

7693
$this->text = $text;
@@ -79,7 +96,7 @@ public function changeContent(string $text): void
7996
public function changePreviewImage(string $imageUrl): void
8097
{
8198
if (empty($imageUrl)) {
82-
throw new \InvalidArgumentException('Empty image url.');
99+
throw new InvalidArgumentException('Empty image url.');
83100
}
84101

85102
$this->image_url = $imageUrl;

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

backend/tests/Feature/Api/ApiTestCase.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
1010
use Illuminate\Foundation\Testing\RefreshDatabase;
1111
use Illuminate\Contracts\Auth\Authenticatable;
12+
use Illuminate\Foundation\Testing\TestResponse;
1213
use Illuminate\Support\Facades\Auth;
14+
use InvalidArgumentException;
1315
use Tests\CreatesApplication;
1416

1517
abstract class ApiTestCase extends BaseTestCase
@@ -43,7 +45,8 @@ abstract class ApiTestCase extends BaseTestCase
4345
'id',
4446
'text',
4547
'image_url',
46-
'author' => self::USER_RESOURCE_STRUCTURE
48+
'author' => self::USER_RESOURCE_STRUCTURE,
49+
'comments_count'
4750
];
4851

4952
/**
@@ -91,7 +94,7 @@ protected function seedFakeData(int $itemsAmount = 5): void
9194
* @param array $files
9295
* @param array $server
9396
* @param null $content
94-
* @return \Illuminate\Foundation\Testing\TestResponse
97+
* @return TestResponse
9598
*/
9699
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
97100
{
@@ -121,7 +124,7 @@ protected function actingWithToken(Authenticatable $user = null): self
121124
private function assertUriIsValid(string $uri): void
122125
{
123126
if (empty($uri)) {
124-
throw new \InvalidArgumentException('Request URI cannot be empty.');
127+
throw new InvalidArgumentException('Request URI cannot be empty.');
125128
}
126129
}
127130

@@ -240,7 +243,7 @@ protected function createResourceItemUri(string $uri, int $id): string
240243
private function assertAttributesIsValid(array $attributes): void
241244
{
242245
if (empty($attributes)) {
243-
throw new \InvalidArgumentException('Request attributes are empty.');
246+
throw new InvalidArgumentException('Request attributes are empty.');
244247
}
245248
}
246249
}

0 commit comments

Comments
 (0)