Add new REST ressources.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
index 734f29c9eca165ade74de8773ffde5970a07cb1a..49c456d999609bbcb0a6f89cdf7b2eff89ea50b8 100644 (file)
@@ -2,7 +2,10 @@
 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;
 use FOS\RestBundle\View\ViewHandler;
 use FOS\RestBundle\View\View;
@@ -11,18 +14,25 @@ use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 
-class PersonController extends Controller
+class PersonController extends FOSRestController
 {
     /**
      * @Rest\Post(
-     *     path = "/api/person/inscription",
+     *     path = "/api/person/register",
      *     name = "create_person"
      * )
      * @Rest\View(StatusCode = Response::HTTP_CREATED)
-     * @ParamConverter("person", converter="fos_rest.request_body")
      */
-    public function createPersonAction(Person $person)
+    public function createPersonAction(Request $request)
     {
+        $person = new Person();
+        $person->setFirstname($request->get('firstname'));
+        $person->setLastName($request->get('lastname'));
+        //TODO: email creation should normally have a verification step
+        $person->setEmail($request->get('email'));
+        $person->setPassword($request->get('password'));
+        $person->setOnline(false);
+
         $em = $this->getDoctrine()->getManager();
 
         $em->persist($person);
@@ -37,25 +47,50 @@ class PersonController extends Controller
      */
     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'));
 
@@ -65,6 +100,7 @@ class PersonController extends Controller
 
         $person->setFirstName($request->get('firstname'));
         $person->setLastName($request->get('lastname'));
+        //TODO: email update should normally have a verification step
         $person->setEmail($request->get('email'));
 
         $em->merge($person);
@@ -73,12 +109,36 @@ class PersonController extends Controller
         return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
     }
 
+    /**
+     * @Rest\Post(
+     *     path = "/api/person/authenticate",
+     *     name = "authenticate_person"
+     * )
+     * @Rest\View(StatusCode = Response::HTTP_ACCEPTED)
+     */
+    public function authenticatePersonAction(Request $request)
+    {
+        $em = $this->getDoctrine()->getManager();
+        $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
+
+        if (empty($person)) {
+            return $this->PersonNotFound();
+        }
+
+        if ($request->get('password') != $person->getPassword()) {
+            return $this->PersonWrongPassword();
+        } else {
+            return $this->view($person, Response::HTTP_ACCEPTED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
+        }
+    }
+
     /**
      * @Rest\Get("/api/person/{id}/localisations")
      * @Rest\View()
      */
     public function getLocalisationsAction(Request $request)
     {
+        //TODO: Check that the authenticated user is allowed to see the localisation
         $em = $this->getDoctrine()->getManager();
         $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
 
@@ -89,20 +149,110 @@ class PersonController extends Controller
         return $localisations;
     }
 
+    /**
+     * @Rest\Get(
+     *     path = "/api/person/{id}/localisations/fuzzy/{distance}",
+     *     name = "person_localisations_fuzzy",
+     *     requirements = {"id"="\d+", "distance"="\d+"}
+     * )
+     * @Rest\View()
+     */
+    public function getLocalisationsFuzzyAction(Request $request)
+    {
+        //TODO: Check that the authenticated user is allowed to see the localisation
+        $em = $this->getDoctrine()->getManager();
+        $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
+
+        if (empty($localisations)) {
+            return $this->PersonLocalisationsNotFound();
+        }
+
+        if (!$this->chk_distance($request->get('distance'), 200, 500)) {
+            return $this->PersonLocalisationFuzzyWrongDistance();
+        }
+
+        $fuzzy_localisations = array_map(function($item) use ($request) { return $this->randomizeLocation($item, $request->get('distance'), 200, 500); }, $localisations);
+
+        return $fuzzy_localisations;
+    }
+
+    private function getLastLocalisation($em, $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', $id);
+        return $query->getResult()[0];
+    }
+
     /**
      * @Rest\Get("/api/person/{id}/localisation")
      * @Rest\View()
      */
     public function getLocalisationAction(Request $request)
     {
+        //TODO: Check that the authenticated user is allowed to see the localisation
         $em = $this->getDoctrine()->getManager();
-        $localisations = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]);
 
-        if (empty($localisations)) {
+        $localisation = $this->getLastLocalisation($em, $request->get('id'));
+
+        if (empty($localisation)) {
             return $this->PersonLocalisationNotFound();
         }
 
-        return $localisations;
+        return $localisation;
+    }
+
+    private function chk_distance($distance, $min, $max) {
+        if ($distance >= $min && $distance <= $max) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    private function randomizeLocation($localisation, $distance, $min, $max) {
+        // Generate random float in [0, 1[, [0, 1)
+        $u = rand(0, getrandmax() - 1) / getrandmax();
+        $v = rand(0, getrandmax() - 1) / getrandmax();
+
+        if ($this->chk_distance($distance, $min, $max)) {
+            $r = $distance / 111300;
+        } else {
+            return $this->PersonLocalisationFuzzyWrongDistance();
+        }
+
+        $w = $r * sqrt($u);
+        $t = 2 * pi() * $v;
+
+        $x = $w * cos($t);
+        $lng_off = $x / cos(deg2rad($localisation->getLatitude()));
+        $lat_off = $w * sin($t);
+
+        $fuzzy_localisation = new Localisation();
+        $fuzzy_localisation->setTimestamp($localisation->getTimestamp());
+        $fuzzy_localisation->setLatitude($localisation->getLatitude() + $lat_off);
+        $fuzzy_localisation->setLongitude($localisation->getLongitude() + $lng_off);
+        return $fuzzy_localisation;
+    }
+
+    /**
+     * @Rest\Get(
+     *     path = "/api/person/{id}/localisation/fuzzy/{distance}",
+     *     name = "person_localisation_fuzzy",
+     *     requirements = {"id"="\d+", "distance"="\d+"}
+     * )
+     * @Rest\View()
+     */
+    public function getLocalisationFuzzyAction(Request $request)
+    {
+        //TODO: Check that the authenticated user is allowed to see the localisation
+        $em = $this->getDoctrine()->getManager();
+
+        $localisation = $this->getLastLocalisation($em, $request->get('id'));
+
+        if (empty($localisation)) {
+            return $this->PersonLocalisationNotFound();
+        }
+
+        return $this->randomizeLocation($localisation, $request->get('distance'), 200, 500);
     }
 
     /**
@@ -111,28 +261,37 @@ class PersonController extends Controller
      */
     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();
     }
 
     /**
-    * @Rest\Get(
-    *     path = "/api/person/{id}",
-    *     name = "show_person",
-    *     requirements = {"id"="\d+"}
-    * )
-    * @Rest\View()
-    */
-   public function showPerson(Request $request)
-   {
+     * @Rest\Get(
+     *     path = "/api/person/{id}",
+     *     name = "show_person",
+     *     requirements = {"id"="\d+"}
+     * )
+     * @Rest\View()
+     */
+    public function showPerson(Request $request)
+    {
         $em = $this->getDoctrine()->getManager();
         $person = $em->getRepository('App:Person')->find($request->get('id'));
 
@@ -141,38 +300,38 @@ class PersonController extends Controller
         }
 
         return $person;
-   }
-
-   /**
-   * @Rest\Get(
-   *     path = "/api/person/{email}",
-   *     name = "show_person_by_email",
-   *     requirements = {"email"="\s+"}
-   * )
-   * @Rest\View()
-   */
-  public function showPersonByEmail(Request $request)
-  {
+    }
+
+    /**
+     * @Rest\Get(
+     *     path = "/api/person/{id}/friends",
+     *     name = "show_person_friends",
+     *     requirements = {"id"="\d+"}
+     * )
+     * @Rest\View()
+     */
+    public function showPersonFriends(Request $request)
+    {
         $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;
-  }
-
-   /**
-   * @Rest\Get(
-   *     path = "/api/person/{id}/friends",
-   *     name = "show_person_friends",
-   *     requirements = {"id"="\d+"}
-   * )
-   * @Rest\View()
-   */
-  public function showPersonFriends(Request $request)
-  {
+        return $person->getFriends();
+    }
+
+    /**
+     * @Rest\Get(
+     *     path = "/api/person/{id}/friendswithme",
+     *     name = "show_person_friends_with_me",
+     *     requirements = {"id"="\d+"}
+     * )
+     * @Rest\View()
+     */
+    public function showPersonFriendsWithMe(Request $request)
+    {
         $em = $this->getDoctrine()->getManager();
         $person = $em->getRepository('App:Person')->find($request->get('id'));
 
@@ -180,62 +339,95 @@ class PersonController extends Controller
             return $this->PersonNotFound();
         }
 
-        return $person->getFriends();
-  }
-
-  /**
-  * @Rest\Get(
-  *     path = "/api/persons",
-  *     name = "show_persons"
-  * )
-  * @Rest\View()
-  */
- public function showPersons(Request $request)
- {
-       $em = $this->getDoctrine()->getManager();
-       $persons = $em->getRepository('App:Person')->findAll();
-
-       if (empty($persons)) {
+        return $person->getFriendsWithMe();
+    }
+
+    /**
+     * @Rest\Get(
+     *     path = "/api/persons",
+     *     name = "show_persons"
+     * )
+     * @Rest\View()
+     */
   public function showPersons(Request $request)
   {
+        $em = $this->getDoctrine()->getManager();
+        $persons = $em->getRepository('App:Person')->findAll();
+
+        if (empty($persons)) {
            return $this->PersonsNotFound();
-       }
-
-       return $persons;
- }
-
-  /**
-  * @Rest\Get(
-  *     path = "/api/person/{email}/friends",
-  *     name = "show_person_friends_by_email",
-  *     requirements = {"email"="\s+"}
-  )
-  * @Rest\View()
-  */
- public function showPersonFriendsByEmail(Request $request)
- {
+        }
+
+        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')->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);
 
- private function PersonNotFound() {
-     return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
- }
+        $em->merge($person);
+        $em->flush();
   }
 
