Add REST ressources for onlining or offlining a user.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
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);
     }