|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Blog; |
|
6
|
|
|
|
|
7
|
|
|
use App\Formatter\PaginatorFormatter; |
|
8
|
|
|
use App\User\UserRequest; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
10
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
|
11
|
|
|
use OpenApi\Annotations as OA; |
|
12
|
|
|
use Yiisoft\RequestModel\Attribute\Query; |
|
13
|
|
|
use Yiisoft\RequestModel\Attribute\Route; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @OA\Tag( |
|
17
|
|
|
* name="blog", |
|
18
|
|
|
* description="Blog" |
|
19
|
|
|
* ) |
|
20
|
|
|
* @OA\Parameter( |
|
21
|
|
|
* @OA\Schema( |
|
22
|
|
|
* type="int", |
|
23
|
|
|
* example="2" |
|
24
|
|
|
* ), |
|
25
|
|
|
* in="query", |
|
26
|
|
|
* name="page", |
|
27
|
5 |
|
* parameter="PageRequest" |
|
28
|
|
|
* ) |
|
29
|
|
|
*/ |
|
30
|
|
|
final class BlogController |
|
31
|
|
|
{ |
|
32
|
|
|
private DataResponseFactoryInterface $responseFactory; |
|
33
|
|
|
private PostRepository $postRepository; |
|
34
|
5 |
|
private PostFormatter $postFormatter; |
|
35
|
5 |
|
private PostBuilder $postBuilder; |
|
36
|
5 |
|
private BlogService $blogService; |
|
37
|
5 |
|
|
|
38
|
5 |
|
public function __construct( |
|
39
|
|
|
PostRepository $postRepository, |
|
40
|
|
|
DataResponseFactoryInterface $responseFactory, |
|
41
|
|
|
PostFormatter $postFormatter, |
|
42
|
|
|
PostBuilder $postBuilder, |
|
43
|
|
|
BlogService $blogService |
|
44
|
|
|
) { |
|
45
|
|
|
$this->postRepository = $postRepository; |
|
46
|
|
|
$this->responseFactory = $responseFactory; |
|
47
|
|
|
$this->postFormatter = $postFormatter; |
|
48
|
|
|
$this->postBuilder = $postBuilder; |
|
49
|
|
|
$this->blogService = $blogService; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @OA\Get( |
|
54
|
|
|
* tags={"blog"}, |
|
55
|
|
|
* path="/blog/", |
|
56
|
|
|
* summary="Returns paginated blog posts", |
|
57
|
|
|
* description="", |
|
58
|
|
|
* @OA\Parameter(ref="#/components/parameters/PageRequest"), |
|
59
|
|
|
* @OA\Response( |
|
60
|
|
|
* response="200", |
|
61
|
|
|
* description="Success", |
|
62
|
|
|
* @OA\JsonContent( |
|
63
|
|
|
* allOf={ |
|
64
|
|
|
* @OA\Schema(ref="#/components/schemas/Response"), |
|
65
|
|
|
* @OA\Schema( |
|
66
|
|
|
* @OA\Property( |
|
67
|
|
|
* property="data", |
|
68
|
|
|
* type="object", |
|
69
|
|
|
* @OA\Property( |
|
70
|
|
|
* property="posts", |
|
71
|
|
|
* type="array", |
|
72
|
|
|
* @OA\Items(ref="#/components/schemas/Post") |
|
73
|
|
|
* ), |
|
74
|
|
|
* @OA\Property( |
|
75
|
1 |
|
* property="paginator", |
|
76
|
|
|
* type="object", |
|
77
|
1 |
|
* ref="#/components/schemas/Paginator" |
|
78
|
1 |
|
* ), |
|
79
|
1 |
|
* ), |
|
80
|
1 |
|
* ), |
|
81
|
|
|
* }, |
|
82
|
|
|
* ) |
|
83
|
1 |
|
* ), |
|
84
|
|
|
* ) |
|
85
|
1 |
|
*/ |
|
86
|
|
|
public function index(PaginatorFormatter $paginatorFormatter, #[Query('page')] int $page = 1): Response |
|
87
|
|
|
{ |
|
88
|
|
|
$paginator = $this->blogService->getPosts($page); |
|
89
|
|
|
$posts = []; |
|
90
|
|
|
foreach ($paginator->read() as $post) { |
|
91
|
|
|
$posts[] = $this->postFormatter->format($post); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->responseFactory->createResponse( |
|
95
|
|
|
[ |
|
96
|
|
|
'paginator' => $paginatorFormatter->format($paginator), |
|
97
|
|
|
'posts' => $posts, |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @OA\Get( |
|
104
|
|
|
* tags={"blog"}, |
|
105
|
|
|
* path="/blog/{id}", |
|
106
|
|
|
* summary="Returns a post with a given ID", |
|
107
|
|
|
* description="", |
|
108
|
|
|
* @OA\Parameter( |
|
109
|
|
|
* @OA\Schema(type="int", example="2"), |
|
110
|
|
|
* in="path", |
|
111
|
|
|
* name="id", |
|
112
|
|
|
* parameter="id" |
|
113
|
|
|
* ), |
|
114
|
|
|
* @OA\Response( |
|
115
|
|
|
* response="200", |
|
116
|
|
|
* description="Success", |
|
117
|
|
|
* @OA\JsonContent( |
|
118
|
|
|
* allOf={ |
|
119
|
|
|
* @OA\Schema(ref="#/components/schemas/Response"), |
|
120
|
|
|
* @OA\Schema( |
|
121
|
|
|
* @OA\Property( |
|
122
|
|
|
* property="data", |
|
123
|
|
|
* type="object", |
|
124
|
|
|
* @OA\Property( |
|
125
|
|
|
* property="post", |
|
126
|
|
|
* type="object", |
|
127
|
|
|
* ref="#/components/schemas/Post" |
|
128
|
|
|
* ), |
|
129
|
|
|
* ), |
|
130
|
|
|
* ), |
|
131
|
|
|
* }, |
|
132
|
|
|
* ) |
|
133
|
|
|
* ), |
|
134
|
|
|
* @OA\Response( |
|
135
|
|
|
* response="404", |
|
136
|
|
|
* description="Not found", |
|
137
|
|
|
* @OA\JsonContent( |
|
138
|
1 |
|
* allOf={ |
|
139
|
|
|
* @OA\Schema(ref="#/components/schemas/BadResponse"), |
|
140
|
1 |
|
* @OA\Schema( |
|
141
|
|
|
* @OA\Property(property="error_message", example="Entity not found"), |
|
142
|
1 |
|
* @OA\Property(property="error_code", nullable=true, example=404) |
|
143
|
1 |
|
* ), |
|
144
|
|
|
* }, |
|
145
|
|
|
* ) |
|
146
|
|
|
* ), |
|
147
|
|
|
* ) |
|
148
|
|
|
*/ |
|
149
|
|
|
public function view(#[Route('id')] int $id): Response |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->responseFactory->createResponse( |
|
152
|
|
|
[ |
|
153
|
|
|
'post' => $this->postFormatter->format( |
|
154
|
|
|
$this->blogService->getPost($id) |
|
155
|
|
|
), |
|
156
|
|
|
] |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @OA\Post( |
|
162
|
|
|
* tags={"blog"}, |
|
163
|
|
|
* path="/blog", |
|
164
|
|
|
* summary="Creates a blog post", |
|
165
|
|
|
* description="", |
|
166
|
|
|
* security={{"ApiKey": {}}}, |
|
167
|
|
|
* @OA\Response( |
|
168
|
|
|
* response="200", |
|
169
|
|
|
* description="Success", |
|
170
|
|
|
* @OA\JsonContent( |
|
171
|
|
|
* ref="#/components/schemas/Response" |
|
172
|
1 |
|
* ) |
|
173
|
|
|
* ), |
|
174
|
1 |
|
* @OA\RequestBody( |
|
175
|
1 |
|
* required=true, |
|
176
|
|
|
* @OA\MediaType( |
|
177
|
1 |
|
* mediaType="application/json", |
|
178
|
|
|
* @OA\Schema(ref="#/components/schemas/EditPostRequest"), |
|
179
|
1 |
|
* ), |
|
180
|
|
|
* ), |
|
181
|
|
|
* ) |
|
182
|
|
|
*/ |
|
183
|
|
|
public function create(EditPostRequest $postRequest, UserRequest $userRequest): Response |
|
184
|
|
|
{ |
|
185
|
|
|
$post = $this->postBuilder->build(new Post(), $postRequest); |
|
186
|
|
|
$post->setUser($userRequest->getUser()); |
|
187
|
|
|
|
|
188
|
|
|
$this->postRepository->save($post); |
|
189
|
|
|
|
|
190
|
|
|
return $this->responseFactory->createResponse(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @OA\Put( |
|
195
|
|
|
* tags={"blog"}, |
|
196
|
|
|
* path="/blog/{id}", |
|
197
|
|
|
* summary="Updates a blog post with a given ID", |
|
198
|
|
|
* description="", |
|
199
|
|
|
* security={{"ApiKey": {}}}, |
|
200
|
|
|
* @OA\Parameter( |
|
201
|
|
|
* @OA\Schema(type="int", example="2"), |
|
202
|
|
|
* in="path", |
|
203
|
|
|
* name="id", |
|
204
|
|
|
* parameter="id" |
|
205
|
|
|
* ), |
|
206
|
|
|
* @OA\Response( |
|
207
|
|
|
* response="200", |
|
208
|
|
|
* description="Success", |
|
209
|
|
|
* @OA\JsonContent( |
|
210
|
|
|
* ref="#/components/schemas/Response" |
|
211
|
1 |
|
* ) |
|
212
|
|
|
* ), |
|
213
|
1 |
|
* @OA\RequestBody( |
|
214
|
1 |
|
* required=true, |
|
215
|
|
|
* @OA\MediaType( |
|
216
|
|
|
* mediaType="application/json", |
|
217
|
|
|
* @OA\Schema(ref="#/components/schemas/EditPostRequest"), |
|
218
|
1 |
|
* ), |
|
219
|
|
|
* ) |
|
220
|
1 |
|
* ) |
|
221
|
|
|
*/ |
|
222
|
|
|
public function update(EditPostRequest $postRequest, #[Route('id')] int $id): Response |
|
223
|
|
|
{ |
|
224
|
|
|
$post = $this->postBuilder->build( |
|
225
|
|
|
$this->blogService->getPost($id), |
|
226
|
|
|
$postRequest |
|
227
|
|
|
); |
|
228
|
|
|
|
|
229
|
|
|
$this->postRepository->save($post); |
|
230
|
|
|
|
|
231
|
|
|
return $this->responseFactory->createResponse(); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|