Skip to content

Commit 360c329

Browse files
authored
Merge pull request #81 from cslant/feature/show-like-count
feat: add total like
2 parents 2d3b9a4 + f856909 commit 360c329

File tree

7 files changed

+10
-3
lines changed

7 files changed

+10
-3
lines changed

phpstan-baseline.neon

+3
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ parameters:
2525

2626
-
2727
identifier: method.notFound
28+
29+
- identifier: function.alreadyNarrowedType
30+
path: src/Http/Resources/Post/ListPostResource.php

src/Http/Controllers/PostController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function index(Request $request): BaseHttpResponse|JsonResponse|JsonResou
121121
$data = $this
122122
->postRepository
123123
->advancedGet([
124-
'with' => ['tags', 'categories', 'author', 'slugable'],
124+
'with' => ['tags', 'categories', 'author', 'likes', 'slugable'],
125125
'condition' => ['status' => StatusEnum::PUBLISHED->value],
126126
'paginate' => [
127127
'per_page' => $request->integer('per_page', 10),
@@ -211,7 +211,7 @@ public function findBySlug(string $slug): BaseHttpResponse|JsonResponse|JsonReso
211211
}
212212

213213
$post = Post::query()
214-
->with(['tags', 'categories', 'author'])
214+
->with(['tags', 'categories', 'author', 'likes'])
215215
->where([
216216
'id' => $slug->reference_id,
217217
'status' => StatusEnum::PUBLISHED,

src/Http/Resources/Post/ListPostResource.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function toArray($request): array
3333
'categories' => CategoryResource::collection($this->categories),
3434
'tags' => TagResource::collection($this->tags),
3535
'author' => AuthorResource::make($this->author),
36+
'likes_count' => method_exists($this, 'likesCountDigital') ? $this->likesCountDigital() : 0,
3637
'created_at' => $this->created_at,
3738
'updated_at' => $this->updated_at,
3839
];

src/Http/Resources/Post/PostResource.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function toArray($request): array
3333
'categories' => CategoryResource::collection($this->categories),
3434
'tags' => TagResource::collection($this->tags),
3535
'author' => AuthorResource::make($this->author),
36+
'likes_count' => $this->likesCountDigital(),
3637
'created_at' => $this->created_at,
3738
'updated_at' => $this->updated_at,
3839
];

src/OpenApi/Schemas/Resources/Post/PostListResourceSchema.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
ref: AuthorModelResourceSchema::class,
3434
type: "object",
3535
),
36+
new Property(property: "likes_count", description: "Likes count", type: "string", nullable: true),
3637
new Property(property: "created_at", description: "Post created at", type: "datetime", nullable: true),
3738
new Property(property: "updated_at", description: "Post updated at", type: "datetime", nullable: true),
3839
],

src/OpenApi/Schemas/Resources/Post/PostModelResourceSchema.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
ref: AuthorModelResourceSchema::class,
3535
type: "object",
3636
),
37+
new Property(property: "likes_count", description: "Likes count", type: "string", nullable: true),
3738
new Property(property: "created_at", description: "Post created at", type: "datetime", nullable: true),
3839
new Property(property: "updated_at", description: "Post updated at", type: "datetime", nullable: true),
3940
],

src/Services/PostService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class PostService
2727
*/
2828
public function getCustomFilters(array $filters): LengthAwarePaginator
2929
{
30-
$data = Post::query();
30+
$data = Post::query()->with(['likes']);
3131

3232
if ($filters['tags'] !== null) {
3333
$tags = array_filter((array) $filters['tags']);

0 commit comments

Comments
 (0)