Commit | Line | Data |
---|---|---|
dec6d031 JB |
1 | <?php |
2 | namespace App\Controller; | |
3 | ||
4 | use App\Entity\Person; | |
3d47ccc8 JB |
5 | use App\Entity\Localisation; |
6 | use \Datetime; | |
1d9d8d51 | 7 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
f0640a52 | 8 | use FOS\RestBundle\Controller\FOSRestController; |
dec6d031 | 9 | use FOS\RestBundle\Controller\Annotations as Rest; |
5347d06b JB |
10 | use FOS\RestBundle\View\ViewHandler; |
11 | use FOS\RestBundle\View\View; | |
dec6d031 JB |
12 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
13 | use Symfony\Component\HttpFoundation\Request; | |
14 | use Symfony\Component\HttpFoundation\Response; | |
15 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
16 | ||
f0640a52 | 17 | class PersonController extends FOSRestController |
dec6d031 JB |
18 | { |
19 | /** | |
20 | * @Rest\Post( | |
f0640a52 | 21 | * path = "/api/person/register", |
dec6d031 JB |
22 | * name = "create_person" |
23 | * ) | |
24 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
dec6d031 | 25 | */ |
f0640a52 | 26 | public function createPersonAction(Request $request) |
3d47ccc8 JB |
27 | { |
28 | $person = new Person(); | |
f0640a52 JB |
29 | $person->setFirstname($request->get('firstname')); |
30 | $person->setLastName($request->get('lastname')); | |
31 | $person->setEmail($request->get('email')); | |
32 | $person->setPassword($request->get('password')); | |
33 | $person->setOnline(false); | |
34 | ||
dec6d031 JB |
35 | $em = $this->getDoctrine()->getManager(); |
36 | ||
37 | $em->persist($person); | |
38 | $em->flush(); | |
39 | ||
40 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); | |
41 | } | |
42 | ||
43 | /** | |
98f85207 | 44 | * @Rest\Delete("/api/person/{id}") |
dec6d031 JB |
45 | * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) |
46 | */ | |
47 | public function removePersonAction(Request $request) | |
48 | { | |
3d47ccc8 | 49 | //TODO: check that the authenticated user have the same id |
dec6d031 | 50 | $em = $this->getDoctrine()->getManager(); |
282545e5 | 51 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
3d47ccc8 JB |
52 | $friends = $em->getRepository('App:Friendship')->findBy(['person' => $request->get('id')]); |
53 | $friends_with_me = $em->getRepository('App:Friendship')->findBy(['friend' => $request->get('id')]); | |
54 | $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); | |
dec6d031 | 55 | |
3d47ccc8 JB |
56 | if (!empty($localisations)) { |
57 | foreach ($localisations as $localisation) { | |
58 | $em->remove($localisation); | |
59 | } | |
60 | $em->flush(); | |
61 | } | |
62 | ||
63 | if (!empty($friends)) { | |
64 | foreach ($friends as $friend) { | |
65 | $em->remove($friend); | |
66 | } | |
67 | $em->flush(); | |
68 | } | |
69 | ||
70 | if (!empty($friends_with_me)) { | |
71 | foreach ($friends_with_me as $friend) { | |
72 | $em->remove($friend); | |
73 | } | |
dec6d031 JB |
74 | $em->flush(); |
75 | } | |
3d47ccc8 JB |
76 | |
77 | if (!empty($person)) { | |
78 | $em->remove($person); | |
79 | $em->flush(); | |
80 | } | |
dec6d031 JB |
81 | } |
82 | ||
83 | /** | |
84 | * @Rest\Put( | |
3d47ccc8 | 85 | * path = "/api/person/{id}", |
dec6d031 JB |
86 | * name = "update_person" |
87 | * ) | |
88 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
89 | */ | |
90 | public function updatePersonAction(Request $request) | |
91 | { | |
3d47ccc8 | 92 | //TODO: check that the authenticated user have the same id |
dec6d031 | 93 | $em = $this->getDoctrine()->getManager(); |
282545e5 | 94 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
dec6d031 | 95 | |
51963d7f | 96 | if (empty($person)) { |
1faa29dc | 97 | return $this->PersonNotFound(); |
dec6d031 JB |
98 | } |
99 | ||
100 | $person->setFirstName($request->get('firstname')); | |
101 | $person->setLastName($request->get('lastname')); | |
102 | $person->setEmail($request->get('email')); | |
103 | ||
104 | $em->merge($person); | |
105 | $em->flush(); | |
106 | ||
107 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); | |
108 | } | |
109 | ||
110 | /** | |
84fd6c7f JB |
111 | * @Rest\Get("/api/person/{id}/localisations") |
112 | * @Rest\View() | |
dec6d031 | 113 | */ |
84fd6c7f | 114 | public function getLocalisationsAction(Request $request) |
dec6d031 | 115 | { |
f0640a52 | 116 | //TODO: Check that the authenticated user is allowed to see the localisation |
84fd6c7f JB |
117 | $em = $this->getDoctrine()->getManager(); |
118 | $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); | |
dec6d031 | 119 | |
84fd6c7f JB |
120 | if (empty($localisations)) { |
121 | return $this->PersonLocalisationsNotFound(); | |
122 | } | |
123 | ||
124 | return $localisations; | |
dec6d031 JB |
125 | } |
126 | ||
4c4feb3e | 127 | /** |
84fd6c7f JB |
128 | * @Rest\Get("/api/person/{id}/localisation") |
129 | * @Rest\View() | |
4c4feb3e | 130 | */ |
84fd6c7f | 131 | public function getLocalisationAction(Request $request) |
4c4feb3e | 132 | { |
f0640a52 | 133 | //TODO: Check that the authenticated user is allowed to see the localisation |
4c4feb3e | 134 | $em = $this->getDoctrine()->getManager(); |
787fc3b7 JB |
135 | |
136 | $query = $em->createQuery("SELECT l1 FROM App\Entity\Localisation l1 WHERE l1.person = :person and l1.timestamp = (SELECT MAX(l2.timestamp) FROM App\Entity\Localisation l2 WHERE l2.person = l1.person)"); | |
137 | $query->setParameter('person', $request->get('id')); | |
138 | $localisation = $query->getResult(); | |
4c4feb3e | 139 | |
f0640a52 | 140 | if (empty($localisation)) { |
84fd6c7f | 141 | return $this->PersonLocalisationNotFound(); |
4c4feb3e JB |
142 | } |
143 | ||
f0640a52 | 144 | return $localisation; |
4c4feb3e JB |
145 | } |
146 | ||
dec6d031 | 147 | /** |
98f85207 | 148 | * @Rest\Post("/api/person/{id}/localisation") |
43f58db4 | 149 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
dec6d031 JB |
150 | */ |
151 | public function updateLocalisationAction(Request $request) | |
152 | { | |
3d47ccc8 JB |
153 | //TODO: Check that the authenticated user is allowed to update the localisation |
154 | $em = $this->getDoctrine()->getManager(); | |
155 | ||
156 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
157 | ||
158 | if (empty($person)) { | |
159 | return $this->PersonNotFound(); | |
160 | } | |
161 | ||
162 | $datetime = new DateTime($request->get('timestamp')); | |
163 | ||
dec6d031 | 164 | $localisation = new Localisation(); |
3d47ccc8 JB |
165 | $localisation->setPerson($person); |
166 | $localisation->setTimestamp($datetime); | |
dec6d031 JB |
167 | $localisation->setLatitude($request->get('latitude')); |
168 | $localisation->setLongitude($request->get('longitude')); | |
169 | ||
dec6d031 JB |
170 | $em->persist($localisation); |
171 | $em->flush(); | |
172 | } | |
173 | ||
174 | /** | |
f0640a52 JB |
175 | * @Rest\Get( |
176 | * path = "/api/person/{id}", | |
177 | * name = "show_person", | |
178 | * requirements = {"id"="\d+"} | |
179 | * ) | |
180 | * @Rest\View() | |
181 | */ | |
182 | public function showPerson(Request $request) | |
183 | { | |
5347d06b JB |
184 | $em = $this->getDoctrine()->getManager(); |
185 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
51963d7f | 186 | |
5347d06b | 187 | if (empty($person)) { |
1faa29dc | 188 | return $this->PersonNotFound(); |
51963d7f JB |
189 | } |
190 | ||
1faa29dc | 191 | return $person; |
f0640a52 JB |
192 | } |
193 | ||
f0640a52 JB |
194 | /** |
195 | * @Rest\Get( | |
196 | * path = "/api/person/{id}/friends", | |
197 | * name = "show_person_friends", | |
198 | * requirements = {"id"="\d+"} | |
199 | * ) | |
200 | * @Rest\View() | |
201 | */ | |
202 | public function showPersonFriends(Request $request) | |
203 | { | |
5347d06b JB |
204 | $em = $this->getDoctrine()->getManager(); |
205 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
206 | ||
207 | if (empty($person)) { | |
1faa29dc | 208 | return $this->PersonNotFound(); |
5347d06b | 209 | } |
51963d7f | 210 | |
1faa29dc | 211 | return $person->getFriends(); |
f0640a52 JB |
212 | } |
213 | ||
214 | /** | |
215 | * @Rest\Get( | |
216 | * path = "/api/persons", | |
217 | * name = "show_persons" | |
218 | * ) | |
219 | * @Rest\View() | |
220 | */ | |
221 | public function showPersons(Request $request) | |
222 | { | |
223 | $em = $this->getDoctrine()->getManager(); | |
224 | $persons = $em->getRepository('App:Person')->findAll(); | |
225 | ||
226 | if (empty($persons)) { | |
84fd6c7f | 227 | return $this->PersonsNotFound(); |
f0640a52 JB |
228 | } |
229 | ||
230 | return $persons; | |
231 | } | |
232 | ||
8629835e JB |
233 | /** |
234 | * @Rest\Put( | |
235 | * path = "/api/person/{id}/online", | |
236 | * name = "set_person_online" | |
237 | * ) | |
238 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
239 | */ | |
240 | public function onlinePersonAction(Request $request) | |
241 | { | |
242 | //TODO: check that the authenticated user have the same id | |
243 | $em = $this->getDoctrine()->getManager(); | |
244 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
245 | ||
246 | if (empty($person)) { | |
247 | return $this->PersonNotFound(); | |
248 | } | |
249 | ||
250 | $person->setOnline(true); | |
251 | ||
252 | $em->merge($person); | |
253 | $em->flush(); | |
254 | } | |
255 | ||
256 | /** | |
257 | * @Rest\Put( | |
258 | * path = "/api/person/{id}/offline", | |
259 | * name = "set_person_offline" | |
260 | * ) | |
261 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
262 | */ | |
263 | public function offlinePersonAction(Request $request) | |
264 | { | |
265 | //TODO: check that the authenticated user have the same id | |
266 | $em = $this->getDoctrine()->getManager(); | |
267 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
268 | ||
269 | if (empty($person)) { | |
270 | return $this->PersonNotFound(); | |
271 | } | |
272 | ||
273 | $person->setOnline(false); | |
274 | ||
275 | $em->merge($person); | |
276 | $em->flush(); | |
277 | } | |
278 | ||
f0640a52 JB |
279 | private function PersonNotFound() { |
280 | return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
281 | } | |
b6077893 | 282 | |
f0640a52 JB |
283 | private function PersonsNotFound() { |
284 | return View::create(['message' => 'Persons not found'], Response::HTTP_NOT_FOUND); | |
285 | } | |
84fd6c7f | 286 | |
f0640a52 JB |
287 | private function PersonLocalisationNotFound() { |
288 | return View::create(['message' => 'Person localisation not found'], Response::HTTP_NOT_FOUND); | |
289 | } | |
84fd6c7f | 290 | |
f0640a52 JB |
291 | private function PersonLocalisationsNotFound() { |
292 | return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND); | |
293 | } | |
b6077893 | 294 | |
dec6d031 | 295 | } |