From 6bdae4258239b4fee894b606a3b0e38d2d1eefe3 Mon Sep 17 00:00:00 2001 From: John Quairia Date: Thu, 4 Jun 2026 17:22:31 +0200 Subject: [PATCH] feature: filter deck by card reference --- src/Controller/PublicDeckController.php | 5 +++-- src/Repository/DeckRepository.php | 16 +++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Controller/PublicDeckController.php b/src/Controller/PublicDeckController.php index cc07e64..97e984c 100644 --- a/src/Controller/PublicDeckController.php +++ b/src/Controller/PublicDeckController.php @@ -35,6 +35,7 @@ public function __invoke(Request $request): JsonResponse $itemsPerPage = min(1000, max(1, (int) $request->query->get('itemsPerPage', 30))); $hero = $request->query->get('hero') ?: null; $cardName = $request->query->get('cardName') ?: null; + $cardRef = $request->query->get('cardRef') ?: null; $name = $request->query->get('name') ?: null; $faction = $request->query->get('faction') ?: null; $format = $request->query->get('format') ?: null; @@ -45,8 +46,8 @@ public function __invoke(Request $request): JsonResponse default => 'created_at', }; - $decks = $this->deckRepository->findPublic($page, $itemsPerPage, $hero, $cardName, $orderBy, $faction, $name, $format); - $total = $this->deckRepository->countPublic($hero, $cardName, $faction, $name, $format); + $decks = $this->deckRepository->findPublic($page, $itemsPerPage, $hero, $cardName, $orderBy, $faction, $name, $format, $cardRef); + $total = $this->deckRepository->countPublic($hero, $cardName, $faction, $name, $format, $cardRef); /** @var array> $data */ $data = $this->serializer->normalize($decks, 'json', ['groups' => ['deck:read']]) ?? []; diff --git a/src/Repository/DeckRepository.php b/src/Repository/DeckRepository.php index 46c7719..e1f44d1 100644 --- a/src/Repository/DeckRepository.php +++ b/src/Repository/DeckRepository.php @@ -73,12 +73,12 @@ public function findBatchWithCards(int $offset, int $limit): array ->getResult(); } - public function findPublic(int $page, int $itemsPerPage, ?string $hero = null, ?string $cardName = null, string $orderBy = 'created_at', ?string $faction = null, ?string $name = null, ?string $format = null): array + public function findPublic(int $page, int $itemsPerPage, ?string $hero = null, ?string $cardName = null, string $orderBy = 'created_at', ?string $faction = null, ?string $name = null, ?string $format = null, ?string $cardRef = null): array { $rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata(Deck::class, 'd'); - [$join, $where, $params] = $this->buildPublicFilters($hero, $cardName, $faction, $name, $format); + [$join, $where, $params] = $this->buildPublicFilters($hero, $cardName, $faction, $name, $format, $cardRef); $allowedOrderBy = ['created_at', 'upvote_count', 'view_count']; $col = in_array($orderBy, $allowedOrderBy, true) ? $orderBy : 'created_at'; @@ -100,9 +100,9 @@ public function findPublic(int $page, int $itemsPerPage, ?string $hero = null, ? return $query->getResult(); } - public function countPublic(?string $hero = null, ?string $cardName = null, ?string $faction = null, ?string $name = null, ?string $format = null): int + public function countPublic(?string $hero = null, ?string $cardName = null, ?string $faction = null, ?string $name = null, ?string $format = null, ?string $cardRef = null): int { - [$join, $where, $params] = $this->buildPublicFilters($hero, $cardName, $faction, $name, $format); + [$join, $where, $params] = $this->buildPublicFilters($hero, $cardName, $faction, $name, $format, $cardRef); return (int) $this->getEntityManager()->getConnection()->fetchOne( "SELECT COUNT(DISTINCT d.id) FROM deck d {$join} WHERE {$where}", @@ -113,7 +113,7 @@ public function countPublic(?string $hero = null, ?string $cardName = null, ?str /** * @return array{0: string, 1: string, 2: array} */ - private function buildPublicFilters(?string $hero, ?string $cardName, ?string $faction = null, ?string $name = null, ?string $format = null): array + private function buildPublicFilters(?string $hero, ?string $cardName, ?string $faction = null, ?string $name = null, ?string $format = null, ?string $cardRef = null): array { $where = 'd.is_public = true AND d.is_draft = false'; $join = ''; @@ -140,6 +140,12 @@ private function buildPublicFilters(?string $hero, ?string $cardName, ?string $f $params['cardName'] = '%'.$cardName.'%'; } + if (null !== $cardRef) { + $join = 'JOIN deck_card dc ON dc.deck_id = d.id'; + $where .= ' AND dc.cardReference = :cardRef'; + $params['cardRef'] = $cardRef; + } + if (null !== $name) { $where .= ' AND d.name LIKE :name'; $params['name'] = '%'.$name.'%';