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