Test and fixes every REST ressources already implemented.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 28 Jun 2018 15:33:35 +0000 (17:33 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 28 Jun 2018 15:33:35 +0000 (17:33 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Controller/PersonController.php
src/Entity/Friendship.php
src/Entity/Localisation.php
src/Entity/Person.php
tests/curl.txt

index bc34bcd229ec24e6c02ceadddb6e83478b18d8e2..9ef74692683fd17976ef9ed4619943d28ae63e6a 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",
@@ -210,26 +227,6 @@ class PersonController extends FOSRestController
         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)
-    {
-        $em = $this->getDoctrine()->getManager();
-        $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
-
-        if (empty($person)) {
-            return $this->PersonNotFound();
-        }
-
-        return $person->getFriends();
-    }
-
     private function PersonNotFound() {
         return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
     }
index 444523f5b3d50513c2faad00aecf1453f51787d4..5065733cea9c2022538038e30675d36d6dd822c5 100644 (file)
@@ -13,13 +13,13 @@ class Friendship
 {
     /**
      * @ORM\Id
-     * @ORM\ManyToOne(targetEntity="Person", inversedBy="friends", cascade={"all"})
+     * @ORM\ManyToOne(targetEntity="Person", inversedBy="friends")
      */
     protected $person;
 
     /**
      * @ORM\Id
-     * @ORM\ManyToOne(targetEntity="Person", inversedBy="friends_with_me", cascade={"all"})
+     * @ORM\ManyToOne(targetEntity="Person", inversedBy="friends_with_me")
      * @Serializer\Expose
      */
     protected $friend;
index b85a705bca5acfb4d1a2a3c202df708ec2feca62..25ee27e7634e01b342df0d8319a73af27951995c 100644 (file)
@@ -19,7 +19,7 @@ class Localisation
     protected $id;
 
     /**
-     * @ORM\ManyToOne(targetEntity="App\Entity\Person", cascade={"all"})
+     * @ORM\ManyToOne(targetEntity="App\Entity\Person")
      */
     protected $person;
 
@@ -72,6 +72,12 @@ class Localisation
         return $this;
     }
 
+    public function setPerson($person)
+    {
+        $this->person = $person;
+        return $this;
+    }
+
     public function setTimestamp($timestamp)
     {
         $this->timestamp = $timestamp;
index d06f5756f9ce2a8ce1b2e981ae1b01e0e92d397c..62cb46b3a446ccb2cf0648191b7107faeada4fa7 100644 (file)
@@ -52,13 +52,13 @@ class Person
 
     /**
     * One person have many friends
-    * @ORM\OneToMany(targetEntity="App\Entity\Friendship", mappedBy="person", cascade={"all"})
+    * @ORM\OneToMany(targetEntity="App\Entity\Friendship", mappedBy="person")
     */
     protected $friends;
 
     /**
     * One person have many friends
-    * @ORM\OneToMany(targetEntity="App\Entity\Friendship", mappedBy="friend", cascade={"all"})
+    * @ORM\OneToMany(targetEntity="App\Entity\Friendship", mappedBy="friend")
     */
     protected $friends_with_me;
 
index 8202612adde585ce0d596dffd3a0a7f7c39ff6fc..aa80b3a9ccbead7707dff6e056c322eb9d385b27 100644 (file)
@@ -1,14 +1,23 @@
-* Create a user :
-curl --request POST --data "{ \"firstname\": \"James\", \"lastname\": \"Elbow\", \"email\": \"james@elbow.com\", \"password\": \"FD3CC7719AB1776B81D70B65170D8A51119E127488C106622E0D2E680C1B3FFF\"}" http://localhost:8000/api/person/register --header "Content-Type: application/json"
+* Create a user:
+curl --request POST http://localhost:8000/api/person/register --data "{ \"firstname\": \"James\", \"lastname\": \"Elbow\", \"email\": \"james@elbow.com\", \"password\": \"FD3CC7719AB1776B81D70B65170D8A51119E127488C106622E0D2E680C1B3FFF\"}" --header "Content-Type: application/json"
+
+* Delete a user with id 1:
+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:
+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"
 
 * Show a user with id 1:
 curl --request GET http://localhost:8000/api/person/1
 
-* Show a user with id 1 friends :
+* Show a user with id 1 friends:
 curl --request GET http://localhost:8000/api/person/1/friends
 
-* Show a user with id 1 localisation :
+* Show a user with id 1 localisation:
 curl --request GET http://localhost:8000/api/person/1/localisation
 
-* Show a user with id 1 localisations :
+* Show a user with id 1 localisations:
 curl --request GET http://localhost:8000/api/person/1/localisations