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