| 1 | <?php |
| 2 | namespace App\Controller; |
| 3 | |
| 4 | use App\Entity\Person; |
| 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; |
| 11 | |
| 12 | class PersonController extends FOSRestController |
| 13 | { |
| 14 | /** |
| 15 | * @Rest\Post( |
| 16 | * path = "/person/inscription", |
| 17 | * name = "create_person" |
| 18 | * ) |
| 19 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
| 20 | * @ParamConverter("person", converter="fos_rest.request_body") |
| 21 | */ |
| 22 | public function createPersonAction(Person $person) |
| 23 | { |
| 24 | $em = $this->getDoctrine()->getManager(); |
| 25 | |
| 26 | $em->persist($person); |
| 27 | $em->flush(); |
| 28 | |
| 29 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @Rest\Delete("/person/{id}") |
| 34 | * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) |
| 35 | */ |
| 36 | public function removePersonAction(Request $request) |
| 37 | { |
| 38 | $em = $this->getDoctrine()->getManager(); |
| 39 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
| 40 | |
| 41 | if ($person) { |
| 42 | $em->remove($place); |
| 43 | $em->flush(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @Rest\Put( |
| 49 | * path = "/person/{id}/update", |
| 50 | * name = "update_person" |
| 51 | * ) |
| 52 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
| 53 | */ |
| 54 | public function updatePersonAction(Request $request) |
| 55 | { |
| 56 | $em = $this->getDoctrine()->getManager(); |
| 57 | $person = $em->getRepository('App::Person')->find($request->get('id')); |
| 58 | |
| 59 | if (empty($place)) { |
| 60 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); |
| 61 | } |
| 62 | |
| 63 | $person->setFirstName($request->get('firstname')); |
| 64 | $person->setLastName($request->get('lastname')); |
| 65 | $person->setEmail($request->get('email')); |
| 66 | |
| 67 | $em->merge($person); |
| 68 | $em->flush(); |
| 69 | |
| 70 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @Rest\Get("/person/{id}/friends/localisation") |
| 75 | */ |
| 76 | public function getFriendsLocalisationAction(Request $request) |
| 77 | { |
| 78 | |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @Rest\Post("/person/{id}/localisation") |
| 83 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
| 84 | */ |
| 85 | public function updateLocalisationAction(Request $request) |
| 86 | { |
| 87 | $localisation = new Localisation(); |
| 88 | $localisation->setPerson($request->get('id')); |
| 89 | $localisation->setTimestamp($request->get('timestamp')); |
| 90 | $localisation->setLatitude($request->get('latitude')); |
| 91 | $localisation->setLongitude($request->get('longitude')); |
| 92 | |
| 93 | $em = $this->getDoctrine()->getManager(); |
| 94 | |
| 95 | $em->persist($localisation); |
| 96 | $em->flush(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @Rest\Get( |
| 101 | * path = "/person/{id}", |
| 102 | * name = "show_person", |
| 103 | * requirements = {"id"="\d+"} |
| 104 | * ) |
| 105 | * @Rest\View() |
| 106 | */ |
| 107 | public function showPerson(Person $person) |
| 108 | { |
| 109 | return $person; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @Rest\Get( |
| 114 | * path = "/person/{email}", |
| 115 | * name = "show_person_by_email", |
| 116 | * requirements = {"email"="\s+"} |
| 117 | * ) |
| 118 | * @Rest\View() |
| 119 | */ |
| 120 | public function showPersonByEmail(Person $person) |
| 121 | { |
| 122 | return $person; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @Rest\Get( |
| 127 | * path = "/person/{id}/friends", |
| 128 | * name = "show_person_friends", |
| 129 | * requirements = {"id"="\d+"} |
| 130 | * ) |
| 131 | * @Rest\View |
| 132 | */ |
| 133 | public function showPersonFriends(Person $person) |
| 134 | { |
| 135 | return $person->getFriends(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @Rest\Get( |
| 140 | * path = "/person/{email}/friends", |
| 141 | * name = "show_person_friends_by_email", |
| 142 | * requirements = {"email"="\s+"} |
| 143 | * ) |
| 144 | * @Rest\View |
| 145 | */ |
| 146 | public function showPersonFriendsByEmail(Person $person) |
| 147 | { |
| 148 | return $person->getFriends(); |
| 149 | } |
| 150 | } |