2 namespace App\Controller
;
5 use App\Entity\Localisation
;
7 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
8 use FOS\RestBundle\Controller\FOSRestController
;
9 use FOS\RestBundle\Controller\Annotations
as Rest
;
10 use FOS\RestBundle\View\ViewHandler
;
11 use FOS\RestBundle\View\View
;
12 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
13 use Symfony\Component\HttpFoundation\Request
;
14 use Symfony\Component\HttpFoundation\Response
;
15 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
17 class PersonController
extends FOSRestController
21 * path = "/api/person/register",
22 * name = "create_person"
24 * @Rest\View(StatusCode = Response::HTTP_CREATED)
26 public function createPersonAction(Request
$request)
28 $person = new Person();
29 $person->setFirstname($request->get('firstname'));
30 $person->setLastName($request->get('lastname'));
31 $person->setEmail($request->get('email'));
32 $person->setPassword($request->get('password'));
33 $person->setOnline(false);
35 $em = $this->getDoctrine()->getManager();
37 $em->persist($person);
40 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
44 * @Rest\Delete("/api/person/{id}")
45 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
47 public function removePersonAction(Request
$request)
49 //TODO: check that the authenticated user have the same id
50 $em = $this->getDoctrine()->getManager();
51 $person = $em->getRepository('App:Person')->find($request->get('id'));
52 $friends = $em->getRepository('App:Friendship')->findBy(['person' => $request->get('id')]);
53 $friends_with_me = $em->getRepository('App:Friendship')->findBy(['friend' => $request->get('id')]);
54 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
56 if (!empty($localisations)) {
57 foreach ($localisations as $localisation) {
58 $em->remove($localisation);
63 if (!empty($friends)) {
64 foreach ($friends as $friend) {
70 if (!empty($friends_with_me)) {
71 foreach ($friends_with_me as $friend) {
77 if (!empty($person)) {
85 * path = "/api/person/{id}",
86 * name = "update_person"
88 * @Rest\View(StatusCode = Response::HTTP_CREATED)
90 public function updatePersonAction(Request
$request)
92 //TODO: check that the authenticated user have the same id
93 $em = $this->getDoctrine()->getManager();
94 $person = $em->getRepository('App:Person')->find($request->get('id'));
97 return $this->PersonNotFound();
100 $person->setFirstName($request->get('firstname'));
101 $person->setLastName($request->get('lastname'));
102 $person->setEmail($request->get('email'));
107 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
111 * @Rest\Get("/api/person/{id}/localisations")
114 public function getLocalisationsAction(Request
$request)
116 //TODO: Check that the authenticated user is allowed to see the localisation
117 $em = $this->getDoctrine()->getManager();
118 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
120 if (empty($localisations)) {
121 return $this->PersonLocalisationsNotFound();
124 return $localisations;
128 * @Rest\Get("/api/person/{id}/localisation")
131 public function getLocalisationAction(Request
$request)
133 //TODO: Check that the authenticated user is allowed to see the localisation
134 $em = $this->getDoctrine()->getManager();
136 $query = $em->createQuery("SELECT l1 FROM App\Entity\Localisation l1 WHERE l1.person = :person and l1.timestamp = (SELECT MAX(l2.timestamp) FROM App\Entity\Localisation l2 WHERE l2.person = l1.person)");
137 $query->setParameter('person', $request->get('id'));
138 $localisation = $query->getResult();
140 if (empty($localisation)) {
141 return $this->PersonLocalisationNotFound();
144 return $localisation;
148 * @Rest\Post("/api/person/{id}/localisation")
149 * @Rest\View(StatusCode = Response::HTTP_CREATED)
151 public function updateLocalisationAction(Request
$request)
153 //TODO: Check that the authenticated user is allowed to update the localisation
154 $em = $this->getDoctrine()->getManager();
156 $person = $em->getRepository('App:Person')->find($request->get('id'));
158 if (empty($person)) {
159 return $this->PersonNotFound();
162 $datetime = new DateTime($request->get('timestamp'));
164 $localisation = new Localisation();
165 $localisation->setPerson($person);
166 $localisation->setTimestamp($datetime);
167 $localisation->setLatitude($request->get('latitude'));
168 $localisation->setLongitude($request->get('longitude'));
170 $em->persist($localisation);
176 * path = "/api/person/{id}",
177 * name = "show_person",
178 * requirements = {"id"="\d+"}
182 public function showPerson(Request
$request)
184 $em = $this->getDoctrine()->getManager();
185 $person = $em->getRepository('App:Person')->find($request->get('id'));
187 if (empty($person)) {
188 return $this->PersonNotFound();
196 * path = "/api/person/{id}/friends",
197 * name = "show_person_friends",
198 * requirements = {"id"="\d+"}
202 public function showPersonFriends(Request
$request)
204 $em = $this->getDoctrine()->getManager();
205 $person = $em->getRepository('App:Person')->find($request->get('id'));
207 if (empty($person)) {
208 return $this->PersonNotFound();
211 return $person->getFriends();
216 * path = "/api/persons",
217 * name = "show_persons"
221 public function showPersons(Request
$request)
223 $em = $this->getDoctrine()->getManager();
224 $persons = $em->getRepository('App:Person')->findAll();
226 if (empty($persons)) {
227 return $this->PersonsNotFound();
235 * path = "/api/person/{id}/online",
236 * name = "set_person_online"
238 * @Rest\View(StatusCode = Response::HTTP_CREATED)
240 public function onlinePersonAction(Request
$request)
242 //TODO: check that the authenticated user have the same id
243 $em = $this->getDoctrine()->getManager();
244 $person = $em->getRepository('App:Person')->find($request->get('id'));
246 if (empty($person)) {
247 return $this->PersonNotFound();
250 $person->setOnline(true);
258 * path = "/api/person/{id}/offline",
259 * name = "set_person_offline"
261 * @Rest\View(StatusCode = Response::HTTP_CREATED)
263 public function offlinePersonAction(Request
$request)
265 //TODO: check that the authenticated user have the same id
266 $em = $this->getDoctrine()->getManager();
267 $person = $em->getRepository('App:Person')->find($request->get('id'));
269 if (empty($person)) {
270 return $this->PersonNotFound();
273 $person->setOnline(false);
279 private function PersonNotFound() {
280 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
283 private function PersonsNotFound() {
284 return View
::create(['message' => 'Persons not found'], Response
::HTTP_NOT_FOUND
);
287 private function PersonLocalisationNotFound() {
288 return View
::create(['message' => 'Person localisation not found'], Response
::HTTP_NOT_FOUND
);
291 private function PersonLocalisationsNotFound() {
292 return View
::create(['message' => 'Person localisations not found'], Response
::HTTP_NOT_FOUND
);