2 namespace App\Controller
;
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
6 #use FOS\RestBundle\Controller\FOSRestController;
7 use FOS\RestBundle\Controller\Annotations
as Rest
;
8 use FOS\RestBundle\View\ViewHandler
;
9 use FOS\RestBundle\View\View
;
10 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
11 use Symfony\Component\HttpFoundation\Request
;
12 use Symfony\Component\HttpFoundation\Response
;
13 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
15 class PersonController
extends Controller
19 * path = "/api/person/inscription",
20 * name = "create_person"
22 * @Rest\View(StatusCode = Response::HTTP_CREATED)
23 * @ParamConverter("person", converter="fos_rest.request_body")
25 public function createPersonAction(Person
$person)
27 $em = $this->getDoctrine()->getManager();
29 $em->persist($person);
32 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
36 * @Rest\Delete("/api/person/{id}")
37 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
39 public function removePersonAction(Request
$request)
41 $em = $this->getDoctrine()->getManager();
42 $person = $em->getRepository('App:Person')->find($request->get('id'));
44 if (!empty($person)) {
48 //TODO: remove localisation and friendship
53 * path = "/api/person/{id}/update",
54 * name = "update_person"
56 * @Rest\View(StatusCode = Response::HTTP_CREATED)
58 public function updatePersonAction(Request
$request)
60 $em = $this->getDoctrine()->getManager();
61 $person = $em->getRepository('App:Person')->find($request->get('id'));
64 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
67 $person->setFirstName($request->get('firstname'));
68 $person->setLastName($request->get('lastname'));
69 $person->setEmail($request->get('email'));
74 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
78 * @Rest\Get("/api/person/{id}/friends/localisation")
80 public function getFriendsLocalisationAction(Request
$request)
86 * @Rest\Get("/api/person/{id}/localisations")
88 public function getLocalisationsAction(Request
$request)
90 $em = $this->getDoctrine()->getManager();
91 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
93 if (empty($localisations)) {
94 return View
::create(['message' => 'Person localisations not found'], Response
::HTTP_NOT_FOUND
);
97 $view = View
::create($localisations);
98 $view->setFormat('json');
100 $viewHandler = $this->get('fos_rest.view_handler');
101 return $viewHandler->handle($view);
106 * @Rest\Post("/api/person/{id}/localisation")
107 * @Rest\View(StatusCode = Response::HTTP_CREATED)
109 public function updateLocalisationAction(Request
$request)
111 $localisation = new Localisation();
112 $localisation->setPerson($request->get('id'));
113 $localisation->setTimestamp($request->get('timestamp'));
114 $localisation->setLatitude($request->get('latitude'));
115 $localisation->setLongitude($request->get('longitude'));
117 $em = $this->getDoctrine()->getManager();
119 $em->persist($localisation);
125 * path = "/api/person/{id}",
126 * name = "show_person",
127 * requirements = {"id"="\d+"}
131 public function showPerson(Request
$request)
133 $em = $this->getDoctrine()->getManager();
134 $person = $em->getRepository('App:Person')->find($request->get('id'));
136 if (empty($person)) {
137 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
140 $view = View
::create($person);
141 $view->setFormat('json');
143 $viewHandler = $this->get('fos_rest.view_handler');
144 return $viewHandler->handle($view);
149 * path = "/api/person/{email}",
150 * name = "show_person_by_email",
151 * requirements = {"email"="\s+"}
155 public function showPersonByEmail(Request
$request)
157 $em = $this->getDoctrine()->getManager();
158 $person = $em->getRepository('App:Person')->find($request->get('email'));
160 if (empty($person)) {
161 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
164 $view = View
::create($person);
165 $view->setFormat('json');
167 $viewHandler = $this->get('fos_rest.view_handler');
168 return $viewHandler->handle($view);
173 * path = "/api/person/{id}/friends",
174 * name = "show_person_friends",
175 * requirements = {"id"="\d+"}
179 public function showPersonFriends(Request
$request)
181 $em = $this->getDoctrine()->getManager();
182 $person = $em->getRepository('App:Person')->find($request->get('id'));
184 if (empty($person)) {
185 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
188 $view = View
::create($person->getFriends());
189 $view->setFormat('json');
191 $viewHandler = $this->get('fos_rest.view_handler');
192 return $viewHandler->handle($view);
197 * path = "/api/person/{email}/friends",
198 * name = "show_person_friends_by_email",
199 * requirements = {"email"="\s+"}
203 public function showPersonFriendsByEmail(Request
$request)
205 $em = $this->getDoctrine()->getManager();
206 $person = $em->getRepository('App:Person')->find($request->get('email'));
208 if (empty($person)) {
209 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
212 $view = View
::create($person->getFriends());
213 $view->setFormat('json');
215 $viewHandler = $this->get('fos_rest.view_handler');
216 return $viewHandler->handle($view);