- private function PersonsNotFound() {
-     return View::create(['message' => 'Persons not found'], Response::HTTP_NOT_FOUND);
- }
+    /**
+     * @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();
+        }
 
- private function PersonLocalisationNotFound() {
-     return View::create(['message' => 'Person localisation not found'], Response::HTTP_NOT_FOUND);
- }
+        $person->setOnline(false);
 
- private function PersonLocalisationsNotFound() {
-     return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
- }
+        $em->merge($person);
+        $em->flush();
+    }
+
+    private function PersonNotFound() {
+        return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+    }
+
+    private function PersonsNotFound() {
+        return View::create(['message' => 'Persons not found'], Response::HTTP_NOT_FOUND);
+    }
+
+    private function PersonLocalisationNotFound() {
+        return View::create(['message' => 'Person localisation not found'], Response::HTTP_NOT_FOUND);
+    }
+
+    private function PersonLocalisationsNotFound() {
+        return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
+    }
+
+    private function PersonWrongPassword() {
+        return View::create(['message' => 'Supplied password do not match'], Response::HTTP_UNAUTHORIZED);
+    }
+    private function PersonLocalisationFuzzyWrongDistance() {
+        return View::create(['message' => 'Distance range do not match'], Response::HTTP_NOT_ACCEPTABLE);
+    }
 
 }