Permalink
Browse files

add phpspec tests for paginator, improvements for editorial comments

  • Loading branch information...
ahilles107 committed Oct 8, 2014
1 parent 0ee97f3 commit 64398f33eefa7713e53ed3b73d73fd9e2532471e
@@ -61,7 +61,7 @@ fos_rest:
codes:
'Newscoop\Exception\ResourcesConflictException': 409
'Newscoop\Exception\InvalidParametersException': 422
'Newscoop\Exception\ResourceIsEmptyException': 204
'Newscoop\Exception\ResourceIsEmptyException': 200
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\EntityNotFoundException': 404
'Symfony\Component\Form\Exception\InvalidArgumentException': 500
@@ -41,6 +41,10 @@ NewscoopGimmeBundleConfigureApi:
newscoop:
prefix: /
resource: "@NewscoopNewscoopBundle/Resources/config/routing.yml"
articles:
prefix: /
prefix: /api
resource: "@NewscoopArticlesBundle/Resources/config/routing.yml"
plugins:
resource: .
type: plugins
@@ -93,7 +93,8 @@ public function setPagination(Pagination $pagination)
* Get Pagination object
* @return Pagination Pagination object
*/
public function getPagination() {
public function getPagination()
{
return $this->pagination;
}
@@ -104,6 +105,8 @@ public function getPagination() {
public function setPartialResponse($partialResponse)
{
$this->partialResponse = $partialResponse;
return $this;
}
/**
@@ -163,21 +166,13 @@ public function setPaginationData(array $paginationData)
/**
* Paginate data
*
* @param mixed $data Data to paginate
* @param array $params Parameters for Paginator
* @param bool $params['emptyResponse'] Sets behaviour for an empty response, Default true returns 204. False returns 404
* Won't be send to the Paginator
* @param mixed $data Data to paginate
* @param array $params Parameters for Paginator
*
* @return array Paginated data
*/
public function paginate($data, $params = array())
{
$emptyResponse = true;
if (array_key_exists('emptyResponse', $params)) {
$emptyResponse = $params['emptyResponse'];
unset($params['emptyAllowed']);
}
$paginator = $this->paginator->paginate(
$data,
$this->pagination->getPage(),
@@ -187,14 +182,6 @@ public function paginate($data, $params = array())
$items['items'] = $paginator->getItems();
if (count($items['items']) == 0) {
if ($emptyResponse) {
throw new ResourceIsEmptyException('Result is empty.');
} else {
throw new NotFoundHttpException('Results was not found.');
}
}
/**
* Set pagination object only when need
*/
@@ -214,10 +201,6 @@ public function paginate($data, $params = array())
*/
private function getPaginationLinks($paginationData)
{
// idea is that if you are somewhere and you use pagination
// and get link to go back it should be the very same uri you've visited
// in general it can filter all the params with default values
$data = array();
if ($paginationData['current'] < $paginationData['lastPageInRange']) {
@@ -232,4 +215,4 @@ private function getPaginationLinks($paginationData)
return $data;
}
}
}
@@ -17,7 +17,7 @@ class PartialResponse {
* @var string
*/
protected $fields = null;
/**
* Set fields
* @param string $fields string with comma separated fields
@@ -0,0 +1,148 @@
<?php
namespace Newscoop\ArticlesBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\View;
use Symfony\Component\HttpFoundation\Request;
use Newscoop\ArticlesBundle\Form\Type\EditorialCommentType;
use Newscoop\ArticlesBundle\Entity\EditorialComment;
use Symfony\Component\HttpFoundation\Response;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class EditorialCommentsApiController extends FOSRestController
{
/**
* Get editorial comments
*
* @ApiDoc(
* statusCodes={
* 200="Returned when success",
* }
* )
*
* @Route("/articles/{number}/{language}/editorial_comments.{_format}", defaults={"_format"="json"}, options={"expose"=true}, name="newscoop_gimme_articles_get_editorial_comment")
* @Method("GET")
* @View(serializerGroups={"list"})
*/
public function getCommentsAction(Request $request)
{
$em = $this->container->get('em');
$editorialComments = $em->getRepository('Newscoop\ArticlesBundle\Entity\EditorialComment')->getAll();
$paginator = $this->get('newscoop.paginator.paginator_service');
$editorialComments = $paginator->paginate($editorialComments, array(
'distinct' => false
));
return $editorialComments;
}
/**
* Create editorial comments
*
* @ApiDoc(
* statusCodes={
* 201="Returned when editorial comment is created",
* 404={
* "Returned when article is not found",
* }
* }
* )
*
* @Route("/articles/{number}/{language}/editorial_comments.{_format}", defaults={"_format"="json"}, options={"expose"=true}, name="newscoop_gimme_articles_create_editorial_comment")
* @Method("POST")
* @View(serializerGroups={"list"})
*/
public function createCommentAction(Request $request, $number, $language)
{
$em = $this->container->get('em');
$article = $em->getRepository('Newscoop\Entity\Article')
->getArticle($number, $language)
->getOneOrNullResult();
return $this->processForm($request, $article);
}
/**
* Edit editorial comments
*
* @ApiDoc(
* statusCodes={
* 201="Returned when editorial comment is created",
* 404={
* "Returned when article is not found",
* }
* }
* )
*
* @Route("/articles/{number}/{language}/editorial_comments/{commentId}.{_format}", defaults={"_format"="json"}, options={"expose"=true}, name="newscoop_gimme_articles_edit_editorial_comment")
* @Method("POST")
* @View(serializerGroups={"list"})
*/
public function editCommentAction(Request $request, $number, $language, $commentId)
{
return $this->processForm($request, null, $commentId);
}
public function removeCommentAction(Request $request, $number, $language, $commentId)
{
//return $this->processForm($request, null, $commentId);
}
/**
* Process editorial comments form
*
* @param Request $request
*
* @return Form
*/
private function processForm($request, $article = null, $comment = null)
{
$em = $this->container->get('em');
$editorialCommentService = $this->container->get('newscoop.editorial_comments');
if (!$comment) {
$statusCode = 201;
} else {
$statusCode = 200;
$comment = $em->getRepository('Newscoop\ArticlesBundle\Entity\EditorialComment')->findOneBy(array('id' => 1));
if (!$comment) {
throw new EntityNotFoundException('Result was not found.');
}
}
$form = $this->createForm(new EditorialCommentType(), array());
$form->handleRequest($request);
if ($form->isValid()) {
$attributes = $form->getData();
$user = $this->container->get('user')->getCurrentUser();
$response = new Response();
$response->setStatusCode($statusCode);
if ($statusCode == 201 && $article) {
$comment = $editorialCommentService->create($attributes['comment'], $article, $user);
// TODO: apply here new route
/* $response->headers->set(
'X-Location',
$this->generateUrl('newscoop_gimme_attachments_getattachment', array(
'number' => $comment->getId(),
), true)
);*/
} elseif ($statusCode == 200 && $comment) {
$comment = $editorialCommentService->edit($attributes['comment'], $comment, $user);
}
return $response;
}
return $form;
}
}
@@ -0,0 +1,42 @@
<?php
/**
* @package Newscoop\ArticlesBundle
* @author Paweł Mikołajczuk <pawel.mikolajczuk@sourcefabric.org>
* @copyright 2014 Sourcefabric ź.u.
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/
namespace Newscoop\ArticlesBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;
class EditorialCommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('comment', null, array(
'required' => true,
));
$builder->add('resolved', 'checkbox', array(
'required' => false,
));
$builder->add('parent', 'number', array(
'required' => false,
));
}
public function getName()
{
return 'editorial_comment';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
));
}
}
@@ -0,0 +1,4 @@
NewscoopArticlesBundleEditorialComments:
resource: "@NewscoopArticlesBundle/Controller/EditorialCommentsApiController.php"
prefix: /
type: annotation
@@ -0,0 +1,4 @@
services:
newscoop.editorial_comments:
class: Newscoop\ArticlesBundle\Services\EditorialCommentsService
arguments: ["@em"]

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 64398f3

Please sign in to comment.