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;
* @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'));
*/
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'));
*/
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();
}
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",
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);
}
-* 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