From 787fc3b74e498cb78f516e372827790ba78920ac Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 28 Jun 2018 21:44:53 +0200 Subject: [PATCH] Fix the REST ressource for getting the latest localisation of a user. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- config/packages/fos_rest.yaml | 2 ++ src/Controller/PersonController.php | 5 ++++- tests/curl.txt | 6 +++--- 3 files changed, 9 insertions(+), 4 deletions(-) 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 -- 2.34.1