Add REST ressources for onlining or offlining a user.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 28 Jun 2018 18:01:51 +0000 (20:01 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 28 Jun 2018 18:01:51 +0000 (20:01 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Controller/PersonController.php
tests/curl.txt

index 9ef74692683fd17976ef9ed4619943d28ae63e6a..0430370f17f0b4380d0c03ed62fdffdf97e23be3 100644 (file)
@@ -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);
     }
index aa80b3a9ccbead7707dff6e056c322eb9d385b27..3798713872e33883cc4ba0bcd3ae7b3decd9bccb 100644 (file)
@@ -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