/**
* Update existing comment from the submitted data or create a new comment at a specific location.
*
* @ApiDoc(
* resource = true,
* input = "AppBundle\Entity\Comment",
* statusCodes = {
* 201 = "Returned when a new resource is created",
* 204 = "Returned when successful",
* 400 = "Returned when the form has errors"
* }
* )
*
* @Put("/comments/{id}", requirements={"id" = "\d+"})
*
* @param Request $request
* @param int $id
*
* @return View|Response
*/
public function updateCommentAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$comment = $em->getRepository('AppBundle:Comment')->find($id);
if (null === $comment) {
$comment = new Comment();
$comment->setId($id);
$em->persist($comment);
$metadata = $em->getClassMetadata(get_class($comment));
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
$statusCode = Response::HTTP_CREATED;
} else {
$statusCode = Response::HTTP_NO_CONTENT;
}
$form = $this->createForm(new CommentRestType(), $comment);
$form->submit($request);
if ($form->isValid()) {
$em->flush();
return $this->routeRedirectView('get_comment', array('id' => $comment->getId()), $statusCode);
}
$view = new View($form);
return $this->handleView($view);
}