$em->persist($person);
$em->flush();
+ //TODO: use ViewHandler
return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
}
$em = $this->getDoctrine()->getManager();
$person = $em->getRepository('App:Person')->find($request->get('id'));
+ $viewHandler = $this->get('fos_rest.view_handler');
+
if (empty($person)) {
- return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ return $this->PersonNotFound($viewHandler);
}
$person->setFirstName($request->get('firstname'));
$em->merge($person);
$em->flush();
+ //TODO: use ViewHandler
return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
}
$em = $this->getDoctrine()->getManager();
$localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
+ $viewHandler = $this->get('fos_rest.view_handler');
+
if (empty($localisations)) {
- return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
+ return $this->PersonLocalisationsNotFound($viewHandler);
}
$view = View::create($localisations);
$view->setFormat('json');
- $viewHandler = $this->get('fos_rest.view_handler');
return $viewHandler->handle($view);
}
$em = $this->getDoctrine()->getManager();
$person = $em->getRepository('App:Person')->find($request->get('id'));
+ $viewHandler = $this->get('fos_rest.view_handler');
+
if (empty($person)) {
- return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ return $this->PersonNotFound($viewHandler);
}
$view = View::create($person);
$view->setFormat('json');
- $viewHandler = $this->get('fos_rest.view_handler');
return $viewHandler->handle($view);
}
public function showPersonByEmail(Request $request)
{
$em = $this->getDoctrine()->getManager();
- $person = $em->getRepository('App:Person')->find($request->get('email'));
+ $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
+
+ $viewHandler = $this->get('fos_rest.view_handler');
if (empty($person)) {
- return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ return $this->PersonNotFound($viewHandler);
}
$view = View::create($person);
$view->setFormat('json');
- $viewHandler = $this->get('fos_rest.view_handler');
return $viewHandler->handle($view);
}
$em = $this->getDoctrine()->getManager();
$person = $em->getRepository('App:Person')->find($request->get('id'));
+ $viewHandler = $this->get('fos_rest.view_handler');
+
if (empty($person)) {
- return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ return $this->PersonNotFound($viewHandler);
}
$view = View::create($person->getFriends());
$view->setFormat('json');
- $viewHandler = $this->get('fos_rest.view_handler');
return $viewHandler->handle($view);
}
public function showPersonFriendsByEmail(Request $request)
{
$em = $this->getDoctrine()->getManager();
- $person = $em->getRepository('App:Person')->find($request->get('email'));
+ $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
+
+ $viewHandler = $this->get('fos_rest.view_handler');
if (empty($person)) {
- return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ return $this->PersonNotFound($viewHandler);
}
$view = View::create($person->getFriends());
$view->setFormat('json');
- $viewHandler = $this->get('fos_rest.view_handler');
return $viewHandler->handle($view);
}
+
+ private function PersonNotFound($viewHandler) {
+ return $viewHandler->handle(View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND));
+ }
+
+ private function PersonLocalisationsNotFound($viewHandler) {
+ return $viewHandler->handle(View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND));
+ }
+
}