2 namespace App\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
6 use FOS\RestBundle\Controller\Annotations
as Rest
;
7 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
8 use Symfony\Component\HttpFoundation\Request
;
9 use Symfony\Component\HttpFoundation\Response
;
10 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
12 class PersonController
extends FOSRestController
16 * path = "/api/person/inscription",
17 * name = "create_person"
19 * @Rest\View(StatusCode = Response::HTTP_CREATED)
20 * @ParamConverter("person", converter="fos_rest.request_body")
22 public function createPersonAction(Person
$person)
24 $em = $this->getDoctrine()->getManager();
26 $em->persist($person);
29 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
33 * @Rest\Delete("/api/person/{id}")
34 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
36 public function removePersonAction(Request
$request)
38 $em = $this->getDoctrine()->getManager();
39 $person = $em->getRepository('App::Person')->find($request->get('id'));
41 if (!empty($person)) {
45 //TODO: remove localisation and friendship
50 * path = "/api/person/{id}/update",
51 * name = "update_person"
53 * @Rest\View(StatusCode = Response::HTTP_CREATED)
55 public function updatePersonAction(Request
$request)
57 $em = $this->getDoctrine()->getManager();
58 $person = $em->getRepository('App::Person')->find($request->get('id'));
61 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
64 $person->setFirstName($request->get('firstname'));
65 $person->setLastName($request->get('lastname'));
66 $person->setEmail($request->get('email'));
71 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
75 * @Rest\Get("/api/person/{id}/friends/localisation")
77 public function getFriendsLocalisationAction(Request
$request)
83 * @Rest\Post("/api/person/{id}/localisation")
84 * @Rest\View(StatusCode = Response::HTTP_CREATED)
86 public function updateLocalisationAction(Request
$request)
88 $localisation = new Localisation();
89 $localisation->setPerson($request->get('id'));
90 $localisation->setTimestamp($request->get('timestamp'));
91 $localisation->setLatitude($request->get('latitude'));
92 $localisation->setLongitude($request->get('longitude'));
94 $em = $this->getDoctrine()->getManager();
96 $em->persist($localisation);
103 * path = "/api/person/{id}",
104 * name = "show_person",
105 * requirements = {"id"="\d+"}
108 public function showPerson(Request
$request)
110 $em = $this->getDoctrine()->getManager();
111 $person = $em->getRepository('App:Person')->find($request->get('id'));
113 if (empty($person)) {
114 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
122 * path = "/api/person/{email}",
123 * name = "show_person_by_email",
124 * requirements = {"email"="\s+"}
128 public function showPersonByEmail(Request
$request)
130 $em = $this->getDoctrine()->getManager();
131 $person = $em->getRepository('App::Person')->find($request->get('email'));
133 if (empty($person)) {
134 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
142 * path = "/api/person/{id}/friends",
143 * name = "show_person_friends",
144 * requirements = {"id"="\d+"}
148 public function showPersonFriends(Request
$request)
150 $em = $this->getDoctrine()->getManager();
151 $person = $em->getRepository('App:Person')->find($request->get('id'));
153 if (empty($person)) {
154 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
157 return $person->getFriends();
162 * path = "/api/person/{email}/friends",
163 * name = "show_person_friends_by_email",
164 * requirements = {"email"="\s+"}
168 public function showPersonFriendsByEmail(Request
$request)
170 $em = $this->getDoctrine()->getManager();
171 $person = $em->getRepository('App::Person')->find($request->get('email'));
173 if (empty($person)) {
174 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
177 return $person->getFriends();