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 Controller
19 * path = "/api/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("/api/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)) {
48 //TODO: remove localisation and friendship
53 * path = "/api/person/{id}/update",
54 * name = "update_person"
56 * @Rest\View(StatusCode = Response::HTTP_CREATED)
58 public function updatePersonAction(Request
$request)
60 $em = $this->getDoctrine()->getManager();
61 $person = $em->getRepository('App:Person')->find($request->get('id'));
64 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
67 $person->setFirstName($request->get('firstname'));
68 $person->setLastName($request->get('lastname'));
69 $person->setEmail($request->get('email'));
74 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
78 * @Rest\Get("/api/person/{id}/friends/localisation")
80 public function getFriendsLocalisationAction(Request
$request)
86 * @Rest\Post("/api/person/{id}/localisation")
87 * @Rest\View(StatusCode = Response::HTTP_CREATED)
89 public function updateLocalisationAction(Request
$request)
91 $localisation = new Localisation();
92 $localisation->setPerson($request->get('id'));
93 $localisation->setTimestamp($request->get('timestamp'));
94 $localisation->setLatitude($request->get('latitude'));
95 $localisation->setLongitude($request->get('longitude'));
97 $em = $this->getDoctrine()->getManager();
99 $em->persist($localisation);
105 * path = "/api/person/{id}",
106 * name = "show_person",
107 * requirements = {"id"="\d+"}
111 public function showPerson(Request
$request)
113 $em = $this->getDoctrine()->getManager();
114 $person = $em->getRepository('App:Person')->find($request->get('id'));
116 if (empty($person)) {
117 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
120 $view = View
::create($person);
121 $view->setFormat('json');
128 * path = "/api/person/{email}",
129 * name = "show_person_by_email",
130 * requirements = {"email"="\s+"}
134 public function showPersonByEmail(Request
$request)
136 $em = $this->getDoctrine()->getManager();
137 $person = $em->getRepository('App:Person')->find($request->get('email'));
139 if (empty($person)) {
140 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
143 $view = View
::create($person);
144 $view->setFormat('json');
151 * path = "/api/person/{id}/friends",
152 * name = "show_person_friends",
153 * requirements = {"id"="\d+"}
157 public function showPersonFriends(Request
$request)
159 $em = $this->getDoctrine()->getManager();
160 $person = $em->getRepository('App:Person')->find($request->get('id'));
162 if (empty($person)) {
163 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
166 $view = View
::create($person->getFriends());
167 $view->setFormat('json');
174 * path = "/api/person/{email}/friends",
175 * name = "show_person_friends_by_email",
176 * requirements = {"email"="\s+"}
180 public function showPersonFriendsByEmail(Request
$request)
182 $em = $this->getDoctrine()->getManager();
183 $person = $em->getRepository('App:Person')->find($request->get('email'));
185 if (empty($person)) {
186 return View
::create(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
189 $view = View
::create($person->getFriends());
190 $view->setFormat('json');