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 FOSRestController
19 * path = "/api/person/register",
20 * name = "create_person"
22 * @Rest\View(StatusCode = Response::HTTP_CREATED)
24 public function createPersonAction(Request
$request)
25 { $person = new Person();
26 $person->setFirstname($request->get('firstname'));
27 $person->setLastName($request->get('lastname'));
28 $person->setEmail($request->get('email'));
29 $person->setPassword($request->get('password'));
30 $person->setOnline(false);
32 $em = $this->getDoctrine()->getManager();
34 $em->persist($person);
37 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
41 * @Rest\Delete("/api/person/{id}")
42 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
44 public function removePersonAction(Request
$request)
46 $em = $this->getDoctrine()->getManager();
47 $person = $em->getRepository('App:Person')->find($request->get('id'));
49 if (!empty($person)) {
53 //TODO: remove localisation and friendship
58 * path = "/api/person/{id}/update",
59 * name = "update_person"
61 * @Rest\View(StatusCode = Response::HTTP_CREATED)
63 public function updatePersonAction(Request
$request)
65 $em = $this->getDoctrine()->getManager();
66 $person = $em->getRepository('App:Person')->find($request->get('id'));
69 return $this->PersonNotFound();
72 $person->setFirstName($request->get('firstname'));
73 $person->setLastName($request->get('lastname'));
74 $person->setEmail($request->get('email'));
79 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
83 * @Rest\Get("/api/person/{id}/localisations")
86 public function getLocalisationsAction(Request
$request)
88 //TODO: Check that the authenticated user is allowed to see the localisation
89 $em = $this->getDoctrine()->getManager();
90 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
92 if (empty($localisations)) {
93 return $this->PersonLocalisationsNotFound();
96 return $localisations;
100 * @Rest\Get("/api/person/{id}/localisation")
103 public function getLocalisationAction(Request
$request)
105 //TODO: Check that the authenticated user is allowed to see the localisation
106 $em = $this->getDoctrine()->getManager();
107 $localisation = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]);
109 if (empty($localisation)) {
110 return $this->PersonLocalisationNotFound();
113 return $localisation;
117 * @Rest\Post("/api/person/{id}/localisation")
118 * @Rest\View(StatusCode = Response::HTTP_CREATED)
120 public function updateLocalisationAction(Request
$request)
122 $localisation = new Localisation();
123 $localisation->setPerson($request->get('id'));
124 $localisation->setTimestamp($request->get('timestamp'));
125 $localisation->setLatitude($request->get('latitude'));
126 $localisation->setLongitude($request->get('longitude'));
128 $em = $this->getDoctrine()->getManager();
130 $em->persist($localisation);
136 * path = "/api/person/{id}",
137 * name = "show_person",
138 * requirements = {"id"="\d+"}
142 public function showPerson(Request
$request)
144 $em = $this->getDoctrine()->getManager();
145 $person = $em->getRepository('App:Person')->find($request->get('id'));
147 if (empty($person)) {
148 return $this->PersonNotFound();
156 * path = "/api/person/{email}",
157 * name = "show_person_by_email",
158 * requirements = {"email"="\s+"}
162 public function showPersonByEmail(Request
$request)
164 $em = $this->getDoctrine()->getManager();
165 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
167 if (empty($person)) {
168 return $this->PersonNotFound();
176 * path = "/api/person/{id}/friends",
177 * name = "show_person_friends",
178 * requirements = {"id"="\d+"}
182 public function showPersonFriends(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();
191 return $person->getFriends();
196 * path = "/api/persons",
197 * name = "show_persons"
201 public function showPersons(Request
$request)
203 $em = $this->getDoctrine()->getManager();
204 $persons = $em->getRepository('App:Person')->findAll();
206 if (empty($persons)) {
207 return $this->PersonsNotFound();
215 * path = "/api/person/{email}/friends",
216 * name = "show_person_friends_by_email",
217 * requirements = {"email"="\s+"}
221 public function showPersonFriendsByEmail(Request
$request)
223 $em = $this->getDoctrine()->getManager();
224 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
226 if (empty($person)) {
227 return $this->PersonNotFound();
230 return $person->getFriends();
233 private function PersonNotFound() {
234 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
237 private function PersonsNotFound() {
238 return View
::create(['message' => 'Persons not found'], Response
::HTTP_NOT_FOUND
);
241 private function PersonLocalisationNotFound() {
242 return View
::create(['message' => 'Person localisation not found'], Response
::HTTP_NOT_FOUND
);
245 private function PersonLocalisationsNotFound() {
246 return View
::create(['message' => 'Person localisations not found'], Response
::HTTP_NOT_FOUND
);