2 namespace App\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
6 use FOS\RestBundle\Controller\Annotations
as Rest
;
7 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
8 use Symfony\Component\HttpFoundation\Request
;
9 use Symfony\Component\HttpFoundation\Response
;
10 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
12 class PersonController
extends FOSRestController
16 * path = "/person/inscription",
17 * name = "create_person"
19 * @Rest\View(StatusCode = Response::HTTP_CREATED)
20 * @ParamConverter("person", converter="fos_rest.request_body")
22 public function createPersonAction(Person
$person)
24 $em = $this->getDoctrine()->getManager();
26 $em->persist($person);
29 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
33 * @Rest\Delete("/person/{id}")
34 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
36 public function removePersonAction(Request
$request)
38 $em = $this->getDoctrine()->getManager();
39 $person = $em->getRepository('App:Person')->find($request->get('id'));
49 * path = "/person/{id}/update",
50 * name = "update_person"
52 * @Rest\View(StatusCode = Response::HTTP_CREATED)
54 public function updatePersonAction(Request
$request)
56 $em = $this->getDoctrine()->getManager();
57 $person = $em->getRepository('App:Person')->find($request->get('id'));
60 return new JsonResponse(['message' => 'Person not found'], Response
::HTTP_NOT_FOUND
);
63 $person->setFirstName($request->get('firstname'));
64 $person->setLastName($request->get('lastname'));
65 $person->setEmail($request->get('email'));
70 return $this->view($person, Response
::HTTP_CREATED
, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface
::ABSOLUTE_URL
])]);
74 * @Rest\Get("/person/{id}/friends/localisation")
76 public function getFriendsLocalisationAction(Request
$request)
82 * @Rest\Post("/person/{id}/localisation")
84 public function updateLocalisationAction(Request
$request)
86 $localisation = new Localisation();
87 $localisation->setPerson($request->get('id'));
88 $localisation->setTimestamp($request->get('timestamp'));
89 $localisation->setLatitude($request->get('latitude'));
90 $localisation->setLongitude($request->get('longitude'));
92 $em = $this->getDoctrine()->getManager();
94 $em->persist($localisation);
100 * path = "/person/{id}",
101 * name = "show_person",
102 * requirements = {"id"="\d+"}
106 public function showPerson(Person
$person)
113 * path = "/person/{email}",
114 * name = "show_person_by_email",
115 * requirements = {"email"="\s+"}
119 public function showPersonByEmail(Person
$person)
126 * path = "/person/{id}/friends",
127 * name = "show_person_friends",
128 * requirements = {"id"="\d+"}
132 public function showPersonFriends(Person
$person)
134 return $person->getFriends();
139 * path = "/person/{email}/friends",
140 * name = "show_person_friends_by_email",
141 * requirements = {"email"="\s+"}
145 public function showPersonFriendsByEmail(Person
$person)
147 return $person->getFriends();