|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Api\Http\Resources\Post; |
|
4
|
|
|
|
|
5
|
|
|
use CSlant\Blog\Api\Http\Resources\Author\AuthorResource; |
|
6
|
|
|
use CSlant\Blog\Api\Http\Resources\Category\CategoryResource; |
|
7
|
|
|
use CSlant\Blog\Api\Http\Resources\Tag\TagResource; |
|
8
|
|
|
use CSlant\Blog\Core\Facades\Base\Media\RvMedia; |
|
9
|
|
|
use CSlant\Blog\Core\Models\Post; |
|
10
|
|
|
use CSlant\Blog\Core\Models\Slug; |
|
11
|
|
|
use Illuminate\Http\Request; |
|
12
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
|
13
|
|
|
use Illuminate\Support\Facades\Auth; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @mixin Post |
|
17
|
|
|
*/ |
|
18
|
|
|
class ListPostResource extends JsonResource |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param Request $request |
|
22
|
|
|
* |
|
23
|
|
|
* @return array<string, mixed> |
|
24
|
|
|
*/ |
|
25
|
|
|
public function toArray($request): array |
|
26
|
|
|
{ |
|
27
|
|
|
$userId = 0; |
|
28
|
|
|
if (Auth::user()) { |
|
29
|
|
|
$userId = Auth::user()->id; |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** @var Post $this */ |
|
33
|
|
|
return [ |
|
34
|
|
|
'id' => $this->id, |
|
35
|
|
|
'name' => $this->name, |
|
36
|
|
|
'slug' => $this->slug instanceof Slug ? $this->slug->key : $this->slug, |
|
|
|
|
|
|
37
|
|
|
'description' => $this->description, |
|
38
|
|
|
'image' => $this->image ? RvMedia::url($this->image) : null, |
|
39
|
|
|
'categories' => CategoryResource::collection($this->categories), |
|
40
|
|
|
'tags' => TagResource::collection($this->tags), |
|
41
|
|
|
'author' => AuthorResource::make($this->author), |
|
42
|
|
|
'likes_count' => $this->relationLoaded('likes') ? $this->likesCountDigital() : 0, |
|
43
|
|
|
'is_liked' => $this->relationLoaded('likes') && $this->isLikedBy($userId), |
|
44
|
|
|
'is_commented' => $this->relationLoaded('comments') && $this->isCommentBy($userId), |
|
45
|
|
|
'created_at' => $this->created_at, |
|
46
|
|
|
'updated_at' => $this->updated_at, |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|