Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"gedmo/doctrine-extensions": "^2.4",
"hubspot/hubspot-php": "^2.0",
"knplabs/doctrine-behaviors": "^2.0.6",
"runroom-packages/form-handler-bundle": "^0.8.1",
"runroom-packages/render-event-bundle": "^0.8.1",
"runroom-packages/sortable-behavior-bundle": "^0.8.1",
"knplabs/knp-paginator-bundle": "^5.3",
"runroom-packages/form-handler-bundle": "^0.8",
"runroom-packages/render-event-bundle": "^0.8",
"runroom-packages/sortable-behavior-bundle": "^0.8",
"sonata-project/admin-bundle": "^3.68",
"sonata-project/doctrine-extensions": "^1.9",
"sonata-project/doctrine-orm-admin-bundle": "^3.20",
Expand Down
1 change: 1 addition & 0 deletions src/BasicEntities/Admin/BookAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected function configureListFields(ListMapper $listMapper): void
->add('_action', 'actions', [
'actions' => [
'delete' => [],
'edit' => [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yo este cambio lo desharía, no es consistente con el resto de admins.

'move' => [
'template' => '@SortableBehavior/sort.html.twig',
],
Expand Down
12 changes: 12 additions & 0 deletions src/BasicEntities/Controller/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Runroom\RenderEventBundle\Renderer\PageRenderer;
use Runroom\SamplesBundle\BasicEntities\Service\BookService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class BookController
Expand Down Expand Up @@ -52,4 +53,15 @@ public function book(string $slug): Response
$model
);
}

public function list(Request $request): Response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

porqué no usas el mismo método que ya teniamos de books() y amplias su funcionalidad para incluir esto?, así evitamos tener 2 listados.

{
$page = (int) $request->get('page', 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creo que si haces: $request->query->getInt('page', 1); te da directamente un int, puede ser?

$model = $this->service->getBooksListViewModel($page);

return $this->renderer->renderResponse(
'@RunroomSamples/BasicEntities/book-list.html.twig',
$model
);
}
}
11 changes: 11 additions & 0 deletions src/BasicEntities/Repository/BookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Runroom\SamplesBundle\BasicEntities\Entity\Book;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -47,4 +48,14 @@ public function findBySlug(string $slug): Book

return $query->getSingleResult();
}

public function getBooksQueryBuilder(): QueryBuilder
{
$request = $this->requestStack->getCurrentRequest() ?? new Request();

return $this->createQueryBuilder('book')
->where('book.publish = true')
->leftJoin('book.translations', 'translations', Join::WITH, 'translations.locale = :locale')
->setParameter('locale', $request->getLocale());
}
}
28 changes: 26 additions & 2 deletions src/BasicEntities/Service/BookService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@

namespace Runroom\SamplesBundle\BasicEntities\Service;

use Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface;
use Knp\Component\Pager\PaginatorInterface;
use Runroom\SamplesBundle\BasicEntities\Repository\BookRepository;
use Runroom\SamplesBundle\BasicEntities\ViewModel\BooksListViewModel;
use Runroom\SamplesBundle\BasicEntities\ViewModel\BooksViewModel;
use Runroom\SamplesBundle\BasicEntities\ViewModel\BookViewModel;

class BookService
{
/** @var int */
private const MAX_RESULT = 5;

/** @var BookRepository */
private $repository;

public function __construct(BookRepository $repository)
{
/** @var PaginatorInterface */
private $paginator;

public function __construct(
BookRepository $repository,
PaginatorInterface $paginator
) {
$this->repository = $repository;
$this->paginator = $paginator;
}

public function getBooksViewModel(): BooksViewModel
Expand All @@ -46,4 +58,16 @@ public function getBookViewModel(string $slug): BookViewModel

return $model;
}

public function getBooksListViewModel(int $page): BooksListViewModel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aquí lo mismo, podríamos reusar el método ya existente: getBooksViewModel()

{
$queryBuilder = $this->repository->getBooksQueryBuilder();

$pagination = $this->paginator->paginate($queryBuilder, $page, self::MAX_RESULT);
\assert($pagination instanceof SlidingPaginationInterface);
$model = new BooksListViewModel();
$model->setPagination($pagination);

return $model;
}
}
64 changes: 64 additions & 0 deletions src/BasicEntities/ViewModel/BooksListViewModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RunroomSamplesBundle.
*
* (c) Runroom <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Runroom\SamplesBundle\BasicEntities\ViewModel;

use Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface;
use Runroom\SamplesBundle\BasicEntities\Entity\Book;

class BooksListViewModel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podríamos reusar el viewModel ya existente.

{
/**
* @phpstan-var SlidingPaginationInterface<Book>
* @psalm-var SlidingPaginationInterface
*/
protected $pagination;

/** @var array<mixed> */
protected $paginationData;

/** @phpstan-return SlidingPaginationInterface<Book>|null
* @psalm-return SlidingPaginationInterface|null
*/
public function getPagination(): ?SlidingPaginationInterface
{
return $this->pagination;
}

/**
* @phpstan-param SlidingPaginationInterface<Book> $pagination
* @psalm-param SlidingPaginationInterface $pagination
* */
public function setPagination(SlidingPaginationInterface $pagination): self
{
$this->pagination = $pagination;
$this->paginationData = $pagination->getPaginationData();

return $this;
}

public function getPreviousPage(): ?int
{
return $this->getPaginationData('previous');
}

public function getNextPage(): ?int
{
return $this->getPaginationData('next');
}

public function getPaginationData(string $data): ?int
{
return $this->paginationData[$data] ?? null;
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ runroom_samples.basic_entities.book:
path: /book/{slug}
controller: Runroom\SamplesBundle\BasicEntities\Controller\BookController::book

runroom_samples.basic_entities.books_list:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podríamos reusar la url ya existente.

path: /pagination
controller: Runroom\SamplesBundle\BasicEntities\Controller\BookController::list

# Forms
runroom_samples.forms.contact:
path: /contact
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ services:
arguments: [null, Runroom\SamplesBundle\BasicEntities\Entity\Category, null]
tags:
- { name: sonata.admin, manager_type: orm, label: 'Categories' }

Runroom\SamplesBundle\BasicEntities\Service\BookService:
public: true
arguments: ['@Runroom\SamplesBundle\BasicEntities\Repository\BookRepository', '@knp_paginator']

# Forms
Runroom\SamplesBundle\Forms\Controller\:
Expand Down
20 changes: 20 additions & 0 deletions src/Resources/views/BasicEntities/book-list.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends '@RunroomSamples/base.html.twig' %}

{% block content %}
<div class="u-wrapper">
<div class="list">
<ul>
{% for book in model.pagination.items %}
<div class="book-item">
<li>
<a href="{{ path('runroom_samples.basic_entities.book', {slug: book.slug}) }}">{{book.title}}</a>
</li>
</div>
{% endfor %}
</ul>
</div>
<div class="pagination">
{{ knp_pagination_render(model.pagination) }}
</div>
</div>
{% endblock content %}
2 changes: 2 additions & 0 deletions tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use FOS\CKEditorBundle\FOSCKEditorBundle;
use Knp\Bundle\MenuBundle\KnpMenuBundle;
use Knp\Bundle\PaginatorBundle\KnpPaginatorBundle;
use Knp\DoctrineBehaviors\DoctrineBehaviorsBundle;
use Runroom\FormHandlerBundle\RunroomFormHandlerBundle;
use Runroom\RenderEventBundle\RunroomRenderEventBundle;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function registerBundles(): iterable
new FOSCKEditorBundle(),
new FrameworkBundle(),
new KnpMenuBundle(),
new KnpPaginatorBundle(),
new SecurityBundle(),
new TwigBundle(),
new SonataMediaBundle(),
Expand Down
10 changes: 9 additions & 1 deletion tests/BasicEntities/Unit/BookServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Runroom\SamplesBundle\Tests\BasicEntities\Unit;

use Knp\Component\Pager\PaginatorInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
Expand All @@ -30,11 +31,18 @@ class BookServiceTest extends TestCase
/** @var BookService */
private $service;

/** @var PaginatorInterface */
private $paginator;

protected function setUp(): void
{
$this->repository = $this->prophesize(BookRepository::class);
$this->paginator = $this->createMock(PaginatorInterface::class);

$this->service = new BookService($this->repository->reveal());
$this->service = new BookService(
$this->repository->reveal(),
$this->paginator
);
}

/** @test */
Expand Down