| 1 | <?php |
| 2 | namespace App\Controller; |
| 3 | |
| 4 | use App\Entity\Person; |
| 5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
| 6 | #use FOS\RestBundle\Controller\FOSRestController; |
| 7 | use FOS\RestBundle\Controller\Annotations as Rest; |
| 8 | use FOS\RestBundle\View\ViewHandler; |
| 9 | use FOS\RestBundle\View\View; |
| 10 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
| 11 | use Symfony\Component\HttpFoundation\Request; |
| 12 | use Symfony\Component\HttpFoundation\Response; |
| 13 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 14 | |
| 15 | class PersonController extends Controller |
| 16 | { |
| 17 | /** |
| 18 | * @Rest\Post( |
| 19 | * path = "/api/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 | //TODO: use ViewHandler |
| 33 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @Rest\Delete("/api/person/{id}") |
| 38 | * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) |
| 39 | */ |
| 40 | public function removePersonAction(Request $request) |
| 41 | { |
| 42 | $em = $this->getDoctrine()->getManager(); |
| 43 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
| 44 | |
| 45 | if (!empty($person)) { |
| 46 | $em->remove($person); |
| 47 | $em->flush(); |
| 48 | } |
| 49 | //TODO: remove localisation and friendship |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @Rest\Put( |
| 54 | * path = "/api/person/{id}/update", |
| 55 | * name = "update_person" |
| 56 | * ) |
| 57 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
| 58 | */ |
| 59 | public function updatePersonAction(Request $request) |
| 60 | { |
| 61 | $em = $this->getDoctrine()->getManager(); |
| 62 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
| 63 | |
| 64 | $viewHandler = $this->get('fos_rest.view_handler'); |
| 65 | |
| 66 | if (empty($person)) { |
| 67 | return $this->PersonNotFound($viewHandler); |
| 68 | } |
| 69 | |
| 70 | $person->setFirstName($request->get('firstname')); |
| 71 | $person->setLastName($request->get('lastname')); |
| 72 | $person->setEmail($request->get('email')); |
| 73 | |
| 74 | $em->merge($person); |
| 75 | $em->flush(); |
| 76 | |
| 77 | //TODO: use ViewHandler |
| 78 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @Rest\Get("/api/person/{id}/friends/localisation") |
| 83 | */ |
| 84 | public function getFriendsLocalisationAction(Request $request) |
| 85 | { |
| 86 | |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @Rest\Get("/api/person/{id}/localisations") |
| 91 | */ |
| 92 | public function getLocalisationsAction(Request $request) |
| 93 | { |
| 94 | $em = $this->getDoctrine()->getManager(); |
| 95 | $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); |
| 96 | |
| 97 | $viewHandler = $this->get('fos_rest.view_handler'); |
| 98 | |
| 99 | if (empty($localisations)) { |
| 100 | return $this->PersonLocalisationsNotFound($viewHandler); |
| 101 | } |
| 102 | |
| 103 | $view = View::create($localisations); |
| 104 | $view->setFormat('json'); |
| 105 | |
| 106 | return $viewHandler->handle($view); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @Rest\Post("/api/person/{id}/localisation") |
| 112 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
| 113 | */ |
| 114 | public function updateLocalisationAction(Request $request) |
| 115 | { |
| 116 | $localisation = new Localisation(); |
| 117 | $localisation->setPerson($request->get('id')); |
| 118 | $localisation->setTimestamp($request->get('timestamp')); |
| 119 | $localisation->setLatitude($request->get('latitude')); |
| 120 | $localisation->setLongitude($request->get('longitude')); |
| 121 | |
| 122 | $em = $this->getDoctrine()->getManager(); |
| 123 | |
| 124 | $em->persist($localisation); |
| 125 | $em->flush(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @Rest\Get( |
| 130 | * path = "/api/person/{id}", |
| 131 | * name = "show_person", |
| 132 | * requirements = {"id"="\d+"} |
| 133 | * ) |
| 134 | * @Rest\View() |
| 135 | */ |
| 136 | public function showPerson(Request $request) |
| 137 | { |
| 138 | $em = $this->getDoctrine()->getManager(); |
| 139 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
| 140 | |
| 141 | $viewHandler = $this->get('fos_rest.view_handler'); |
| 142 | |
| 143 | if (empty($person)) { |
| 144 | return $this->PersonNotFound($viewHandler); |
| 145 | } |
| 146 | |
| 147 | $view = View::create($person); |
| 148 | $view->setFormat('json'); |
| 149 | |
| 150 | return $viewHandler->handle($view); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @Rest\Get( |
| 155 | * path = "/api/person/{email}", |
| 156 | * name = "show_person_by_email", |
| 157 | * requirements = {"email"="\s+"} |
| 158 | * ) |
| 159 | * @Rest\View() |
| 160 | */ |
| 161 | public function showPersonByEmail(Request $request) |
| 162 | { |
| 163 | $em = $this->getDoctrine()->getManager(); |
| 164 | $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]); |
| 165 | |
| 166 | $viewHandler = $this->get('fos_rest.view_handler'); |
| 167 | |
| 168 | if (empty($person)) { |
| 169 | return $this->PersonNotFound($viewHandler); |
| 170 | } |
| 171 | |
| 172 | $view = View::create($person); |
| 173 | $view->setFormat('json'); |
| 174 | |
| 175 | return $viewHandler->handle($view); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @Rest\Get( |
| 180 | * path = "/api/person/{id}/friends", |
| 181 | * name = "show_person_friends", |
| 182 | * requirements = {"id"="\d+"} |
| 183 | * ) |
| 184 | * @Rest\View() |
| 185 | */ |
| 186 | public function showPersonFriends(Request $request) |
| 187 | { |
| 188 | $em = $this->getDoctrine()->getManager(); |
| 189 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
| 190 | |
| 191 | $viewHandler = $this->get('fos_rest.view_handler'); |
| 192 | |
| 193 | if (empty($person)) { |
| 194 | return $this->PersonNotFound($viewHandler); |
| 195 | } |
| 196 | |
| 197 | $view = View::create($person->getFriends()); |
| 198 | $view->setFormat('json'); |
| 199 | |
| 200 | return $viewHandler->handle($view); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @Rest\Get( |
| 205 | * path = "/api/person/{email}/friends", |
| 206 | * name = "show_person_friends_by_email", |
| 207 | * requirements = {"email"="\s+"} |
| 208 | * ) |
| 209 | * @Rest\View() |
| 210 | */ |
| 211 | public function showPersonFriendsByEmail(Request $request) |
| 212 | { |
| 213 | $em = $this->getDoctrine()->getManager(); |
| 214 | $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]); |
| 215 | |
| 216 | $viewHandler = $this->get('fos_rest.view_handler'); |
| 217 | |
| 218 | if (empty($person)) { |
| 219 | return $this->PersonNotFound($viewHandler); |
| 220 | } |
| 221 | |
| 222 | $view = View::create($person->getFriends()); |
| 223 | $view->setFormat('json'); |
| 224 | |
| 225 | return $viewHandler->handle($view); |
| 226 | } |
| 227 | |
| 228 | private function PersonNotFound($viewHandler) { |
| 229 | return $viewHandler->handle(View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND)); |
| 230 | } |
| 231 | |
| 232 | private function PersonLocalisationsNotFound($viewHandler) { |
| 233 | return $viewHandler->handle(View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND)); |
| 234 | } |
| 235 | |
| 236 | } |