From 3d47ccc80097bec1a6890e103357d7c7cf0cff87 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 28 Jun 2018 17:33:35 +0200 Subject: [PATCH] Test and fixes every REST ressources already implemented. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Controller/PersonController.php | 95 ++++++++++++++--------------- src/Entity/Friendship.php | 4 +- src/Entity/Localisation.php | 8 ++- src/Entity/Person.php | 4 +- tests/curl.txt | 19 ++++-- 5 files changed, 71 insertions(+), 59 deletions(-) diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index bc34bcd..9ef7469 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -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); } diff --git a/src/Entity/Friendship.php b/src/Entity/Friendship.php index 444523f..5065733 100644 --- a/src/Entity/Friendship.php +++ b/src/Entity/Friendship.php @@ -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; diff --git a/src/Entity/Localisation.php b/src/Entity/Localisation.php index b85a705..25ee27e 100644 --- a/src/Entity/Localisation.php +++ b/src/Entity/Localisation.php @@ -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; diff --git a/src/Entity/Person.php b/src/Entity/Person.php index d06f575..62cb46b 100644 --- a/src/Entity/Person.php +++ b/src/Entity/Person.php @@ -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; diff --git a/tests/curl.txt b/tests/curl.txt index 8202612..aa80b3a 100644 --- a/tests/curl.txt +++ b/tests/curl.txt @@ -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 -- 2.34.1