Commit 5f6dc020 authored by i.vasilenko@iq-adv.ru's avatar i.vasilenko@iq-adv.ru
Browse files

Merge branch 'pagination' into 'main'

quests pagination

See merge request !10
parents bb0a2f33 87070608
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -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(
+30 −0
Original line number Diff line number Diff line
@@ -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);
+17 −2
Original line number Diff line number Diff line
@@ -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('Пользователь не сохранен');
        }
+1 −9
Original line number Diff line number Diff line
@@ -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('Ошибка сохранения фильтра');
        }
+14 −0
Original line number Diff line number Diff line
<?php

namespace App\Service\Dto\Classes;

use App\Service\Dto\BaseDto;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;

#[AsAlias(id: 'dto.page', public: true)]
class PageDto extends BaseDto
{
    public ?int $page = 1;

    public ?int $limit = 10;
}
 No newline at end of file
Loading