From 870706086cfd0497112ab8c8ea5ab747922c24ad Mon Sep 17 00:00:00 2001 From: Ilya Vasilenko Date: Thu, 25 Jul 2024 16:34:03 +0500 Subject: [PATCH] quests pagination --- app/src/Controller/QuestController.php | 10 ++++-- app/src/Repository/QuestRepository.php | 30 ++++++++++++++++ app/src/Service/Action/Classes/GetQuests.php | 19 ++++++++-- app/src/Service/Action/Classes/SetFilter.php | 10 +----- app/src/Service/Dto/Classes/PageDto.php | 14 ++++++++ .../Response/Classes/Data/Pagination.php | 24 +++++++++++++ .../Response/Classes/QuestsPagedResponse.php | 35 +++++++++++++++++++ 7 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 app/src/Service/Dto/Classes/PageDto.php create mode 100644 app/src/Service/Response/Classes/Data/Pagination.php create mode 100644 app/src/Service/Response/Classes/QuestsPagedResponse.php diff --git a/app/src/Controller/QuestController.php b/app/src/Controller/QuestController.php index 4c8b52d..50916c7 100644 --- a/app/src/Controller/QuestController.php +++ b/app/src/Controller/QuestController.php @@ -6,6 +6,7 @@ use App\Service\Action\ActionServiceInterface; use App\Service\Dto\Classes\CreateReviewDto; use App\Service\Dto\Classes\FilterDto; use App\Service\Dto\Classes\IdDto; +use App\Service\Dto\Classes\PageDto; use App\Service\Dto\Classes\UpdateReviewDto; use App\Service\Response\Classes\FilterParamsResponse; use App\Service\Response\Classes\FilterResponse; @@ -23,12 +24,15 @@ use OpenApi\Attributes as OA; #[OA\Tag(name: 'Квесты')] class QuestController extends AbstractController { - #[Route('/quests', name: 'quests', methods: ['GET'])] + #[Route('/quests', name: 'quests', methods: ['POST'])] + #[OA\RequestBody( + content: new OA\JsonContent(ref: new Model(type: PageDto::class)) + )] #[OA\Response( response: 200, description: 'Ответ', content: new OA\JsonContent( - ref: new Model(type: QuestsResponse::class, groups: ["message", "data", "card"]) + ref: new Model(type: QuestsResponse::class, groups: ["message", "data", "card", "pagination"]) ) )] public function quests( @@ -66,7 +70,7 @@ class QuestController extends AbstractController response: 200, description: 'Ответ', content: new OA\JsonContent( - ref: new Model(type: QuestsResponse::class, groups: ["message", "data", "card"]) + ref: new Model(type: Response::class, groups: ["message"]) ) )] public function setFilter( diff --git a/app/src/Repository/QuestRepository.php b/app/src/Repository/QuestRepository.php index 5b2bd7a..c37ec84 100644 --- a/app/src/Repository/QuestRepository.php +++ b/app/src/Repository/QuestRepository.php @@ -5,9 +5,11 @@ namespace App\Repository; use App\Entity\Quest; use App\Redis\RedisFilter; use App\Service\Dto\Classes\FilterDto; +use App\Service\Response\Classes\Data\Pagination; use DateInterval; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\Persistence\ManagerRegistry; /** @@ -36,6 +38,34 @@ class QuestRepository extends ServiceEntityRepository return $queryBuilder->getQuery()->getResult(); } + public function findAllByFilterPaged(int $userId, int $page = 1, int $limit = 10): Pagination + { + $queryBuilder = $this->getFilterQuery($userId); + + $paginator = new Paginator($queryBuilder->getQuery()); + + $result = new Pagination(); + + $maxCount = count($paginator); + + $maxPage = ceil($maxCount / $limit); + + if ($page > $maxPage) { + $page = (int)$maxPage; + } + $result->page = $page; + $result->maxPage = $maxPage; + $result->limit = $limit; + + $paginator->getQuery() + ->setFirstResult(($page - 1) * $limit) + ->setMaxResults($limit); + + $result->quests = $paginator->getQuery()->getResult(); + + return $result; + } + public function findCompletedByFilter(int $userId): array { $queryBuilder = $this->getFilterQuery($userId); diff --git a/app/src/Service/Action/Classes/GetQuests.php b/app/src/Service/Action/Classes/GetQuests.php index 5abf867..274eeb3 100644 --- a/app/src/Service/Action/Classes/GetQuests.php +++ b/app/src/Service/Action/Classes/GetQuests.php @@ -4,6 +4,8 @@ namespace App\Service\Action\Classes; use App\Entity\Quest; use App\Service\Action\UserBaseActionService; +use App\Service\Dto\Classes\PageDto; +use App\Service\Dto\DtoServiceInterface; use App\Service\Response\ResponseServiceInterface; use Symfony\Component\DependencyInjection\Attribute\AsAlias; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -13,18 +15,31 @@ use Symfony\Contracts\Service\Attribute\Required; class GetQuests extends UserBaseActionService { #[Required] public function initResponse( - #[Autowire(service: 'response.quests')] + #[Autowire(service: 'response.quests.page')] ResponseServiceInterface $responseService ): void { parent::initResponse($responseService); } + #[Required] public function initDto( + #[Autowire(service: 'dto.page')] + DtoServiceInterface $dtoService + ): void + { + parent::initDto($dtoService); + } public function runAction(): void { + /** @var PageDto $dto */ + $dto = $this->getDto(); if ($userId = $this->user->getId()) { - $this->responseService->setData($this->doctrine->getRepository(Quest::class)->findAllByFilter($userId)); + if ($dto) { + $this->responseService->setData($this->doctrine->getRepository(Quest::class)->findAllByFilterPaged($userId, $dto->page, $dto->limit)); + } else { + $this->responseService->setData($this->doctrine->getRepository(Quest::class)->findAllByFilterPaged($userId)); + } } else { $this->responseService->addError('Пользователь не сохранен'); } diff --git a/app/src/Service/Action/Classes/SetFilter.php b/app/src/Service/Action/Classes/SetFilter.php index 45803cc..ed9d4c6 100644 --- a/app/src/Service/Action/Classes/SetFilter.php +++ b/app/src/Service/Action/Classes/SetFilter.php @@ -23,21 +23,13 @@ class SetFilter extends UserBaseActionService parent::initDto($dtoService); } - #[Required] public function initResponse( - #[Autowire(service: 'response.quests')] - ResponseServiceInterface $responseService - ): void - { - parent::initResponse($responseService); - } - public function runAction(): void { $dto = $this->getDto(); if ($this->user->getId() && $dto) { $redisFilter = new RedisFilter($this->user->getId()); $redisFilter->set($dto); - $this->responseService->setData($this->doctrine->getRepository(Quest::class)->findAllByFilter($this->user->getId()) ?: []); + $this->responseService->addMessage('Фильтр установлен'); } else { $this->responseService->addError('Ошибка сохранения фильтра'); } diff --git a/app/src/Service/Dto/Classes/PageDto.php b/app/src/Service/Dto/Classes/PageDto.php new file mode 100644 index 0000000..601192d --- /dev/null +++ b/app/src/Service/Dto/Classes/PageDto.php @@ -0,0 +1,14 @@ +data = $questList; + + return $this; + } + + public function getGroups(): array + { + return ['card', 'pagination']; + } +} \ No newline at end of file -- GitLab