namespace App\Controller;
use App\Entity\Person;
-use FOS\RestBundle\Controller\FOSRestController;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+#use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
+use FOS\RestBundle\View\ViewHandler;
+use FOS\RestBundle\View\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
-class PersonController extends FOSRestController
+class PersonController extends Controller
{
/**
* @Rest\Post(
- * path = "/api/person/inscription",
+ * path = "/person/inscription",
* name = "create_person"
* )
* @Rest\View(StatusCode = Response::HTTP_CREATED)
public function removePersonAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
- $person = $em->getRepository('App::Person')->find($request->get('id'));
+ $person = $em->getRepository('App:Person')->find($request->get('id'));
if (!empty($person)) {
$em->remove($person);
public function updatePersonAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
- $person = $em->getRepository('App::Person')->find($request->get('id'));
+ $person = $em->getRepository('App:Person')->find($request->get('id'));
if (empty($person)) {
- return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ return $this->PersonNotFound();
}
$person->setFirstName($request->get('firstname'));
}
+ /**
+ * @Rest\Get("/api/person/{id}/localisations")
+ */
+ public function getLocalisationsAction(Request $request)
+ {
+ $em = $this->getDoctrine()->getManager();
+ $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
+
+ if (empty($localisations)) {
+ return $this->PersonLocalisationsNotFound();
+ }
+
+ return $localisations;
+ }
+
/**
* @Rest\Post("/api/person/{id}/localisation")
* @Rest\View(StatusCode = Response::HTTP_CREATED)
*/
public function showPerson(Request $request)
{
- $em = $this->getDoctrine()->getManager();
- $person = $em->getRepository('App::Person')->find($request->get('id'));
+ $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);
+ if (empty($person)) {
+ return $this->PersonNotFound();
}
- return $person;
+ return $person;
}
/**
*/
public function showPersonByEmail(Request $request)
{
- $em = $this->getDoctrine()->getManager();
- $person = $em->getRepository('App::Person')->find($request->get('email'));
+ $em = $this->getDoctrine()->getManager();
+ $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
- if (empty($person)) {
- return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
- }
+ if (empty($person)) {
+ return $this->PersonNotFound();
+ }
- return $person;
+ return $person;
}
/**
*/
public function showPersonFriends(Request $request)
{
- $em = $this->getDoctrine()->getManager();
- $person = $em->getRepository('App::Person')->find($request->get('id'));
+ $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);
- }
+ if (empty($person)) {
+ return $this->PersonNotFound();
+ }
- return $person->getFriends();
+ return $person->getFriends();
}
+ /**
+ * @Rest\Get(
+ * path = "/api/persons",
+ * name = "show_persons"
+ * )
+ * @Rest\View()
+ */
+ public function showPersons(Request $request)
+ {
+ $em = $this->getDoctrine()->getManager();
+ $persons = $em->getRepository('App:Person')->findAll();
+
+ if (empty($persons)) {
+ return $this->PersonNotFound();
+ }
+
+ return $persons;
+ }
+
/**
* @Rest\Get(
* path = "/api/person/{email}/friends",
*/
public function showPersonFriendsByEmail(Request $request)
{
- $em = $this->getDoctrine()->getManager();
- $person = $em->getRepository('App::Person')->find($request->get('email'));
+ $em = $this->getDoctrine()->getManager();
+ $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
+
+ if (empty($person)) {
+ return $this->PersonNotFound();
+ }
- if (empty($person)) {
- return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
- }
+ return $person->getFriends();
+ }
- return $person->getFriends();
+ private function PersonNotFound() {
+ return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
}
+
+ private function PersonLocalisationsNotFound() {
+ return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
+ }
+
}