From 117c10a56a0dea77a8713913d3adb7de4851e21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Danyi?= Date: Mon, 9 Apr 2018 18:41:04 +0200 Subject: [PATCH] * changed routes while debuging endpoints * added corsmiddlewarefactory * changing teams is now possible --- config/autoload/dependencies.global.php | 1 + config/routes.php | 7 +++++- src/App/Handler/TeamHandler.php | 23 +++++++++++++++++++ src/App/Service/TeamService.php | 22 +++++++++++------- .../Middleware/CorsMiddlewareFactory.php | 18 +++++++++++++++ 5 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 src/DoctrineExpressiveModule/Middleware/CorsMiddlewareFactory.php diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index a9ab249..479de17 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -21,6 +21,7 @@ return [ // Use 'factories' for services provided by callbacks/factory classes. 'factories' => [ // Fully\Qualified\ClassName::class => Fully\Qualified\FactoryName::class, + Tuupola\Middleware\CorsMiddleware::class => DoctrineExpressiveModule\Middleware\CorsMiddlewareFactory::class, ], ], ]; diff --git a/config/routes.php b/config/routes.php index eb319ca..a0ac2f6 100644 --- a/config/routes.php +++ b/config/routes.php @@ -36,6 +36,11 @@ return function (Application $app, MiddlewareFactory $factory, ContainerInterfac $app->get('/', App\Handler\HomePageHandler::class, 'home'); $app->get('/api/ping', App\Handler\PingHandler::class, 'api.ping'); - $app->route('/api/team[/{id:\d+}]', App\Handler\TeamHandler::class)->setName('api.team'); + $app->get('/api/team', App\Handler\TeamHandler::class,'api.team.list'); + $app->get('/api/team/{id:\d+}', App\Handler\TeamHandler::class,'api.team.get'); + $app->post('/api/team', App\Handler\TeamHandler::class,'api.team.add'); + $app->put('/api/team[/{id:\d+}]', App\Handler\TeamHandler::class,'api.team.change'); + $app->delete('/api/team/{id:\d+}', App\Handler\TeamHandler::class,'api.team.delete'); + $app->route('/api/slide[/{id:\d+}]', App\Handler\SlideHandler::class)->setName('api.slide'); }; diff --git a/src/App/Handler/TeamHandler.php b/src/App/Handler/TeamHandler.php index 81afc6f..962b830 100644 --- a/src/App/Handler/TeamHandler.php +++ b/src/App/Handler/TeamHandler.php @@ -60,6 +60,29 @@ class TeamHandler extends AbstractCrudHandler } } + /** + * @param ServerRequestInterface $request + * @return JsonResponse + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function update(ServerRequestInterface $request) + { + $id = $request->getAttribute('id'); + $data = $this->getRequestData($request); + try { + return new JsonResponse($this->teamService->changeTeam((int)$id, $data)); + } catch (UniqueConstraintViolationException $e) { + return new JsonResponse([ + 'message' => 'The field `name` must be unique', + ], 500); + } catch (\InvalidArgumentException $e) { + return new JsonResponse([ + 'message' => $e->getMessage(), + ], 500); + } + } + /** * @param ServerRequestInterface $request * @return JsonResponse diff --git a/src/App/Service/TeamService.php b/src/App/Service/TeamService.php index 4e37f87..163c10e 100644 --- a/src/App/Service/TeamService.php +++ b/src/App/Service/TeamService.php @@ -63,14 +63,14 @@ class TeamService $this->em->persist($entity); $this->em->flush(); return $entity; - } else { - $messages = $this->form->getMessages(); - $fields = array_keys($messages); - throw new \InvalidArgumentException(sprintf( - "The following fields are invalid: (%s)", - implode(", ", $fields) - )); } + + $messages = $this->form->getMessages(); + $fields = array_keys($messages); + throw new \InvalidArgumentException(sprintf( + "The following fields are invalid: (%s)", + implode(", ", $fields) + )); } /** @@ -93,7 +93,13 @@ class TeamService $this->em->flush(); return true; } - return false; + + $messages = $this->form->getMessages(); + $fields = array_keys($messages); + throw new \InvalidArgumentException(sprintf( + "The following fields are invalid: (%s)", + implode(", ", $fields) + )); } /** diff --git a/src/DoctrineExpressiveModule/Middleware/CorsMiddlewareFactory.php b/src/DoctrineExpressiveModule/Middleware/CorsMiddlewareFactory.php new file mode 100644 index 0000000..2d6143e --- /dev/null +++ b/src/DoctrineExpressiveModule/Middleware/CorsMiddlewareFactory.php @@ -0,0 +1,18 @@ + ["Authorization", "If-Match", "If-Unmodified-Since", "Content-type"], + ]); + } +} \ No newline at end of file