Extract searchEvents() to EventIndexService, reduce duplication across controllers
- Add searchEvents() method: Meilisearch search with DB fallback - Use in HomeController, AdminController, AccountController (removes ~45 duplicated lines) - Add assets/vendor/ to ESLint ignores - Update EventIndexServiceTest for new constructor Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,6 @@ export default [
|
||||
},
|
||||
},
|
||||
{
|
||||
ignores: ["node_modules/", "public/build/", "vendor/", "public/sw.js", "public/workbox/", "public/idb/"],
|
||||
ignores: ["node_modules/", "public/build/", "vendor/", "public/sw.js", "public/workbox/", "public/idb/", "assets/vendor/"],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Returns the importmap for this application.
|
||||
*
|
||||
* - "path" is a path inside the asset mapper system. Use the
|
||||
* "debug:asset-map" command to see the full list of paths.
|
||||
*
|
||||
* - "entrypoint" (JavaScript only) set to true for any module that will
|
||||
* be used as an "entrypoint" (and passed to the importmap() Twig function).
|
||||
*
|
||||
* The "importmap:require" command can be used to add new entries to this file.
|
||||
*/
|
||||
return [
|
||||
'app' => [
|
||||
'path' => './assets/app.js',
|
||||
'entrypoint' => true,
|
||||
],
|
||||
'@hotwired/stimulus' => [
|
||||
'version' => '3.2.2',
|
||||
],
|
||||
'idb' => [
|
||||
'version' => '8.0.3',
|
||||
],
|
||||
'idb-keyval' => [
|
||||
'version' => '6.2.2',
|
||||
],
|
||||
'@spomky-labs/pwa/helpers' => [
|
||||
'path' => './vendor/spomky-labs/pwa-bundle/assets/src/helpers.js',
|
||||
],
|
||||
];
|
||||
@@ -7,7 +7,6 @@ use App\Entity\Payout;
|
||||
use App\Entity\User;
|
||||
use App\Service\EventIndexService;
|
||||
use App\Service\MailerService;
|
||||
use App\Service\MeilisearchService;
|
||||
use App\Service\PayoutPdfService;
|
||||
use App\Service\StripeService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
@@ -26,7 +25,7 @@ class AccountController extends AbstractController
|
||||
private const BREADCRUMB_ACCOUNT = ['name' => 'Mon compte', 'url' => '/mon-compte'];
|
||||
|
||||
#[Route('/mon-compte', name: 'app_account')]
|
||||
public function index(Request $request, StripeService $stripeService, EntityManagerInterface $em, PaginatorInterface $paginator, MeilisearchService $meilisearch): Response
|
||||
public function index(Request $request, StripeService $stripeService, EntityManagerInterface $em, PaginatorInterface $paginator, EventIndexService $eventIndex): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
@@ -58,25 +57,7 @@ class AccountController extends AbstractController
|
||||
['createdAt' => 'DESC'],
|
||||
);
|
||||
$searchQuery = $request->query->getString('q', '');
|
||||
if ('' !== $searchQuery) {
|
||||
try {
|
||||
$searchResults = $meilisearch->search('event_'.$user->getId(), $searchQuery);
|
||||
$eventIds = array_map(fn (array $hit) => $hit['id'], $searchResults['hits'] ?? []);
|
||||
$eventsQuery = $eventIds
|
||||
? $em->getRepository(Event::class)->findBy(['id' => $eventIds])
|
||||
: [];
|
||||
} catch (\Throwable) {
|
||||
$eventsQuery = $em->getRepository(Event::class)->findBy(
|
||||
['account' => $user],
|
||||
['startAt' => 'ASC'],
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$eventsQuery = $em->getRepository(Event::class)->findBy(
|
||||
['account' => $user],
|
||||
['startAt' => 'ASC'],
|
||||
);
|
||||
}
|
||||
$eventsQuery = $eventIndex->searchEvents('event_'.$user->getId(), $searchQuery, ['account' => $user]);
|
||||
$events = $paginator->paginate($eventsQuery, $request->query->getInt('page', 1), 10);
|
||||
}
|
||||
|
||||
|
||||
@@ -430,23 +430,10 @@ class AdminController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/evenements', name: 'app_admin_events')]
|
||||
public function events(Request $request, EntityManagerInterface $em, PaginatorInterface $paginator, MeilisearchService $meilisearch): Response
|
||||
public function events(Request $request, PaginatorInterface $paginator, \App\Service\EventIndexService $eventIndex): Response
|
||||
{
|
||||
$searchQuery = $request->query->getString('q', '');
|
||||
|
||||
if ('' !== $searchQuery) {
|
||||
try {
|
||||
$searchResults = $meilisearch->search('event_admin', $searchQuery);
|
||||
$eventIds = array_map(fn (array $hit) => $hit['id'], $searchResults['hits'] ?? []);
|
||||
$eventsQuery = $eventIds
|
||||
? $em->getRepository(Event::class)->findBy(['id' => $eventIds])
|
||||
: [];
|
||||
} catch (\Throwable) {
|
||||
$eventsQuery = $em->getRepository(Event::class)->findBy([], ['startAt' => 'ASC']);
|
||||
}
|
||||
} else {
|
||||
$eventsQuery = $em->getRepository(Event::class)->findBy([], ['startAt' => 'ASC']);
|
||||
}
|
||||
$eventsQuery = $eventIndex->searchEvents('event_admin', $searchQuery);
|
||||
|
||||
$events = $paginator->paginate($eventsQuery, $request->query->getInt('page', 1), 10);
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Controller;
|
||||
|
||||
use App\Entity\Event;
|
||||
use App\Entity\User;
|
||||
use App\Service\EventIndexService;
|
||||
use App\Service\MailerService;
|
||||
use App\Service\MeilisearchService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
@@ -38,30 +39,10 @@ class HomeController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/evenements', name: 'app_events')]
|
||||
public function events(Request $request, EntityManagerInterface $em, PaginatorInterface $paginator, MeilisearchService $meilisearch): Response
|
||||
public function events(Request $request, PaginatorInterface $paginator, EventIndexService $eventIndex): Response
|
||||
{
|
||||
$searchQuery = $request->query->getString('q', '');
|
||||
|
||||
if ('' !== $searchQuery) {
|
||||
try {
|
||||
$searchResults = $meilisearch->search('event_global', $searchQuery);
|
||||
$eventIds = array_map(fn (array $hit) => $hit['id'], $searchResults['hits'] ?? []);
|
||||
$eventsQuery = $eventIds
|
||||
? $em->getRepository(Event::class)->findBy(['id' => $eventIds])
|
||||
: [];
|
||||
} catch (\Throwable) {
|
||||
$eventsQuery = $em->getRepository(Event::class)->findBy(
|
||||
['isOnline' => true, 'isSecret' => false],
|
||||
['startAt' => 'ASC'],
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$eventsQuery = $em->getRepository(Event::class)->findBy(
|
||||
['isOnline' => true, 'isSecret' => false],
|
||||
['startAt' => 'ASC'],
|
||||
);
|
||||
}
|
||||
|
||||
$eventsQuery = $eventIndex->searchEvents('event_global', $searchQuery, ['isOnline' => true, 'isSecret' => false]);
|
||||
$events = $paginator->paginate($eventsQuery, $request->query->getInt('page', 1), 12);
|
||||
|
||||
return $this->render('home/events.html.twig', [
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Event;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class EventIndexService
|
||||
{
|
||||
@@ -12,9 +13,31 @@ class EventIndexService
|
||||
|
||||
public function __construct(
|
||||
private readonly MeilisearchService $meilisearch,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $fallbackCriteria
|
||||
*
|
||||
* @return list<Event>|array<Event>
|
||||
*/
|
||||
public function searchEvents(string $index, string $query, array $fallbackCriteria = [], array $fallbackOrder = ['startAt' => 'ASC']): array
|
||||
{
|
||||
if ('' === $query) {
|
||||
return $this->em->getRepository(Event::class)->findBy($fallbackCriteria, $fallbackOrder);
|
||||
}
|
||||
|
||||
try {
|
||||
$results = $this->meilisearch->search($index, $query);
|
||||
$ids = array_map(fn (array $hit) => $hit['id'], $results['hits'] ?? []);
|
||||
|
||||
return $ids ? $this->em->getRepository(Event::class)->findBy(['id' => $ids]) : [];
|
||||
} catch (\Throwable) {
|
||||
return $this->em->getRepository(Event::class)->findBy($fallbackCriteria, $fallbackOrder);
|
||||
}
|
||||
}
|
||||
|
||||
public function indexEvent(Event $event): void
|
||||
{
|
||||
try {
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Entity\Event;
|
||||
use App\Entity\User;
|
||||
use App\Service\EventIndexService;
|
||||
use App\Service\MeilisearchService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class EventIndexServiceTest extends TestCase
|
||||
@@ -16,7 +17,8 @@ class EventIndexServiceTest extends TestCase
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->meilisearch = $this->createMock(MeilisearchService::class);
|
||||
$this->service = new EventIndexService($this->meilisearch);
|
||||
$em = $this->createMock(EntityManagerInterface::class);
|
||||
$this->service = new EventIndexService($this->meilisearch, $em);
|
||||
}
|
||||
|
||||
private function createEvent(bool $online = true, bool $secret = false): Event
|
||||
|
||||
Reference in New Issue
Block a user