From 6c9dc16373623808d60bb07298b8fca675a2ae3b Mon Sep 17 00:00:00 2001 From: evilbloodydemon Date: Wed, 15 Jan 2014 18:54:35 +0400 Subject: [PATCH 1/2] pagination, extracted code and category services --- app/config/config.yml | 1 + .../CodeBundle/Controller/CodeController.php | 39 ++++---- .../CodeBundle/Resources/config/routing.yml | 4 +- .../CodeBundle/Resources/config/services.yml | 14 ++- .../Resources/views/Code/list.html.twig | 98 ++++++++++++++----- .../CodeBundle/Service/CategoryService.php | 27 +++++ .../CodeBundle/Service/CodeService.php | 55 +++++++++++ 7 files changed, 196 insertions(+), 42 deletions(-) create mode 100644 src/Govnokod/CodeBundle/Service/CategoryService.php create mode 100644 src/Govnokod/CodeBundle/Service/CodeService.php diff --git a/app/config/config.yml b/app/config/config.yml index bc18433..45a2f99 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -1,6 +1,7 @@ imports: - { resource: parameters.yml } - { resource: security.yml } + - { resource: @GovnokodCodeBundle/Resources/config/services.yml } framework: #esi: ~ diff --git a/src/Govnokod/CodeBundle/Controller/CodeController.php b/src/Govnokod/CodeBundle/Controller/CodeController.php index aea5057..7c27ec6 100644 --- a/src/Govnokod/CodeBundle/Controller/CodeController.php +++ b/src/Govnokod/CodeBundle/Controller/CodeController.php @@ -5,34 +5,40 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Govnokod\CodeBundle\Entity\Code; use Govnokod\CommentBundle\Entity\Thread; +use Symfony\Component\HttpFoundation\Request; class CodeController extends Controller { - public function listAction($category = null) + public function listAction(Request $request, $category = null) { - $em = $this->getDoctrine()->getManager(); - - $criteria = array(); - if (!is_null($category)) { - $categoryRepository = $em->getRepository("GovnokodCodeBundle:Category"); - $categoryEntity = $categoryRepository->findByName($category); + /* var $codeService \Govnokod\CodeBundle\Service\CodeService */ + $codeService = $this->container->get('code_service'); + /* var $categoryService \Govnokod\CodeBundle\Service\CategoryService */ + $categoryService = $this->container->get('category_service'); + $categoryEntity = null; + + if ($category) { + $categoryEntity = $categoryService->getEntity($category); if (!$categoryEntity) { throw $this->createNotFoundException("Category «{$category}» doesn't exists"); } - $criteria['category'] = $categoryEntity; } - $codeRepository = $em->getRepository('GovnokodCodeBundle:Code'); - $codes = $codeRepository->findBy($criteria, array('created_at' => 'DESC')); + $page = $request->query->get('page', 0); + $pageSize = $this->container->getParameter('code_controller.page_size'); - return $this->render('GovnokodCodeBundle:Code:list.html.twig', array( - 'codes' => $codes - )); + list($codes, $pageCount, $actualPage) = $codeService->paginate($page, $pageSize, $categoryEntity); + + return $this->render('GovnokodCodeBundle:Code:list.html.twig', [ + 'codes' => $codes, + 'currentPage' => $actualPage, + 'pageCount' => $pageCount, + 'current_filters' => ['category' => $category] + ]); } - public function viewAction($id) + public function viewAction(Request $request, $id) { - $request = $this->getRequest(); $em = $this->getDoctrine()->getManager(); $codeRepository = $em->getRepository('GovnokodCodeBundle:Code'); @@ -64,9 +70,8 @@ public function viewAction($id) )); } - public function saveAction($id = null) + public function saveAction(Request $request, $id = null) { - $request = $this->getRequest(); $em = $this->getDoctrine()->getManager(); $codeRepository = $em->getRepository('GovnokodCodeBundle:Code'); diff --git a/src/Govnokod/CodeBundle/Resources/config/routing.yml b/src/Govnokod/CodeBundle/Resources/config/routing.yml index 045ae5b..21c2985 100644 --- a/src/Govnokod/CodeBundle/Resources/config/routing.yml +++ b/src/Govnokod/CodeBundle/Resources/config/routing.yml @@ -21,5 +21,7 @@ code_view_legacy: id: \d+ code_list: - pattern: /{category}/ + pattern: /{category} defaults: { _controller: GovnokodCodeBundle:Code:list } + requirements: + category: "[a-z]*" diff --git a/src/Govnokod/CodeBundle/Resources/config/services.yml b/src/Govnokod/CodeBundle/Resources/config/services.yml index 8ee016c..d293619 100644 --- a/src/Govnokod/CodeBundle/Resources/config/services.yml +++ b/src/Govnokod/CodeBundle/Resources/config/services.yml @@ -1,5 +1,7 @@ parameters: -# govnokod_code.example.class: Govnokod\CodeBundle\Example + code_service.class: Govnokod\CodeBundle\Service\CodeService + category_service.class: Govnokod\CodeBundle\Service\CategoryService + code_controller.page_size: 20 services: sonata.admin.govnokod: @@ -7,3 +9,13 @@ services: arguments: [null, 'Govnokod\CodeBundle\Entity\Category', 'SonataAdminBundle:CRUD'] tags: - { name: sonata.admin, manager_type: orm, label: Категории говнокода } + + code_service: + class: %code_service.class% + arguments: + entityManager: "@doctrine.orm.entity_manager" + + category_service: + class: %category_service.class% + arguments: + entityManager: "@doctrine.orm.entity_manager" diff --git a/src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig b/src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig index 28a9f8d..10e915a 100644 --- a/src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig +++ b/src/Govnokod/CodeBundle/Resources/views/Code/list.html.twig @@ -2,33 +2,85 @@ {% block content %}
    -{% for code in codes %} -
  1. -

    + {% for code in codes %} +
  2. +

    {{ code.category.title }} - / + / Говнокод #{{ code.id }} -

    -

    +

  3. +

    {% if code.rating > 0 %}+{% elseif code.rating < 0 %}−{% endif %}{{ code.rating|abs }} -

    -
    -
    {{ code.body }}
    -
    -

    - {{ code.description|nl2br }} -

    -{# -

    - Запостил: {{ code.user.username }}, - {{ code.created|date('d F Y') }} -

    -#} -
    +

    +
    +
    {{ code.body }}
    +
    +

    + {{ code.description|nl2br }} +

    + +
     Комментарии ({{ code.commentsCount }}) {# Комментарии (getCommentsCount(); if ($toolkit->getUser()->isLoggedIn() && $quote->getNewCommentsCount() > 0) { ?>, +getNewCommentsCount(); ?>)#} -
    -
  4. -{% endfor %} + + + {% endfor %}
+ + {% set paginationPath = 'code_list' %} + {% set nearbyPagesLimit = 4 %} + {% set nearbyHigh = currentPage + nearbyPagesLimit %} + {% set nearbyLow = currentPage - nearbyPagesLimit %} + + + + {% endblock %} diff --git a/src/Govnokod/CodeBundle/Service/CategoryService.php b/src/Govnokod/CodeBundle/Service/CategoryService.php new file mode 100644 index 0000000..40e86ee --- /dev/null +++ b/src/Govnokod/CodeBundle/Service/CategoryService.php @@ -0,0 +1,27 @@ +entityManager = $entityManager; + } + + public function getEntity($category) + { + $categoryRepository = $this->entityManager->getRepository("GovnokodCodeBundle:Category"); + $entities = $categoryRepository->findByName($category); + + return $entities ? $entities[0] : null; + } + +} diff --git a/src/Govnokod/CodeBundle/Service/CodeService.php b/src/Govnokod/CodeBundle/Service/CodeService.php new file mode 100644 index 0000000..2884273 --- /dev/null +++ b/src/Govnokod/CodeBundle/Service/CodeService.php @@ -0,0 +1,55 @@ +entityManager = $entityManager; + } + + public function paginate($page, $pageSize, $categoryEntity) + { + /* @var $codeRepository \Doctrine\ORM\EntityRepository */ + $codeRepository = $this->entityManager->getRepository('GovnokodCodeBundle:Code'); + + $countQuery = $codeRepository->createQueryBuilder('c') + ->select('count(c.id) as _count'); + + if ($categoryEntity) { + $countQuery->where('c.category = :category') + ->setParameter('category', $categoryEntity); + } + + $count = $countQuery->getQuery()->execute()[0]['_count']; + $pageCount = (int) ceil($count / $pageSize); + + if ($page < 1 || $page > $pageCount) { + $page = $pageCount; + } + + $itemQuery = $codeRepository->createQueryBuilder('c') + ->select(['c', 'cat']) + ->join('c.category', 'cat') + ->orderBy('c.created_at', 'DESC') + ->setFirstResult(($pageCount - $page) * $pageSize) + ->setMaxResults($pageSize); + + if ($categoryEntity) { + $itemQuery->where('c.category = :category') + ->setParameter('category', $categoryEntity); + } + + $codes = $itemQuery->getQuery()->execute(); + + return [$codes, $pageCount, $page]; + } +} From 880c9582608f1a633a8a0cd708b85be1330e9d5e Mon Sep 17 00:00:00 2001 From: evilbloodydemon Date: Thu, 16 Jan 2014 08:52:59 +0400 Subject: [PATCH 2/2] QA fixes --- app/config/config.yml | 1 - src/Govnokod/CodeBundle/Service/CategoryService.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/config/config.yml b/app/config/config.yml index 45a2f99..bc18433 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -1,7 +1,6 @@ imports: - { resource: parameters.yml } - { resource: security.yml } - - { resource: @GovnokodCodeBundle/Resources/config/services.yml } framework: #esi: ~ diff --git a/src/Govnokod/CodeBundle/Service/CategoryService.php b/src/Govnokod/CodeBundle/Service/CategoryService.php index 40e86ee..5adbb45 100644 --- a/src/Govnokod/CodeBundle/Service/CategoryService.php +++ b/src/Govnokod/CodeBundle/Service/CategoryService.php @@ -19,9 +19,9 @@ public function __construct(ObjectManager $entityManager) public function getEntity($category) { $categoryRepository = $this->entityManager->getRepository("GovnokodCodeBundle:Category"); - $entities = $categoryRepository->findByName($category); + $entity = $categoryRepository->findOneByName($category); - return $entities ? $entities[0] : null; + return $entity; } }