From: Jérôme Benoit Date: Thu, 28 Jun 2018 10:21:33 +0000 (+0200) Subject: Simplify the code. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=1faa29dccd972e343ecbdc462238702c881493fd;p=Project_proches_de_moi-server.git Simplify the code. Signed-off-by: Jérôme Benoit --- diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index 102b698..d4bd7ff 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -16,7 +16,7 @@ class PersonController extends Controller { /** * @Rest\Post( - * path = "/api/person/inscription", + * path = "/person/inscription", * name = "create_person" * ) * @Rest\View(StatusCode = Response::HTTP_CREATED) @@ -29,7 +29,6 @@ class PersonController extends Controller $em->persist($person); $em->flush(); - //TODO: use ViewHandler return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); } @@ -61,10 +60,8 @@ class PersonController extends Controller $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App:Person')->find($request->get('id')); - $viewHandler = $this->get('fos_rest.view_handler'); - if (empty($person)) { - return $this->PersonNotFound($viewHandler); + return $this->PersonNotFound(); } $person->setFirstName($request->get('firstname')); @@ -74,7 +71,6 @@ class PersonController extends Controller $em->merge($person); $em->flush(); - //TODO: use ViewHandler return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); } @@ -94,17 +90,11 @@ class PersonController extends Controller $em = $this->getDoctrine()->getManager(); $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); - $viewHandler = $this->get('fos_rest.view_handler'); - if (empty($localisations)) { - return $this->PersonLocalisationsNotFound($viewHandler); + return $this->PersonLocalisationsNotFound(); } - $view = View::create($localisations); - $view->setFormat('json'); - - return $viewHandler->handle($view); - + return $localisations; } /** @@ -138,16 +128,11 @@ class PersonController extends Controller $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App:Person')->find($request->get('id')); - $viewHandler = $this->get('fos_rest.view_handler'); - if (empty($person)) { - return $this->PersonNotFound($viewHandler); + return $this->PersonNotFound(); } - $view = View::create($person); - $view->setFormat('json'); - - return $viewHandler->handle($view); + return $person; } /** @@ -163,16 +148,11 @@ class PersonController extends Controller $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]); - $viewHandler = $this->get('fos_rest.view_handler'); - if (empty($person)) { - return $this->PersonNotFound($viewHandler); + return $this->PersonNotFound(); } - $view = View::create($person); - $view->setFormat('json'); - - return $viewHandler->handle($view); + return $person; } /** @@ -188,16 +168,11 @@ class PersonController extends Controller $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App:Person')->find($request->get('id')); - $viewHandler = $this->get('fos_rest.view_handler'); - if (empty($person)) { - return $this->PersonNotFound($viewHandler); + return $this->PersonNotFound(); } - $view = View::create($person->getFriends()); - $view->setFormat('json'); - - return $viewHandler->handle($view); + return $person->getFriends(); } /** @@ -212,16 +187,10 @@ class PersonController extends Controller $em = $this->getDoctrine()->getManager(); $persons = $em->getRepository('App:Person')->findAll(); - $viewHandler = $this->get('fos_rest.view_handler'); - if (empty($persons)) { - return $this->PersonNotFound($viewHandler); + return $this->PersonNotFound(); } - // $view = View::create($persons); - // $view->setFormat('json'); - // - // return $viewHandler->handle($view); return $persons; } @@ -238,24 +207,19 @@ class PersonController extends Controller $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]); - $viewHandler = $this->get('fos_rest.view_handler'); - if (empty($person)) { - return $this->PersonNotFound($viewHandler); + return $this->PersonNotFound(); } - $view = View::create($person->getFriends()); - $view->setFormat('json'); - - return $viewHandler->handle($view); + return $person->getFriends(); } - private function PersonNotFound($viewHandler) { - return $viewHandler->handle(View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND)); + private function PersonNotFound() { + return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } - private function PersonLocalisationsNotFound($viewHandler) { - return $viewHandler->handle(View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND)); + private function PersonLocalisationsNotFound() { + return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND); } }