|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Api\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Botble\Base\Http\Responses\BaseHttpResponse; |
|
|
|
|
|
|
6
|
|
|
use CSlant\Blog\Api\Enums\StatusEnum; |
|
7
|
|
|
use CSlant\Blog\Api\Http\Resources\Post\ListPostResource; |
|
8
|
|
|
use CSlant\Blog\Api\Http\Resources\Post\PostResource; |
|
9
|
|
|
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Post\PostListResourceSchema; |
|
10
|
|
|
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Post\PostModelResourceSchema; |
|
11
|
|
|
use CSlant\Blog\Core\Facades\Base\SlugHelper; |
|
12
|
|
|
use CSlant\Blog\Core\Http\Controllers\Base\BasePostController; |
|
13
|
|
|
use CSlant\Blog\Core\Models\Post; |
|
14
|
|
|
use CSlant\Blog\Core\Models\Slug; |
|
15
|
|
|
use CSlant\Blog\Core\Supports\Base\FilterPost; |
|
16
|
|
|
use Illuminate\Http\JsonResponse; |
|
17
|
|
|
use Illuminate\Http\RedirectResponse; |
|
18
|
|
|
use Illuminate\Http\Request; |
|
19
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
|
20
|
|
|
use OpenApi\Attributes\Get; |
|
21
|
|
|
use OpenApi\Attributes\Items; |
|
22
|
|
|
use OpenApi\Attributes\JsonContent; |
|
23
|
|
|
use OpenApi\Attributes\Parameter; |
|
24
|
|
|
use OpenApi\Attributes\Property; |
|
25
|
|
|
use OpenApi\Attributes\Response; |
|
26
|
|
|
use OpenApi\Attributes\Schema; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class PostController |
|
30
|
|
|
* |
|
31
|
|
|
* @package CSlant\Blog\Api\Http\Controllers |
|
32
|
|
|
* |
|
33
|
|
|
* @group Blog API |
|
34
|
|
|
* |
|
35
|
|
|
* @authenticated |
|
36
|
|
|
* |
|
37
|
|
|
* @method BaseHttpResponse httpResponse() |
|
38
|
|
|
* @method BaseHttpResponse setData(mixed $data) |
|
39
|
|
|
* @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse() |
|
40
|
|
|
*/ |
|
41
|
|
|
class PostController extends BasePostController |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* @group Blog API |
|
45
|
|
|
* |
|
46
|
|
|
* @param Request $request |
|
47
|
|
|
* |
|
48
|
|
|
* @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse |
|
49
|
|
|
*/ |
|
50
|
|
|
#[ |
|
51
|
|
|
Get( |
|
52
|
|
|
path: "/posts", |
|
53
|
|
|
operationId: "postGetAllWithFilter", |
|
54
|
|
|
description: "Get all posts with pagination (10 items per page by default, page 1 by default) |
|
55
|
|
|
|
|
56
|
|
|
This API will get records from the database and return them as a paginated list. |
|
57
|
|
|
The default number of items per page is 10 and the default page number is 1. You can change these values by passing the `per_page` and `page` query parameters. |
|
58
|
|
|
", |
|
59
|
|
|
summary: "Get all posts with pagination", |
|
60
|
|
|
tags: ["Post"], |
|
61
|
|
|
parameters: [ |
|
62
|
|
|
new Parameter( |
|
63
|
|
|
name: 'per_page', |
|
64
|
|
|
description: 'Number of items per page', |
|
65
|
|
|
in: 'query', |
|
66
|
|
|
required: false, |
|
67
|
|
|
schema: new Schema(type: 'integer', default: 10) |
|
68
|
|
|
), |
|
69
|
|
|
new Parameter( |
|
70
|
|
|
name: 'page', |
|
71
|
|
|
description: 'Page number', |
|
72
|
|
|
in: 'query', |
|
73
|
|
|
required: false, |
|
74
|
|
|
schema: new Schema(type: 'integer', default: 1) |
|
75
|
|
|
), |
|
76
|
|
|
], |
|
77
|
|
|
responses: [ |
|
78
|
|
|
new Response( |
|
79
|
|
|
response: 200, |
|
80
|
|
|
description: "Get posts successfully", |
|
81
|
|
|
content: new JsonContent( |
|
82
|
|
|
properties: [ |
|
83
|
|
|
new Property( |
|
84
|
|
|
property: 'error', |
|
85
|
|
|
description: 'Error status', |
|
86
|
|
|
type: 'boolean', |
|
87
|
|
|
default: false |
|
88
|
|
|
), |
|
89
|
|
|
new Property( |
|
90
|
|
|
property: "data", |
|
91
|
|
|
description: "Data of model", |
|
92
|
|
|
type: "array", |
|
93
|
|
|
items: new Items(ref: PostListResourceSchema::class) |
|
94
|
|
|
), |
|
95
|
|
|
] |
|
96
|
|
|
) |
|
97
|
|
|
), |
|
98
|
|
|
new Response( |
|
99
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class, |
|
100
|
|
|
response: 400, |
|
101
|
|
|
), |
|
102
|
|
|
new Response( |
|
103
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class, |
|
104
|
|
|
response: 404, |
|
105
|
|
|
), |
|
106
|
|
|
new Response( |
|
107
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class, |
|
108
|
|
|
response: 500, |
|
109
|
|
|
), |
|
110
|
|
|
] |
|
111
|
|
|
) |
|
112
|
|
|
] |
|
113
|
|
|
public function index(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse |
|
114
|
|
|
{ |
|
115
|
|
|
$data = $this |
|
116
|
|
|
->postRepository |
|
117
|
|
|
->advancedGet([ |
|
118
|
|
|
'with' => ['tags', 'categories', 'author', 'slugable'], |
|
119
|
|
|
'condition' => ['status' => StatusEnum::PUBLISHED->value], |
|
120
|
|
|
'paginate' => [ |
|
121
|
|
|
'per_page' => $request->integer('per_page', 10), |
|
122
|
|
|
'current_paged' => $request->integer('page', 1), |
|
123
|
|
|
], |
|
124
|
|
|
]); |
|
125
|
|
|
|
|
126
|
|
|
return $this |
|
127
|
|
|
->httpResponse() |
|
128
|
|
|
->setData(ListPostResource::collection($data)) |
|
129
|
|
|
->toApiResponse(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $slug |
|
134
|
|
|
* |
|
135
|
|
|
* @group Blog |
|
136
|
|
|
* @queryParam slug Find by slug of post. |
|
137
|
|
|
* @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse |
|
138
|
|
|
*/ |
|
139
|
|
|
#[ |
|
140
|
|
|
Get( |
|
141
|
|
|
path: "/posts/{slug}", |
|
142
|
|
|
operationId: "postFilterBySlug", |
|
143
|
|
|
description: "Get the post by slug |
|
144
|
|
|
|
|
145
|
|
|
This API will get records from the database and return the post by slug. |
|
146
|
|
|
", |
|
147
|
|
|
summary: "Get post by slug", |
|
148
|
|
|
tags: ["Post"], |
|
149
|
|
|
parameters: [ |
|
150
|
|
|
new Parameter( |
|
151
|
|
|
name: 'slug', |
|
152
|
|
|
description: 'Post slug', |
|
153
|
|
|
in: 'path', |
|
154
|
|
|
required: true, |
|
155
|
|
|
schema: new Schema(type: 'string', example: 'php') |
|
156
|
|
|
), |
|
157
|
|
|
], |
|
158
|
|
|
responses: [ |
|
159
|
|
|
new Response( |
|
160
|
|
|
response: 200, |
|
161
|
|
|
description: "Get post successfully", |
|
162
|
|
|
content: new JsonContent( |
|
163
|
|
|
properties: [ |
|
164
|
|
|
new Property( |
|
165
|
|
|
property: 'error', |
|
166
|
|
|
description: 'Error status', |
|
167
|
|
|
type: 'boolean', |
|
168
|
|
|
default: false |
|
169
|
|
|
), |
|
170
|
|
|
new Property( |
|
171
|
|
|
property: "data", |
|
172
|
|
|
ref: PostModelResourceSchema::class, |
|
173
|
|
|
description: "Data of model", |
|
174
|
|
|
type: "object", |
|
175
|
|
|
), |
|
176
|
|
|
] |
|
177
|
|
|
) |
|
178
|
|
|
), |
|
179
|
|
|
new Response( |
|
180
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class, |
|
181
|
|
|
response: 400, |
|
182
|
|
|
), |
|
183
|
|
|
new Response( |
|
184
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class, |
|
185
|
|
|
response: 404, |
|
186
|
|
|
), |
|
187
|
|
|
new Response( |
|
188
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class, |
|
189
|
|
|
response: 500, |
|
190
|
|
|
), |
|
191
|
|
|
] |
|
192
|
|
|
) |
|
193
|
|
|
] |
|
194
|
|
|
public function findBySlug(string $slug): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse |
|
195
|
|
|
{ |
|
196
|
|
|
/** @var Slug $slug */ |
|
197
|
|
|
$slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Post::getBaseModel())); |
|
198
|
|
|
|
|
199
|
|
|
if (!$slug) { |
|
|
|
|
|
|
200
|
|
|
return $this |
|
201
|
|
|
->httpResponse() |
|
202
|
|
|
->setError() |
|
203
|
|
|
->setCode(404) |
|
204
|
|
|
->setMessage('Not found'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$post = Post::query() |
|
208
|
|
|
->with(['tags', 'categories', 'author', 'comments', 'likes']) |
|
209
|
|
|
->where([ |
|
210
|
|
|
'id' => $slug->reference_id, |
|
211
|
|
|
'status' => StatusEnum::PUBLISHED, |
|
212
|
|
|
]) |
|
213
|
|
|
->first(); |
|
214
|
|
|
|
|
215
|
|
|
if (!$post) { |
|
216
|
|
|
return $this |
|
217
|
|
|
->httpResponse() |
|
218
|
|
|
->setError() |
|
219
|
|
|
->setCode(404) |
|
220
|
|
|
->setMessage('Not found'); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return $this |
|
224
|
|
|
->httpResponse() |
|
225
|
|
|
->setData(new PostResource($post)) |
|
226
|
|
|
->toApiResponse(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param Request $request |
|
231
|
|
|
* |
|
232
|
|
|
* @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse |
|
233
|
|
|
*/ |
|
234
|
|
|
#[ |
|
235
|
|
|
Get( |
|
236
|
|
|
path: "/posts/filters", |
|
237
|
|
|
operationId: "postGetWithFilter", |
|
238
|
|
|
description: "Get all posts with pagination (10 items per page by default, page 1 by default) |
|
239
|
|
|
|
|
240
|
|
|
This API will get records from the database and return them as a paginated list. |
|
241
|
|
|
The default number of items per page is 10 and the default page number is 1. You can change these values by passing the `per_page` and `page` query parameters. |
|
242
|
|
|
", |
|
243
|
|
|
summary: "Get posts by filter with pagination", |
|
244
|
|
|
tags: ["Post"], |
|
245
|
|
|
parameters: [ |
|
246
|
|
|
new Parameter( |
|
247
|
|
|
name: 'categories', |
|
248
|
|
|
description: 'Filter posts by categories IDs', |
|
249
|
|
|
in: 'query', |
|
250
|
|
|
required: false, |
|
251
|
|
|
schema: new Schema( |
|
252
|
|
|
type: 'array', |
|
253
|
|
|
items: new Items(description: 'Input the category ID', type: 'integer'), |
|
254
|
|
|
default: null, |
|
255
|
|
|
) |
|
256
|
|
|
), |
|
257
|
|
|
new Parameter( |
|
258
|
|
|
name: 'categories_exclude', |
|
259
|
|
|
description: 'Filter posts by excluding specific category IDs.', |
|
260
|
|
|
in: 'query', |
|
261
|
|
|
required: false, |
|
262
|
|
|
schema: new Schema( |
|
263
|
|
|
type: 'array', |
|
264
|
|
|
items: new Items(description: 'Input the exclude category ID', type: 'integer'), |
|
265
|
|
|
default: null |
|
266
|
|
|
) |
|
267
|
|
|
), |
|
268
|
|
|
new Parameter( |
|
269
|
|
|
name: 'exclude', |
|
270
|
|
|
description: 'Filter posts by excluding specific post IDs.', |
|
271
|
|
|
in: 'query', |
|
272
|
|
|
required: false, |
|
273
|
|
|
schema: new Schema( |
|
274
|
|
|
type: 'array', |
|
275
|
|
|
items: new Items(description: 'Input the exclude post ID', type: 'integer'), |
|
276
|
|
|
default: null |
|
277
|
|
|
) |
|
278
|
|
|
), |
|
279
|
|
|
new Parameter( |
|
280
|
|
|
name: 'author', |
|
281
|
|
|
description: 'Filter posts by author IDs', |
|
282
|
|
|
in: 'query', |
|
283
|
|
|
required: false, |
|
284
|
|
|
schema: new Schema( |
|
285
|
|
|
type: 'array', |
|
286
|
|
|
items: new Items(description: 'Input the author ID', type: 'integer'), |
|
287
|
|
|
default: null |
|
288
|
|
|
) |
|
289
|
|
|
), |
|
290
|
|
|
new Parameter( |
|
291
|
|
|
name: 'author_exclude', |
|
292
|
|
|
description: 'Filter posts by excluding specific author IDs.', |
|
293
|
|
|
in: 'query', |
|
294
|
|
|
required: false, |
|
295
|
|
|
schema: new Schema( |
|
296
|
|
|
type: 'array', |
|
297
|
|
|
items: new Items(description: 'Input the exclude author ID', type: 'integer'), |
|
298
|
|
|
default: null |
|
299
|
|
|
) |
|
300
|
|
|
), |
|
301
|
|
|
new Parameter( |
|
302
|
|
|
name: 'featured', |
|
303
|
|
|
description: 'Filter posts by featured status. Accepts values: |
|
304
|
|
|
1 for featured posts |
|
305
|
|
|
0 for non-featured posts.', |
|
306
|
|
|
in: 'query', |
|
307
|
|
|
required: false, |
|
308
|
|
|
schema: new Schema( |
|
309
|
|
|
type: 'integer', |
|
310
|
|
|
default: null, |
|
311
|
|
|
enum: [0, 1], |
|
312
|
|
|
nullable: true |
|
313
|
|
|
) |
|
314
|
|
|
), |
|
315
|
|
|
new Parameter( |
|
316
|
|
|
name: 'search', |
|
317
|
|
|
description: 'Search for posts where the given keyword appears in either the name or description fields.', |
|
318
|
|
|
in: 'query', |
|
319
|
|
|
required: false, |
|
320
|
|
|
schema: new Schema(type: 'string', default: null) |
|
321
|
|
|
), |
|
322
|
|
|
new Parameter( |
|
323
|
|
|
name: 'per_page', |
|
324
|
|
|
description: 'Number of items per page', |
|
325
|
|
|
in: 'query', |
|
326
|
|
|
required: false, |
|
327
|
|
|
schema: new Schema(type: 'integer', default: 10) |
|
328
|
|
|
), |
|
329
|
|
|
new Parameter( |
|
330
|
|
|
name: 'page', |
|
331
|
|
|
description: 'Page number', |
|
332
|
|
|
in: 'query', |
|
333
|
|
|
required: false, |
|
334
|
|
|
schema: new Schema(type: 'integer', default: 1) |
|
335
|
|
|
), |
|
336
|
|
|
], |
|
337
|
|
|
responses: [ |
|
338
|
|
|
new Response( |
|
339
|
|
|
response: 200, |
|
340
|
|
|
description: "Get posts successfully", |
|
341
|
|
|
content: new JsonContent( |
|
342
|
|
|
properties: [ |
|
343
|
|
|
new Property( |
|
344
|
|
|
property: 'error', |
|
345
|
|
|
description: 'Error status', |
|
346
|
|
|
type: 'boolean', |
|
347
|
|
|
default: false |
|
348
|
|
|
), |
|
349
|
|
|
new Property( |
|
350
|
|
|
property: "data", |
|
351
|
|
|
description: "Data of model", |
|
352
|
|
|
type: "array", |
|
353
|
|
|
items: new Items(ref: PostListResourceSchema::class) |
|
354
|
|
|
), |
|
355
|
|
|
] |
|
356
|
|
|
) |
|
357
|
|
|
), |
|
358
|
|
|
new Response( |
|
359
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class, |
|
360
|
|
|
response: 400, |
|
361
|
|
|
), |
|
362
|
|
|
new Response( |
|
363
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class, |
|
364
|
|
|
response: 404, |
|
365
|
|
|
), |
|
366
|
|
|
new Response( |
|
367
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class, |
|
368
|
|
|
response: 500, |
|
369
|
|
|
), |
|
370
|
|
|
] |
|
371
|
|
|
) |
|
372
|
|
|
] |
|
373
|
|
|
public function getFilters(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse |
|
374
|
|
|
{ |
|
375
|
|
|
$filters = FilterPost::setFilters($request->input()); |
|
376
|
|
|
|
|
377
|
|
|
$data = $this->postRepository->getFilters((array) $filters); |
|
378
|
|
|
|
|
379
|
|
|
return $this |
|
380
|
|
|
->httpResponse() |
|
381
|
|
|
->setData(ListPostResource::collection($data)) |
|
382
|
|
|
->toApiResponse(); |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths