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