|
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\Comment\ListCommentResourceCollection; |
|
8
|
|
|
use CSlant\Blog\Api\Http\Resources\Tag\TagResource; |
|
9
|
|
|
use CSlant\Blog\Core\Facades\Base\Media\RvMedia; |
|
10
|
|
|
use CSlant\Blog\Core\Models\Post; |
|
11
|
|
|
use CSlant\Blog\Core\Models\Slug; |
|
12
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
|
13
|
|
|
use Illuminate\Support\Facades\Auth; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @mixin Post |
|
17
|
|
|
*/ |
|
18
|
|
|
class PostResource extends JsonResource |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param $request |
|
22
|
|
|
* |
|
23
|
|
|
* @return array<string, mixed> |
|
24
|
|
|
*/ |
|
25
|
|
|
public function toArray($request): array |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var Post $this */ |
|
28
|
|
|
$comments = $this->comments() |
|
29
|
|
|
->orderBy((string) $request->get('order_by', 'created_at'), (string) $request->get('order', 'DESC')) |
|
30
|
|
|
->paginate($request->integer('per_page', 10)); |
|
31
|
|
|
|
|
32
|
|
|
$userId = Auth::user() ? Auth::user()->id : 0; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
return [ |
|
35
|
|
|
'id' => $this->id, |
|
36
|
|
|
'name' => $this->name, |
|
37
|
|
|
'slug' => $this->slug instanceof Slug ? $this->slug->key : $this->slug, |
|
|
|
|
|
|
38
|
|
|
'description' => $this->description, |
|
39
|
|
|
'content' => $this->content, |
|
40
|
|
|
'image' => $this->image ? RvMedia::url($this->image) : null, |
|
41
|
|
|
'categories' => CategoryResource::collection($this->categories), |
|
42
|
|
|
'tags' => TagResource::collection($this->tags), |
|
43
|
|
|
'author' => AuthorResource::make($this->author), |
|
44
|
|
|
'comments' => ListCommentResourceCollection::make($comments), |
|
45
|
|
|
'likes_count' => $this->likesCountDigital(), |
|
46
|
|
|
'is_liked' => $this->isLikedBy($userId), |
|
47
|
|
|
'comments_count' => $this->comments()->count(), |
|
48
|
|
|
'is_commented' => $this->isCommentBy($userId), |
|
49
|
|
|
'created_at' => $this->created_at, |
|
50
|
|
|
'updated_at' => $this->updated_at, |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|