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 //TODO: use ViewHandler
33 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
37 * @Rest\Delete("/api/person/{id}")
38 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
40 public function removePersonAction(Request
$request)
42 $em = $this->getDoctrine()->getManager();
43 $person = $em->getRepository('App:Person')->find($request->get('id'));
45 if (!empty($person)) {
49 //TODO: remove localisation and friendship
54 * path = "/api/person/{id}/update",
55 * name = "update_person"
57 * @Rest\View(StatusCode = Response::HTTP_CREATED)
59 public function updatePersonAction(Request
$request)
61 $em = $this->getDoctrine()->getManager();
62 $person = $em->getRepository('App:Person')->find($request->get('id'));
64 $viewHandler = $this->get('fos_rest.view_handler');
67 return $this->PersonNotFound($viewHandler);
70 $person->setFirstName($request->get('firstname'));
71 $person->setLastName($request->get('lastname'));
72 $person->setEmail($request->get('email'));
77 //TODO: use ViewHandler
78 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
82 * @Rest\Get("/api/person/{id}/friends/localisation")
84 public function getFriendsLocalisationAction(Request
$request)
90 * @Rest\Get("/api/person/{id}/localisations")
92 public function getLocalisationsAction(Request
$request)
94 $em = $this->getDoctrine()->getManager();
95 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
97 $viewHandler = $this->get('fos_rest.view_handler');
99 if (empty($localisations)) {
100 return $this->PersonLocalisationsNotFound($viewHandler);
103 $view = View
::create($localisations);
104 $view->setFormat('json');
106 return $viewHandler->handle($view);
111 * @Rest\Post("/api/person/{id}/localisation")
112 * @Rest\View(StatusCode = Response::HTTP_CREATED)
114 public function updateLocalisationAction(Request
$request)
116 $localisation = new Localisation();
117 $localisation->setPerson($request->get('id'));
118 $localisation->setTimestamp($request->get('timestamp'));
119 $localisation->setLatitude($request->get('latitude'));
120 $localisation->setLongitude($request->get('longitude'));
122 $em = $this->getDoctrine()->getManager();
124 $em->persist($localisation);
130 * path = "/api/person/{id}",
131 * name = "show_person",
132 * requirements = {"id"="\d+"}
136 public function showPerson(Request
$request)
138 $em = $this->getDoctrine()->getManager();
139 $person = $em->getRepository('App:Person')->find($request->get('id'));
141 $viewHandler = $this->get('fos_rest.view_handler');
143 if (empty($person)) {
144 return $this->PersonNotFound($viewHandler);
147 $view = View
::create($person);
148 $view->setFormat('json');
150 return $viewHandler->handle($view);
155 * path = "/api/person/{email}",
156 * name = "show_person_by_email",
157 * requirements = {"email"="\s+"}
161 public function showPersonByEmail(Request
$request)
163 $em = $this->getDoctrine()->getManager();
164 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
166 $viewHandler = $this->get('fos_rest.view_handler');
168 if (empty($person)) {
169 return $this->PersonNotFound($viewHandler);
172 $view = View
::create($person);
173 $view->setFormat('json');
175 return $viewHandler->handle($view);
180 * path = "/api/person/{id}/friends",
181 * name = "show_person_friends",
182 * requirements = {"id"="\d+"}
186 public function showPersonFriends(Request
$request)
188 $em = $this->getDoctrine()->getManager();
189 $person = $em->getRepository('App:Person')->find($request->get('id'));
191 $viewHandler = $this->get('fos_rest.view_handler');
193 if (empty($person)) {
194 return $this->PersonNotFound($viewHandler);
197 $view = View
::create($person->getFriends());
198 $view->setFormat('json');
200 return $viewHandler->handle($view);
205 * path = "/api/person/{email}/friends",
206 * name = "show_person_friends_by_email",
207 * requirements = {"email"="\s+"}
211 public function showPersonFriendsByEmail(Request
$request)
213 $em = $this->getDoctrine()->getManager();
214 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
216 $viewHandler = $this->get('fos_rest.view_handler');
218 if (empty($person)) {
219 return $this->PersonNotFound($viewHandler);
222 $view = View
::create($person->getFriends());
223 $view->setFormat('json');
225 return $viewHandler->handle($view);
228 private function PersonNotFound($viewHandler) {
229 return $viewHandler->handle(View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
));
232 private function PersonLocalisationsNotFound($viewHandler) {
233 return $viewHandler->handle(View
::create(['message' => 'Person localisations not found'], Response
::HTTP_NOT_FOUND
));