3838use Psr \Http \Message \ResponseInterface ;
3939use Throwable ;
4040
41+ use function assert ;
42+
4143class AdminController extends AbstractActionController
4244{
4345 use ServerRequestAwareTrait;
@@ -162,42 +164,54 @@ public function editAction(): ResponseInterface
162164
163165 public function deleteAction (): ResponseInterface
164166 {
165- if (! $ this ->isPost ()) {
166- return new JsonResponse (
167- ['message ' => Message::METHOD_NOT_ALLOWED ],
168- StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED
169- );
170- }
171-
172167 $ uuid = $ this ->getAttribute ('uuid ' );
173168 if (empty ($ uuid )) {
174169 return new JsonResponse (
175170 ['message ' => Message::ADMIN_NOT_FOUND ],
176171 StatusCodeInterface::STATUS_NOT_FOUND
177172 );
178173 }
174+ $ admin = $ this ->adminService ->getAdminRepository ()->findOneBy (['uuid ' => $ uuid ]);
175+ assert ($ admin instanceof Admin);
179176
180177 $ form = new AdminDeleteForm ();
181- $ form ->setData ($ this ->getPostParams ());
182- if (! $ form ->isValid ()) {
183- return new JsonResponse (
184- ['message ' => $ this ->forms ->getMessages ($ form )],
185- StatusCodeInterface::STATUS_BAD_REQUEST
186- );
187- }
178+ $ form ->setAttribute ('id ' , 'deleteAdminForm ' );
179+ $ form ->setAttribute ('method ' , RequestMethodInterface::METHOD_POST );
180+ $ form ->setAttribute (
181+ 'action ' ,
182+ $ this ->router ->generateUri ('admin ' , ['action ' => 'delete ' , 'uuid ' => $ uuid ])
183+ );
188184
189- /** @var Admin $admin */
190- $ admin = $ this ->adminService ->getAdminRepository ()->findOneBy (['uuid ' => $ uuid ]);
191- try {
192- $ this ->adminService ->getAdminRepository ()->deleteAdmin ($ admin );
193- return new JsonResponse (['message ' => Message::ADMIN_DELETED_SUCCESSFULLY ]);
194- } catch (Throwable $ e ) {
195- $ this ->logErrors ($ e , Message::DELETE_ADMIN );
196- return new JsonResponse (
197- ['message ' => Message::AN_ERROR_OCCURRED ],
198- StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR
199- );
185+ if ($ this ->isPost ()) {
186+ $ form ->setData ($ this ->getPostParams ());
187+ if (! $ form ->isValid ()) {
188+ return new JsonResponse (
189+ ['message ' => $ this ->forms ->getMessages ($ form )],
190+ StatusCodeInterface::STATUS_BAD_REQUEST
191+ );
192+ }
193+
194+ try {
195+ $ this ->adminService ->getAdminRepository ()->deleteAdmin ($ admin );
196+ return new JsonResponse (['message ' => Message::ADMIN_DELETED_SUCCESSFULLY ]);
197+ } catch (Throwable $ e ) {
198+ $ this ->logErrors ($ e , Message::DELETE_ADMIN );
199+ return new JsonResponse (
200+ ['message ' => Message::AN_ERROR_OCCURRED ],
201+ StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR
202+ );
203+ }
200204 }
205+
206+ return new JsonResponse ([
207+ 'data ' => $ this ->template ->render (
208+ 'admin::delete ' ,
209+ [
210+ 'admin ' => $ admin ,
211+ 'form ' => $ form ->prepare (),
212+ ]
213+ ),
214+ ]);
201215 }
202216
203217 public function listAction (): ResponseInterface
@@ -216,9 +230,7 @@ public function listAction(): ResponseInterface
216230 public function manageAction (): ResponseInterface
217231 {
218232 return new HtmlResponse (
219- $ this ->template ->render ('admin::list ' , [
220- 'form ' => new AdminDeleteForm (),
221- ])
233+ $ this ->template ->render ('admin::list ' )
222234 );
223235 }
224236
0 commit comments