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
;
13 * @Rest\Prefix("/api")
15 class PersonController
extends FOSRestController
19 * path = "/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 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
36 * @Rest\Delete("/person/{id}")
37 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
39 public function removePersonAction(Request
$request)
41 $em = $this->getDoctrine()->getManager();
42 $person = $em->getRepository('App::Person')->find($request->get('id'));
44 if (!empty($person)) {
52 * path = "/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 new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
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("/person/{id}/friends/localisation")
79 public function getFriendsLocalisationAction(Request
$request)
85 * @Rest\Post("/person/{id}/localisation")
86 * @Rest\View(StatusCode = Response::HTTP_CREATED)
88 public function updateLocalisationAction(Request
$request)
90 $localisation = new Localisation();
91 $localisation->setPerson($request->get('id'));
92 $localisation->setTimestamp($request->get('timestamp'));
93 $localisation->setLatitude($request->get('latitude'));
94 $localisation->setLongitude($request->get('longitude'));
96 $em = $this->getDoctrine()->getManager();
98 $em->persist($localisation);
104 * path = "/person/{id}",
105 * name = "show_person",
106 * requirements = {"id"="\d+"}
110 public function showPerson(Request
$request)
112 $em = $this->getDoctrine()->getManager();
113 $person = $em->getRepository('App::Person')->find($request->get('id'));
115 if (empty($person)) {
116 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
124 * path = "/person/{email}",
125 * name = "show_person_by_email",
126 * requirements = {"email"="\s+"}
130 public function showPersonByEmail(Request
$request)
132 $em = $this->getDoctrine()->getManager();
133 $person = $em->getRepository('App::Person')->find($request->get('email'));
135 if (empty($person)) {
136 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
144 * path = "/person/{id}/friends",
145 * name = "show_person_friends",
146 * requirements = {"id"="\d+"}
150 public function showPersonFriends(Request
$request)
152 $em = $this->getDoctrine()->getManager();
153 $person = $em->getRepository('App::Person')->find($request->get('id'));
155 if (empty($person)) {
156 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
159 return $person->getFriends();
164 * path = "/person/{email}/friends",
165 * name = "show_person_friends_by_email",
166 * requirements = {"email"="\s+"}
170 public function showPersonFriendsByEmail(Request
$request)
172 $em = $this->getDoctrine()->getManager();
173 $person = $em->getRepository('App::Person')->find($request->get('email'));
175 if (empty($person)) {
176 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
179 return $person->getFriends();