getDoctrine()->getManager(); $em->persist($person); $em->flush(); return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); } /** * @Rest\Delete("/api/person/{id}") * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) */ public function removePersonAction(Request $request) { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App::Person')->find($request->get('id')); if (!empty($person)) { $em->remove($person); $em->flush(); } //TODO: remove localisation and friendship } /** * @Rest\Put( * path = "/api/person/{id}/update", * name = "update_person" * ) * @Rest\View(StatusCode = Response::HTTP_CREATED) */ public function updatePersonAction(Request $request) { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App::Person')->find($request->get('id')); if (empty($person)) { return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } $person->setFirstName($request->get('firstname')); $person->setLastName($request->get('lastname')); $person->setEmail($request->get('email')); $em->merge($person); $em->flush(); return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); } /** * @Rest\Get("/api/person/{id}/friends/localisation") */ public function getFriendsLocalisationAction(Request $request) { } /** * @Rest\Post("/api/person/{id}/localisation") * @Rest\View(StatusCode = Response::HTTP_CREATED) */ public function updateLocalisationAction(Request $request) { $localisation = new Localisation(); $localisation->setPerson($request->get('id')); $localisation->setTimestamp($request->get('timestamp')); $localisation->setLatitude($request->get('latitude')); $localisation->setLongitude($request->get('longitude')); $em = $this->getDoctrine()->getManager(); $em->persist($localisation); $em->flush(); } /** * @Rest\View() * @Rest\Get( * path = "/api/person/{id}", * name = "show_person", * requirements = {"id"="\d+"} * ) */ public function showPerson(Request $request) { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App:Person')->find($request->get('id')); if (empty($person)) { return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } return $person; } /** * @Rest\Get( * path = "/api/person/{email}", * name = "show_person_by_email", * requirements = {"email"="\s+"} * ) * @Rest\View() */ public function showPersonByEmail(Request $request) { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App::Person')->find($request->get('email')); if (empty($person)) { return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } return $person; } /** * @Rest\Get( * path = "/api/person/{id}/friends", * name = "show_person_friends", * requirements = {"id"="\d+"} * ) * @Rest\View() */ public function showPersonFriends(Request $request) { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App:Person')->find($request->get('id')); if (empty($person)) { return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } return $person->getFriends(); } /** * @Rest\Get( * path = "/api/person/{email}/friends", * name = "show_person_friends_by_email", * requirements = {"email"="\s+"} * ) * @Rest\View() */ public function showPersonFriendsByEmail(Request $request) { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('App::Person')->find($request->get('email')); if (empty($person)) { return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } return $person->getFriends(); } }