1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Api\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Botble\Base\Http\Responses\BaseHttpResponse; |
|
|
|
|
6
|
|
|
use CSlant\Blog\Api\Http\Resources\Category\ListCategoryResource; |
7
|
|
|
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Category\CategoryListResourceSchema; |
8
|
|
|
use CSlant\Blog\Api\Services\CategoryService; |
9
|
|
|
use CSlant\Blog\Api\Supports\Filters\FilterCategory; |
10
|
|
|
use CSlant\Blog\Core\Enums\StatusEnum; |
11
|
|
|
use CSlant\Blog\Core\Facades\Base\SlugHelper; |
12
|
|
|
use CSlant\Blog\Core\Http\Actions\Action; |
13
|
|
|
use CSlant\Blog\Core\Models\Category; |
14
|
|
|
use CSlant\Blog\Core\Models\Slug; |
15
|
|
|
use Illuminate\Http\JsonResponse; |
16
|
|
|
use Illuminate\Http\RedirectResponse; |
17
|
|
|
use Illuminate\Http\Request; |
18
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
19
|
|
|
use OpenApi\Attributes\Get; |
20
|
|
|
use OpenApi\Attributes\Items; |
21
|
|
|
use OpenApi\Attributes\JsonContent; |
22
|
|
|
use OpenApi\Attributes\Parameter; |
23
|
|
|
use OpenApi\Attributes\Property; |
24
|
|
|
use OpenApi\Attributes\Response; |
25
|
|
|
use OpenApi\Attributes\Schema; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class CategoryController |
29
|
|
|
* |
30
|
|
|
* @package CSlant\Blog\Api\Http\Controllers |
31
|
|
|
* |
32
|
|
|
* @group Blog API |
33
|
|
|
* |
34
|
|
|
* @authenticated |
35
|
|
|
* |
36
|
|
|
* @method BaseHttpResponse httpResponse() |
37
|
|
|
* @method BaseHttpResponse setData(mixed $data) |
38
|
|
|
* @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse() |
39
|
|
|
*/ |
40
|
|
|
class CategoryController extends Action |
41
|
|
|
{ |
42
|
|
|
protected CategoryService $categoryService; |
43
|
|
|
|
44
|
|
|
public function __construct(CategoryService $categoryService) |
45
|
|
|
{ |
46
|
|
|
$this->categoryService = $categoryService; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
#[ |
50
|
|
|
Get( |
51
|
|
|
path: "/categories", |
52
|
|
|
operationId: "categoryGetAllWithFilter", |
53
|
|
|
description: "Get all categories with pagination (10 items per page by default, page 1 by default) |
54
|
|
|
|
55
|
|
|
This API will get records from the database and return them as a paginated list. |
56
|
|
|
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. |
57
|
|
|
", |
58
|
|
|
summary: "Get all categories with pagination", |
59
|
|
|
tags: ["Category"], |
60
|
|
|
parameters: [ |
61
|
|
|
new Parameter( |
62
|
|
|
name: 'per_page', |
63
|
|
|
description: 'Number of items per page', |
64
|
|
|
in: 'query', |
65
|
|
|
required: false, |
66
|
|
|
schema: new Schema(type: 'integer', default: 10) |
67
|
|
|
), |
68
|
|
|
new Parameter( |
69
|
|
|
name: 'page', |
70
|
|
|
description: 'Page number', |
71
|
|
|
in: 'query', |
72
|
|
|
required: false, |
73
|
|
|
schema: new Schema(type: 'integer', default: 1) |
74
|
|
|
), |
75
|
|
|
], |
76
|
|
|
responses: [ |
77
|
|
|
new Response( |
78
|
|
|
response: 200, |
79
|
|
|
description: "Get categories successfully", |
80
|
|
|
content: new JsonContent( |
81
|
|
|
properties: [ |
82
|
|
|
new Property( |
83
|
|
|
property: 'error', |
84
|
|
|
description: 'Error status', |
85
|
|
|
type: 'boolean', |
86
|
|
|
default: false |
87
|
|
|
), |
88
|
|
|
new Property( |
89
|
|
|
property: "data", |
90
|
|
|
description: "Data of model", |
91
|
|
|
type: "array", |
92
|
|
|
items: new Items(ref: CategoryListResourceSchema::class) |
93
|
|
|
), |
94
|
|
|
] |
95
|
|
|
) |
96
|
|
|
), |
97
|
|
|
new Response( |
98
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class, |
99
|
|
|
response: 400, |
100
|
|
|
), |
101
|
|
|
new Response( |
102
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class, |
103
|
|
|
response: 404, |
104
|
|
|
), |
105
|
|
|
new Response( |
106
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class, |
107
|
|
|
response: 500, |
108
|
|
|
), |
109
|
|
|
] |
110
|
|
|
) |
111
|
|
|
] |
112
|
|
|
public function index(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse |
113
|
|
|
{ |
114
|
|
|
$data = Category::query() |
115
|
|
|
->wherePublished() |
116
|
|
|
->orderByDesc('created_at') |
117
|
|
|
->with(['slugable']) |
118
|
|
|
->paginate($request->integer('per_page', 10) ?: 10); |
119
|
|
|
|
120
|
|
|
return $this |
121
|
|
|
->httpResponse() |
122
|
|
|
->setData(ListCategoryResource::collection($data)) |
123
|
|
|
->toApiResponse(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
#[ |
127
|
|
|
Get( |
128
|
|
|
path: "/categories/filters", |
129
|
|
|
operationId: "categoryGetWithFilter", |
130
|
|
|
description: "Get all categories with filters and pagination (10 items per page by default, page 1 by default) |
131
|
|
|
|
132
|
|
|
This API will get records from the database and return them as a paginated list. |
133
|
|
|
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. |
134
|
|
|
", |
135
|
|
|
summary: "Get categories by filters with pagination", |
136
|
|
|
tags: ['Category'], |
137
|
|
|
parameters: [ |
138
|
|
|
new Parameter( |
139
|
|
|
name: 'search', |
140
|
|
|
description: 'Search for categories where the given keyword appears in either the name field.', |
141
|
|
|
in: 'query', |
142
|
|
|
required: false, |
143
|
|
|
schema: new Schema(type: 'string', default: null) |
144
|
|
|
), |
145
|
|
|
new Parameter( |
146
|
|
|
name: 'order_by', |
147
|
|
|
description: 'Can order by field: id, name, created_at, posts_count, ...', |
148
|
|
|
in: 'query', |
149
|
|
|
required: false, |
150
|
|
|
schema: new Schema(type: 'string', default: 'posts_count') |
151
|
|
|
), |
152
|
|
|
new Parameter( |
153
|
|
|
name: 'order', |
154
|
|
|
description: 'Order direction: |
155
|
|
|
ASC for ascending |
156
|
|
|
DESC for descending', |
157
|
|
|
in: 'query', |
158
|
|
|
required: false, |
159
|
|
|
schema: new Schema(type: 'string', default: 'ASC', enum: ['ASC', 'DESC']) |
160
|
|
|
), |
161
|
|
|
new Parameter( |
162
|
|
|
name: 'per_page', |
163
|
|
|
description: 'Number of items per page', |
164
|
|
|
in: 'query', |
165
|
|
|
required: false, |
166
|
|
|
schema: new Schema(type: 'integer', default: 10) |
167
|
|
|
), |
168
|
|
|
new Parameter( |
169
|
|
|
name: 'page', |
170
|
|
|
description: 'Page number', |
171
|
|
|
in: 'query', |
172
|
|
|
required: false, |
173
|
|
|
schema: new Schema(type: 'integer', default: 1) |
174
|
|
|
), |
175
|
|
|
], |
176
|
|
|
responses: [ |
177
|
|
|
new Response( |
178
|
|
|
response: 200, |
179
|
|
|
description: "Get filters successfully", |
180
|
|
|
content: new JsonContent( |
181
|
|
|
properties: [ |
182
|
|
|
new Property( |
183
|
|
|
property: 'error', |
184
|
|
|
description: 'Error status', |
185
|
|
|
type: 'boolean', |
186
|
|
|
default: false |
187
|
|
|
), |
188
|
|
|
new Property( |
189
|
|
|
property: "data", |
190
|
|
|
description: "Filter data", |
191
|
|
|
type: "array", |
192
|
|
|
items: new Items(ref: CategoryListResourceSchema::class) |
193
|
|
|
), |
194
|
|
|
] |
195
|
|
|
) |
196
|
|
|
), |
197
|
|
|
], |
198
|
|
|
) |
199
|
|
|
] |
200
|
|
|
public function getFilters( |
201
|
|
|
Request $request |
202
|
|
|
): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse { |
203
|
|
|
$filters = FilterCategory::setFilters((array) $request->input()); |
204
|
|
|
$data = $this->categoryService->getCustomFilters($filters); |
205
|
|
|
|
206
|
|
|
return $this |
207
|
|
|
->httpResponse() |
208
|
|
|
->setData(ListCategoryResource::collection($data)) |
209
|
|
|
->toApiResponse(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get category by slug |
214
|
|
|
* |
215
|
|
|
* @group Blog |
216
|
|
|
* @queryParam slug Find by slug of category. |
217
|
|
|
* |
218
|
|
|
* @param string $slug |
219
|
|
|
* |
220
|
|
|
* @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse |
221
|
|
|
*/ |
222
|
|
|
#[ |
223
|
|
|
Get( |
224
|
|
|
path: "/categories/{slug}", |
225
|
|
|
operationId: "categoryFilterBySlug", |
226
|
|
|
description: "Get the category by slug |
227
|
|
|
|
228
|
|
|
This API will get records from the database and return the category by slug. |
229
|
|
|
", |
230
|
|
|
summary: "Get category by slug", |
231
|
|
|
tags: ["Category"], |
232
|
|
|
parameters: [ |
233
|
|
|
new Parameter( |
234
|
|
|
name: 'slug', |
235
|
|
|
description: 'Category slug', |
236
|
|
|
in: 'path', |
237
|
|
|
required: true, |
238
|
|
|
schema: new Schema(type: 'string', example: 'php') |
239
|
|
|
), |
240
|
|
|
], |
241
|
|
|
responses: [ |
242
|
|
|
new Response( |
243
|
|
|
response: 200, |
244
|
|
|
description: "Get category successfully", |
245
|
|
|
content: new JsonContent( |
246
|
|
|
properties: [ |
247
|
|
|
new Property( |
248
|
|
|
property: 'error', |
249
|
|
|
description: 'Error status', |
250
|
|
|
type: 'boolean', |
251
|
|
|
default: false |
252
|
|
|
), |
253
|
|
|
new Property( |
254
|
|
|
property: "data", |
255
|
|
|
ref: CategoryListResourceSchema::class, |
256
|
|
|
description: "Data of model", |
257
|
|
|
type: "object", |
258
|
|
|
), |
259
|
|
|
] |
260
|
|
|
) |
261
|
|
|
), |
262
|
|
|
new Response( |
263
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class, |
264
|
|
|
response: 400, |
265
|
|
|
), |
266
|
|
|
new Response( |
267
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class, |
268
|
|
|
response: 404, |
269
|
|
|
), |
270
|
|
|
new Response( |
271
|
|
|
ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class, |
272
|
|
|
response: 500, |
273
|
|
|
), |
274
|
|
|
] |
275
|
|
|
) |
276
|
|
|
] |
277
|
|
|
public function findBySlug(string $slug): JsonResponse|RedirectResponse|JsonResource|BaseHttpResponse |
278
|
|
|
{ |
279
|
|
|
/** @var Slug $slug */ |
280
|
|
|
$slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Category::getBaseModel())); |
281
|
|
|
|
282
|
|
|
if (!$slug) { |
|
|
|
|
283
|
|
|
return $this |
284
|
|
|
->httpResponse() |
285
|
|
|
->setError() |
286
|
|
|
->setCode(404) |
287
|
|
|
->setMessage('Not found'); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$category = Category::query() |
291
|
|
|
->with(['slugable']) |
292
|
|
|
->where([ |
293
|
|
|
'id' => $slug->reference_id, |
294
|
|
|
'status' => StatusEnum::PUBLISHED, |
295
|
|
|
]) |
296
|
|
|
->first(); |
297
|
|
|
|
298
|
|
|
if (!$category) { |
299
|
|
|
return $this |
300
|
|
|
->httpResponse() |
301
|
|
|
->setError() |
302
|
|
|
->setCode(404) |
303
|
|
|
->setMessage('Not found'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $this |
307
|
|
|
->httpResponse() |
308
|
|
|
->setData(ListCategoryResource::make($category)) |
309
|
|
|
->toApiResponse(); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
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