X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FController%2FPersonController.php;h=f2e73c132454b5e4d3bf98fc763ab7f121b7fe0f;hb=1de9ba73a6b77b98e94f797623b80b9d993cf01e;hp=a47df7c50736291fc9ef14ab4a31540673b04b4d;hpb=51963d7f74d201615de629e0f6bfd4133e9e3a79;p=Project_proches_de_moi-server.git diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index a47df7c..f2e73c1 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -2,19 +2,21 @@ namespace App\Controller; use App\Entity\Person; -use FOS\RestBundle\Controller\FOSRestController; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; +#use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; +use FOS\RestBundle\View\ViewHandler; +use FOS\RestBundle\View\View; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -class PersonController extends FOSRestController +class PersonController extends Controller { /** - * @Rest\Prefix("/api") * @Rest\Post( - * path = "/person/inscription", + * path = "/api/person/inscription", * name = "create_person" * ) * @Rest\View(StatusCode = Response::HTTP_CREATED) @@ -31,23 +33,24 @@ class PersonController extends FOSRestController } /** - * @Rest\Delete("/person/{id}") + * @Rest\Delete("/api/person/{id}") * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) */ public function removePersonAction(Request $request) { $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App::Person')->find($request->get('id')); + $person = $em->getRepository('App:Person')->find($request->get('id')); if (!empty($person)) { $em->remove($person); $em->flush(); } + //TODO: remove localisation and friendship } /** * @Rest\Put( - * path = "/person/{id}/update", + * path = "/api/person/{id}/update", * name = "update_person" * ) * @Rest\View(StatusCode = Response::HTTP_CREATED) @@ -55,10 +58,10 @@ class PersonController extends FOSRestController public function updatePersonAction(Request $request) { $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App::Person')->find($request->get('id')); + $person = $em->getRepository('App:Person')->find($request->get('id')); if (empty($person)) { - return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); + return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } $person->setFirstName($request->get('firstname')); @@ -72,7 +75,7 @@ class PersonController extends FOSRestController } /** - * @Rest\Get("/person/{id}/friends/localisation") + * @Rest\Get("/api/person/{id}/friends/localisation") */ public function getFriendsLocalisationAction(Request $request) { @@ -80,7 +83,27 @@ class PersonController extends FOSRestController } /** - * @Rest\Post("/person/{id}/localisation") + * @Rest\Get("/api/person/{id}/localisations") + */ + public function getLocalisationsAction(Request $request) + { + $em = $this->getDoctrine()->getManager(); + $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); + + if (empty($localisations)) { + return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND); + } + + $view = View::create($localisations); + $view->setFormat('json'); + + $viewHandler = $this->get('fos_rest.view_handler'); + return $viewHandler->handle($view); + + } + + /** + * @Rest\Post("/api/person/{id}/localisation") * @Rest\View(StatusCode = Response::HTTP_CREATED) */ public function updateLocalisationAction(Request $request) @@ -99,7 +122,7 @@ class PersonController extends FOSRestController /** * @Rest\Get( - * path = "/person/{id}", + * path = "/api/person/{id}", * name = "show_person", * requirements = {"id"="\d+"} * ) @@ -107,19 +130,23 @@ class PersonController extends FOSRestController */ public function showPerson(Request $request) { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App::Person')->find($request->get('id')); + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('id')); - if (empty($person)) { - return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); + if (empty($person)) { + return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } - return $person; + $view = View::create($person); + $view->setFormat('json'); + + $viewHandler = $this->get('fos_rest.view_handler'); + return $viewHandler->handle($view); } /** * @Rest\Get( - * path = "/person/{email}", + * path = "/api/person/{email}", * name = "show_person_by_email", * requirements = {"email"="\s+"} * ) @@ -127,19 +154,23 @@ class PersonController extends FOSRestController */ public function showPersonByEmail(Request $request) { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App::Person')->find($request->get('email')); + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('email')); - if (empty($person)) { - return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); - } + if (empty($person)) { + return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); + } - return $person; + $view = View::create($person); + $view->setFormat('json'); + + $viewHandler = $this->get('fos_rest.view_handler'); + return $viewHandler->handle($view); } /** * @Rest\Get( - * path = "/person/{id}/friends", + * path = "/api/person/{id}/friends", * name = "show_person_friends", * requirements = {"id"="\d+"} * ) @@ -147,19 +178,23 @@ class PersonController extends FOSRestController */ public function showPersonFriends(Request $request) { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App::Person')->find($request->get('id')); + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('id')); - if (empty($person)) { - return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); - } + if (empty($person)) { + return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); + } + + $view = View::create($person->getFriends()); + $view->setFormat('json'); - return $person->getFriends(); + $viewHandler = $this->get('fos_rest.view_handler'); + return $viewHandler->handle($view); } /** * @Rest\Get( - * path = "/person/{email}/friends", + * path = "/api/person/{email}/friends", * name = "show_person_friends_by_email", * requirements = {"email"="\s+"} * ) @@ -167,13 +202,17 @@ class PersonController extends FOSRestController */ public function showPersonFriendsByEmail(Request $request) { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App::Person')->find($request->get('email')); + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('email')); + + if (empty($person)) { + return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); + } - if (empty($person)) { - return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); - } + $view = View::create($person->getFriends()); + $view->setFormat('json'); - return $person->getFriends(); + $viewHandler = $this->get('fos_rest.view_handler'); + return $viewHandler->handle($view); } }