use App\Entity\Person;
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;
}
/**
- * @Rest\View()
* @Rest\Get(
* path = "/api/person/{id}",
* name = "show_person",
* requirements = {"id"="\d+"}
* )
+ * @Rest\View()
*/
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)) {
+ if (empty($person)) {
return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
}
- return $person;
+ $view = View::create($person);
+ $view->setFormat('json');
+
+ return $view;
}
/**
*/
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')->find($request->get('email'));
+
+ if (empty($person)) {
+ return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ }
- if (empty($person)) {
- return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
- }
+ $view = View::create($person);
+ $view->setFormat('json');
- return $person;
+ return $view;
}
/**
*/
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 new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
- }
+ $view = View::create($person->getFriends());
+ $view->setFormat('json');
- return $person->getFriends();
+ return $view;
}
/**
*/
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')->find($request->get('email'));
+
+ if (empty($person)) {
+ return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ }
- if (empty($person)) {
- return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
- }
+ $view = View::create($person->getFriends());
+ $view->setFormat('json');
- return $person->getFriends();
+ return $view;
}
}