From: Jérôme Benoit Date: Thu, 28 Jun 2018 19:44:53 +0000 (+0200) Subject: Fix the REST ressource for getting the latest localisation of a user. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=787fc3b74e498cb78f516e372827790ba78920ac;p=Project_proches_de_moi-server.git Fix the REST ressource for getting the latest localisation of a user. Signed-off-by: Jérôme Benoit --- diff --git a/config/packages/fos_rest.yaml b/config/packages/fos_rest.yaml index 7e7f74d..18a74b1 100644 --- a/config/packages/fos_rest.yaml +++ b/config/packages/fos_rest.yaml @@ -32,3 +32,5 @@ fos_rest: media_type: # Accept header enabled: true regex: '/(v|version)=(?P[0-9\.]+)/' + serializer: + serialize_null: true diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index 0430370..b037f53 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -132,7 +132,10 @@ class PersonController extends FOSRestController { //TODO: Check that the authenticated user is allowed to see the localisation $em = $this->getDoctrine()->getManager(); - $localisation = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]); + + $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)"); + $query->setParameter('person', $request->get('id')); + $localisation = $query->getResult(); if (empty($localisation)) { return $this->PersonLocalisationNotFound(); diff --git a/tests/curl.txt b/tests/curl.txt index 3798713..6f2d0ac 100644 --- a/tests/curl.txt +++ b/tests/curl.txt @@ -1,14 +1,14 @@ * Create a user: -curl --request POST http://localhost:8000/api/person/register --data "{ \"firstname\": \"James\", \"lastname\": \"Elbow\", \"email\": \"james@elbow.com\", \"password\": \"FD3CC7719AB1776B81D70B65170D8A51119E127488C106622E0D2E680C1B3FFF\"}" --header "Content-Type: application/json" +curl --request POST http://localhost:8000/api/person/register --data "{ \"firstname\": \"James\", \"lastname\": \"Elbow\", \"email\": \"james@elbow.com\", \"password\": \"FD3CC7719AB1776B81D70B65170D8A51119E127488C106622E0D2E680C1B3FFF\" }" --header "Content-Type: application/json" * Delete a user with id 1: curl --request DELETE http://localhost:8000/api/person/1 * Update a user with id 1: -curl --request PUT http://localhost:8000/api/person/1 --data "{ \"firstname\": \"Jamesson\", \"lastname\": \"Elbow\", \"email\": \"james@elbow.com\"}" --header "Content-Type: application/json" +curl --request PUT http://localhost:8000/api/person/1 --data "{ \"firstname\": \"Jamesson\", \"lastname\": \"Elbow\", \"email\": \"james@elbow.com\" }" --header "Content-Type: application/json" * Update/add a user with id 1 localisation: -curl --request POST http://localhost:8000/api/person/1/localisation --data "{ \"timestamp\": \"$(date --iso-8601=seconds)\", \"latitude\": \"43.23\", \"longitude\": \"5.43\"}" --header "Content-Type: application/json" +curl --request POST http://localhost:8000/api/person/1/localisation --data "{ \"timestamp\": \"$(date --iso-8601=seconds)\", \"latitude\": \"43.23\", \"longitude\": \"5.43\" }" --header "Content-Type: application/json" * Update a user online status: curl --request PUT http://localhost:8000/api/person/1/online