Add REST ressources for onlining or offlining a user.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
index bc34bcd229ec24e6c02ceadddb6e83478b18d8e2..0430370f17f0b4380d0c03ed62fdffdf97e23be3 100644 (file)
@@ -2,6 +2,8 @@
 namespace App\Controller;
 
 use App\Entity\Person;
+use App\Entity\Localisation;
+use \Datetime;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use FOS\RestBundle\Controller\FOSRestController;
 use FOS\RestBundle\Controller\Annotations as Rest;
@@ -22,7 +24,8 @@ class PersonController extends FOSRestController
      * @Rest\View(StatusCode = Response::HTTP_CREATED)
      */
     public function createPersonAction(Request $request)
-    {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   $person = new Person();
+    {
+        $person = new Person();
         $person->setFirstname($request->get('firstname'));
         $person->setLastName($request->get('lastname'));
         $person->setEmail($request->get('email'));
@@ -43,25 +46,50 @@ class PersonController extends FOSRestController
      */
     public function removePersonAction(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'));
+        $friends = $em->getRepository('App:Friendship')->findBy(['person' => $request->get('id')]);
+        $friends_with_me = $em->getRepository('App:Friendship')->findBy(['friend' => $request->get('id')]);
+        $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
 
-        if (!empty($person)) {
-            $em->remove($person);
+        if (!empty($localisations)) {
+            foreach ($localisations as $localisation) {
+                $em->remove($localisation);
+            }
+            $em->flush();
+        }
+
+        if (!empty($friends)) {
+            foreach ($friends as $friend) {
+                $em->remove($friend);
+            }
+            $em->flush();
+        }
+
+        if (!empty($friends_with_me)) {
+            foreach ($friends_with_me as $friend) {
+                $em->remove($friend);
+            }
             $em->flush();
         }
-        //TODO: remove localisation and friendship
+
+        if (!empty($person)) {
+             $em->remove($person);
+             $em->flush();
+        }
     }
 
     /**
      * @Rest\Put(
-     *     path = "/api/person/{id}/update",
+     *     path = "/api/person/{id}",
      *     name = "update_person"
      * )
      * @Rest\View(StatusCode = Response::HTTP_CREATED)
      */
     public function updatePersonAction(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'));
 
@@ -119,14 +147,23 @@ class PersonController extends FOSRestController
      */
     public function updateLocalisationAction(Request $request)
     {
+        //TODO: Check that the authenticated user is allowed to update the localisation
+        $em = $this->getDoctrine()->getManager();
+
+        $person = $em->getRepository('App:Person')->find($request->get('id'));
+
+        if (empty($person)) {
+            return $this->PersonNotFound();
+        }
+
+        $datetime = new DateTime($request->get('timestamp'));
+
         $localisation = new Localisation();
-        $localisation->setPerson($request->get('id'));
-        $localisation->setTimestamp($request->get('timestamp'));
+        $localisation->setPerson($person);
+        $localisation->setTimestamp($datetime);
         $localisation->setLatitude($request->get('latitude'));
         $localisation->setLongitude($request->get('longitude'));
 
-        $em = $this->getDoctrine()->getManager();
-
         $em->persist($localisation);
         $em->flush();
     }
@@ -151,26 +188,6 @@ class PersonController extends FOSRestController
         return $person;
     }
 
-    /**
-     * @Rest\Get(
-     *     path = "/api/person/{email}",
-     *     name = "show_person_by_email",
-     *     requirements = {"email"="\s+"}
-     * )
-     * @Rest\View()
-     */
-    public function showPersonByEmail(Request $request)
-    {
-        $em = $this->getDoctrine()->getManager();
-        $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
-
-        if (empty($person)) {
-            return $this->PersonNotFound();
-        }
-
-        return $person;
-    }
-
     /**
      * @Rest\Get(
      *     path = "/api/person/{id}/friends",
@@ -211,23 +228,49 @@ class PersonController extends FOSRestController
     }
 
     /**
-     * @Rest\Get(
-     *     path = "/api/person/{email}/friends",
-     *     name = "show_person_friends_by_email",
-     *     requirements = {"email"="\s+"}
+     * @Rest\Put(
+     *     path = "/api/person/{id}/online",
+     *     name = "set_person_online"
      * )
-     * @Rest\View()
+     * @Rest\View(StatusCode = Response::HTTP_CREATED)
      */
-    public function showPersonFriendsByEmail(Request $request)
+    public function onlinePersonAction(Request $request)
     {
+        //TODO: check that the authenticated user have the same id
         $em = $this->getDoctrine()->getManager();
-        $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
+        $person = $em->getRepository('App:Person')->find($request->get('id'));
 
         if (empty($person)) {
             return $this->PersonNotFound();
         }
 
-        return $person->getFriends();
+        $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() {