1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Api\Http\Actions\Post; |
4
|
|
|
|
5
|
|
|
use Botble\Base\Http\Responses\BaseHttpResponse; |
|
|
|
|
6
|
|
|
use CSlant\Blog\Api\Http\Requests\Post\PostGetFiltersRequest; |
7
|
|
|
use CSlant\Blog\Api\Http\Resources\Post\ListPostResource; |
8
|
|
|
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Post\PostListResourceSchema; |
9
|
|
|
use CSlant\Blog\Api\Services\PostService; |
10
|
|
|
use CSlant\Blog\Core\Http\Actions\Action; |
11
|
|
|
use CSlant\Blog\Core\Supports\Base\FilterPost; |
12
|
|
|
use Illuminate\Http\JsonResponse; |
13
|
|
|
use Illuminate\Http\RedirectResponse; |
14
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
15
|
|
|
use OpenApi\Attributes\Get; |
16
|
|
|
use OpenApi\Attributes\Items; |
17
|
|
|
use OpenApi\Attributes\JsonContent; |
18
|
|
|
use OpenApi\Attributes\Parameter; |
19
|
|
|
use OpenApi\Attributes\Property; |
20
|
|
|
use OpenApi\Attributes\Response; |
21
|
|
|
use OpenApi\Attributes\Schema; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class PostGetCustomFiltersAction |
25
|
|
|
* |
26
|
|
|
* @group Blog API |
27
|
|
|
* |
28
|
|
|
* @authenticated |
29
|
|
|
* |
30
|
|
|
* @method BaseHttpResponse httpResponse() |
31
|
|
|
* @method BaseHttpResponse setData(mixed $data) |
32
|
|
|
* @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse() |
33
|
|
|
*/ |
34
|
|
|
class PostGetCustomFiltersAction extends Action |
35
|
|
|
{ |
36
|
|
|
protected PostService $postService; |
37
|
|
|
|
38
|
|
|
public function __construct(PostService $postService) |
39
|
|
|
{ |
40
|
|
|
$this->postService = $postService; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param PostGetFiltersRequest $request |
45
|
|
|
* |
46
|
|
|
* @group Blog |
47
|
|
|
* |
48
|
|
|
* @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse |
49
|
|
|
*/ |
50
|
|
|
#[ |
51
|
|
|
Get( |
52
|
|
|
path: "/posts/custom-filters", |
53
|
|
|
operationId: "postGetWithCustomFilters", |
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 posts by custom filters with pagination", |
60
|
|
|
tags: ["Post"], |
61
|
|
|
parameters: [ |
62
|
|
|
new Parameter( |
63
|
|
|
name: 'tags', |
64
|
|
|
description: 'Filter posts by tag specific tag IDs.', |
65
|
|
|
in: 'query', |
66
|
|
|
required: false, |
67
|
|
|
schema: new Schema( |
68
|
|
|
type: 'array', |
69
|
|
|
items: new Items(description: 'Input the exclude tag ID', type: 'integer'), |
70
|
|
|
default: null |
71
|
|
|
) |
72
|
|
|
), |
73
|
|
|
new Parameter( |
74
|
|
|
name: 'categories', |
75
|
|
|
description: 'Filter posts by categories IDs', |
76
|
|
|
in: 'query', |
77
|
|
|
required: false, |
78
|
|
|
schema: new Schema( |
79
|
|
|
type: 'array', |
80
|
|
|
items: new Items(description: 'Input the category ID', type: 'integer'), |
81
|
|
|
default: null, |
82
|
|
|
) |
83
|
|
|
), |
84
|
|
|
new Parameter( |
85
|
|
|
name: 'categories_exclude', |
86
|
|
|
description: 'Filter posts by excluding specific category IDs.', |
87
|
|
|
in: 'query', |
88
|
|
|
required: false, |
89
|
|
|
schema: new Schema( |
90
|
|
|
type: 'array', |
91
|
|
|
items: new Items(description: 'Input the exclude category ID', type: 'integer'), |
92
|
|
|
default: null |
93
|
|
|
) |
94
|
|
|
), |
95
|
|
|
new Parameter( |
96
|
|
|
name: 'exclude', |
97
|
|
|
description: 'Filter posts by excluding specific post IDs.', |
98
|
|
|
in: 'query', |
99
|
|
|
required: false, |
100
|
|
|
schema: new Schema( |
101
|
|
|
type: 'array', |
102
|
|
|
items: new Items(description: 'Input the exclude post ID', type: 'integer'), |
103
|
|
|
default: null |
104
|
|
|
) |
105
|
|
|
), |
106
|
|
|
new Parameter( |
107
|
|
|
name: 'include', |
108
|
|
|
description: 'Filter posts by include post IDs.', |
109
|
|
|
in: 'query', |
110
|
|
|
required: false, |
111
|
|
|
schema: new Schema( |
112
|
|
|
type: 'array', |
113
|
|
|
items: new Items(description: 'Input the include post ID', type: 'integer'), |
114
|
|
|
default: null |
115
|
|
|
) |
116
|
|
|
), |
117
|
|
|
new Parameter( |
118
|
|
|
name: 'author', |
119
|
|
|
description: 'Filter posts by author IDs', |
120
|
|
|
in: 'query', |
121
|
|
|
required: false, |
122
|
|
|
schema: new Schema( |
123
|
|
|
type: 'array', |
124
|
|
|
items: new Items(description: 'Input the author ID', type: 'integer'), |
125
|
|
|
default: null |
126
|
|
|
) |
127
|
|
|
), |
128
|
|
|
new Parameter( |
129
|
|
|
name: 'author_exclude', |
130
|
|
|
description: 'Filter posts by excluding specific author IDs.', |
131
|
|
|
in: 'query', |
132
|
|
|
required: false, |
133
|
|
|
schema: new Schema( |
134
|
|
|
type: 'array', |
135
|
|
|
items: new Items(description: 'Input the exclude author ID', type: 'integer'), |
136
|
|
|
default: null |
137
|
|
|
) |
138
|
|
|
), |
139
|
|
|
new Parameter( |
140
|
|
|
name: 'featured', |
141
|
|
|
description: 'Filter posts by featured status. Accepts values: |
142
|
|
|
1 for featured posts |
143
|
|
|
0 for non-featured posts.', |
144
|
|
|
in: 'query', |
145
|
|
|
required: false, |
146
|
|
|
schema: new Schema( |
147
|
|
|
type: 'integer', |
148
|
|
|
default: null, |
149
|
|
|
enum: [0, 1], |
150
|
|
|
nullable: true |
151
|
|
|
) |
152
|
|
|
), |
153
|
|
|
new Parameter( |
154
|
|
|
name: 'search', |
155
|
|
|
description: 'Search for posts where the given keyword appears in either the name or description fields.', |
156
|
|
|
in: 'query', |
157
|
|
|
required: false, |
158
|
|
|
schema: new Schema(type: 'string', default: null) |
159
|
|
|
), |
160
|
|
|
new Parameter( |
161
|
|
|
name: 'order_by', |
162
|
|
|
description: 'Can order by field: id, views, comments_count, likes_count, created_at, ...', |
163
|
|
|
in: 'query', |
164
|
|
|
required: false, |
165
|
|
|
schema: new Schema(type: 'string', default: 'created_at') |
166
|
|
|
), |
167
|
|
|
new Parameter( |
168
|
|
|
name: 'order', |
169
|
|
|
description: 'Order direction: |
170
|
|
|
ASC for ascending |
171
|
|
|
DESC for descending', |
172
|
|
|
in: 'query', |
173
|
|
|
required: false, |
174
|
|
|
schema: new Schema(type: 'string', default: 'ASC', enum: ['ASC', 'DESC']) |
175
|
|
|
), |
176
|
|
|
new Parameter( |
177
|
|
|
name: 'per_page', |
178
|
|
|
description: 'Number of items per page', |
179
|
|
|
in: 'query', |
180
|
|
|
required: false, |
181
|
|
|
schema: new Schema(type: 'integer', default: 10) |
182
|
|
|
), |
183
|
|
|
new Parameter( |
184
|
|
|
name: 'page', |
185
|
|
|
description: 'Page number', |
186
|
|
|
in: 'query', |
187
|
|
|
required: false, |
188
|
|
|
schema: new Schema(type: 'integer', default: 1) |
189
|
|
|
), |
190
|
|
|
], |
191
|
|
|
responses: [ |
192
|
|
|
new Response( |
193
|
|
|
response: 200, |
194
|
|
|
description: "Get list posts successfully", |
195
|
|
|
content: new JsonContent( |
196
|
|
|
properties: [ |
197
|
|
|
new Property( |
198
|
|
|
property: 'error', |
199
|
|
|
description: 'Error status', |
200
|
|
|
type: 'boolean', |
201
|
|
|
default: false |
202
|
|
|
), |
203
|
|
|
new Property( |
204
|
|
|
property: "data", |
205
|
|
|
ref: PostListResourceSchema::class, |
206
|
|
|
description: "Data of model", |
207
|
|
|
type: "object", |
208
|
|
|
), |
209
|
|
|
] |
210
|
|
|
) |
211
|
|
|
), |
212
|
|
|
new Response( |
213
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class, |
214
|
|
|
response: 400, |
215
|
|
|
), |
216
|
|
|
new Response( |
217
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class, |
218
|
|
|
response: 404, |
219
|
|
|
), |
220
|
|
|
new Response( |
221
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class, |
222
|
|
|
response: 500, |
223
|
|
|
), |
224
|
|
|
] |
225
|
|
|
) |
226
|
|
|
] |
227
|
|
|
public function __invoke(PostGetFiltersRequest $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse |
228
|
|
|
{ |
229
|
|
|
$filters = FilterPost::setFilters($request->validated()); |
230
|
|
|
|
231
|
|
|
$data = $this->postService->getCustomFilters((array) $filters); |
232
|
|
|
|
233
|
|
|
return $this |
234
|
|
|
->httpResponse() |
235
|
|
|
->setData(ListPostResource::collection($data)) |
236
|
|
|
->toApiResponse(); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
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