2 namespace App\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
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 FOSRestController
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 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("/api/person/{id}/friends/localisation")
79 public function getFriendsLocalisationAction(Request
$request)
85 * @Rest\Post("/api/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 = "/api/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
);
119 $view = View
::create($person);
120 $view->setFormat('json');
127 * path = "/api/person/{email}",
128 * name = "show_person_by_email",
129 * requirements = {"email"="\s+"}
133 public function showPersonByEmail(Request
$request)
135 $em = $this->getDoctrine()->getManager();
136 $person = $em->getRepository('App:Person')->find($request->get('email'));
138 if (empty($person)) {
139 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
142 $view = View
::create($person);
143 $view->setFormat('json');
150 * path = "/api/person/{id}/friends",
151 * name = "show_person_friends",
152 * requirements = {"id"="\d+"}
156 public function showPersonFriends(Request
$request)
158 $em = $this->getDoctrine()->getManager();
159 $person = $em->getRepository('App:Person')->find($request->get('id'));
161 if (empty($person)) {
162 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
165 $view = View
::create($person->getFriends());
166 $view->setFormat('json');
173 * path = "/api/person/{email}/friends",
174 * name = "show_person_friends_by_email",
175 * requirements = {"email"="\s+"}
179 public function showPersonFriendsByEmail(Request
$request)
181 $em = $this->getDoctrine()->getManager();
182 $person = $em->getRepository('App:Person')->find($request->get('email'));
184 if (empty($person)) {
185 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
188 $view = View
::create($person->getFriends());
189 $view->setFormat('json');