2 namespace App\Controller
;
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
6 use FOS\RestBundle\Controller\Annotations
as Rest
;
7 use FOS\RestBundle\View\ViewHandler
;
8 use FOS\RestBundle\View\View
;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
10 use Symfony\Component\HttpFoundation\Request
;
11 use Symfony\Component\HttpFoundation\Response
;
12 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
14 class PersonController
extends Controller
18 * path = "/api/person/inscription",
19 * name = "create_person"
21 * @Rest\View(StatusCode = Response::HTTP_CREATED)
22 * @ParamConverter("person", converter="fos_rest.request_body")
24 public function createPersonAction(Person
$person)
26 $em = $this->getDoctrine()->getManager();
28 $em->persist($person);
31 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
35 * @Rest\Delete("/api/person/{id}")
36 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
38 public function removePersonAction(Request
$request)
40 $em = $this->getDoctrine()->getManager();
41 $person = $em->getRepository('App:Person')->find($request->get('id'));
43 if (!empty($person)) {
47 //TODO: remove localisation and friendship
52 * path = "/api/person/{id}/update",
53 * name = "update_person"
55 * @Rest\View(StatusCode = Response::HTTP_CREATED)
57 public function updatePersonAction(Request
$request)
59 $em = $this->getDoctrine()->getManager();
60 $person = $em->getRepository('App:Person')->find($request->get('id'));
63 return $this->PersonNotFound();
66 $person->setFirstName($request->get('firstname'));
67 $person->setLastName($request->get('lastname'));
68 $person->setEmail($request->get('email'));
73 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
77 * @Rest\Get("/api/person/{id}/localisations")
80 public function getLocalisationsAction(Request
$request)
82 $em = $this->getDoctrine()->getManager();
83 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
85 if (empty($localisations)) {
86 return $this->PersonLocalisationsNotFound();
89 return $localisations;
93 * @Rest\Get("/api/person/{id}/localisation")
96 public function getLocalisationAction(Request
$request)
98 $em = $this->getDoctrine()->getManager();
99 $localisations = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]);
101 if (empty($localisations)) {
102 return $this->PersonLocalisationNotFound();
105 return $localisations;
109 * @Rest\Post("/api/person/{id}/localisation")
110 * @Rest\View(StatusCode = Response::HTTP_CREATED)
112 public function updateLocalisationAction(Request
$request)
114 $localisation = new Localisation();
115 $localisation->setPerson($request->get('id'));
116 $localisation->setTimestamp($request->get('timestamp'));
117 $localisation->setLatitude($request->get('latitude'));
118 $localisation->setLongitude($request->get('longitude'));
120 $em = $this->getDoctrine()->getManager();
122 $em->persist($localisation);
128 * path = "/api/person/{id}",
129 * name = "show_person",
130 * requirements = {"id"="\d+"}
134 public function showPerson(Request
$request)
136 $em = $this->getDoctrine()->getManager();
137 $person = $em->getRepository('App:Person')->find($request->get('id'));
139 if (empty($person)) {
140 return $this->PersonNotFound();
148 * path = "/api/person/{email}",
149 * name = "show_person_by_email",
150 * requirements = {"email"="\s+"}
154 public function showPersonByEmail(Request
$request)
156 $em = $this->getDoctrine()->getManager();
157 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
159 if (empty($person)) {
160 return $this->PersonNotFound();
168 * path = "/api/person/{id}/friends",
169 * name = "show_person_friends",
170 * requirements = {"id"="\d+"}
174 public function showPersonFriends(Request
$request)
176 $em = $this->getDoctrine()->getManager();
177 $person = $em->getRepository('App:Person')->find($request->get('id'));
179 if (empty($person)) {
180 return $this->PersonNotFound();
183 return $person->getFriends();
188 * path = "/api/persons",
189 * name = "show_persons"
193 public function showPersons(Request
$request)
195 $em = $this->getDoctrine()->getManager();
196 $persons = $em->getRepository('App:Person')->findAll();
198 if (empty($persons)) {
199 return $this->PersonsNotFound();
207 * path = "/api/person/{email}/friends",
208 * name = "show_person_friends_by_email",
209 * requirements = {"email"="\s+"}
213 public function showPersonFriendsByEmail(Request
$request)
215 $em = $this->getDoctrine()->getManager();
216 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
218 if (empty($person)) {
219 return $this->PersonNotFound();
222 return $person->getFriends();
225 private function PersonNotFound() {
226 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
229 private function PersonsNotFound() {
230 return View
::create(['message' => 'Persons not found'], Response
::HTTP_NOT_FOUND
);
233 private function PersonLocalisationNotFound() {
234 return View
::create(['message' => 'Person localisation not found'], Response
::HTTP_NOT_FOUND
);
237 private function PersonLocalisationsNotFound() {
238 return View
::create(['message' => 'Person localisations not found'], Response
::HTTP_NOT_FOUND
);