From 8629835e06e50277f8868c19ea93f41313c16586 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 28 Jun 2018 20:01:51 +0200 Subject: [PATCH] Add REST ressources for onlining or offlining a user. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Controller/PersonController.php | 46 +++++++++++++++++++++++++++++ tests/curl.txt | 6 +++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index 9ef7469..0430370 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -227,6 +227,52 @@ class PersonController extends FOSRestController return $persons; } + /** + * @Rest\Put( + * path = "/api/person/{id}/online", + * name = "set_person_online" + * ) + * @Rest\View(StatusCode = Response::HTTP_CREATED) + */ + public function onlinePersonAction(Request $request) + { + //TODO: check that the authenticated user have the same id + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('id')); + + if (empty($person)) { + return $this->PersonNotFound(); + } + + $person->setOnline(true); + + $em->merge($person); + $em->flush(); + } + + /** + * @Rest\Put( + * path = "/api/person/{id}/offline", + * name = "set_person_offline" + * ) + * @Rest\View(StatusCode = Response::HTTP_CREATED) + */ + public function offlinePersonAction(Request $request) + { + //TODO: check that the authenticated user have the same id + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('id')); + + if (empty($person)) { + return $this->PersonNotFound(); + } + + $person->setOnline(false); + + $em->merge($person); + $em->flush(); + } + private function PersonNotFound() { return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } diff --git a/tests/curl.txt b/tests/curl.txt index aa80b3a..3798713 100644 --- a/tests/curl.txt +++ b/tests/curl.txt @@ -7,9 +7,13 @@ 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" -* Update a user localisation with id 1: +* 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" +* Update a user online status: +curl --request PUT http://localhost:8000/api/person/1/online +curl --request PUT http://localhost:8000/api/person/1/offline + * Show a user with id 1: curl --request GET http://localhost:8000/api/person/1 -- 2.34.1