mirror of
https://github.com/ToxicCrack/PrintABrick.git
synced 2025-06-01 11:40:20 -07:00
Add SearchService
This commit is contained in:
parent
fff7139992
commit
3ec5d11782
@ -90,3 +90,16 @@ services:
|
|||||||
AppBundle\Twig\AppExtension:
|
AppBundle\Twig\AppExtension:
|
||||||
tags:
|
tags:
|
||||||
- { name: twig.extension }
|
- { name: twig.extension }
|
||||||
|
|
||||||
|
League\Flysystem\FilesystemInterface:
|
||||||
|
alias: oneup_flysystem.media_filesystem
|
||||||
|
|
||||||
|
Knp\Menu\FactoryInterface:
|
||||||
|
alias: knp_menu.factory
|
||||||
|
|
||||||
|
FOS\ElasticaBundle\Manager\RepositoryManagerInterface:
|
||||||
|
alias: fos_elastica.repository_manager
|
||||||
|
|
||||||
|
AppBundle\Service\SearchService:
|
||||||
|
arguments:
|
||||||
|
- '@fos_elastica.manager'
|
@ -3,6 +3,7 @@
|
|||||||
namespace AppBundle\Controller;
|
namespace AppBundle\Controller;
|
||||||
|
|
||||||
use League\Flysystem\Filesystem;
|
use League\Flysystem\Filesystem;
|
||||||
|
use League\Flysystem\FilesystemInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||||
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
|
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
|
||||||
@ -20,7 +21,7 @@ class MediaController extends Controller
|
|||||||
*
|
*
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function fileAction($path, Filesystem $mediaFilesystem)
|
public function fileAction($path, FilesystemInterface $mediaFilesystem)
|
||||||
{
|
{
|
||||||
if ($mediaFilesystem->has($path)) {
|
if ($mediaFilesystem->has($path)) {
|
||||||
$response = new BinaryFileResponse($mediaFilesystem->getAdapter()->getPathPrefix().DIRECTORY_SEPARATOR.$path);
|
$response = new BinaryFileResponse($mediaFilesystem->getAdapter()->getPathPrefix().DIRECTORY_SEPARATOR.$path);
|
||||||
|
@ -5,7 +5,9 @@ namespace AppBundle\Controller;
|
|||||||
use AppBundle\Entity\LDraw\Model;
|
use AppBundle\Entity\LDraw\Model;
|
||||||
use AppBundle\Form\Search\ModelSearchType;
|
use AppBundle\Form\Search\ModelSearchType;
|
||||||
use AppBundle\Model\ModelSearch;
|
use AppBundle\Model\ModelSearch;
|
||||||
|
use AppBundle\Model\SetSearch;
|
||||||
use AppBundle\Service\ModelService;
|
use AppBundle\Service\ModelService;
|
||||||
|
use AppBundle\Service\SearchService;
|
||||||
use AppBundle\Service\SetService;
|
use AppBundle\Service\SetService;
|
||||||
use AppBundle\Service\ZipService;
|
use AppBundle\Service\ZipService;
|
||||||
use Knp\Component\Pager\Paginator;
|
use Knp\Component\Pager\Paginator;
|
||||||
@ -30,20 +32,17 @@ class ModelController extends Controller
|
|||||||
*
|
*
|
||||||
* @Route("/", name="model_index")
|
* @Route("/", name="model_index")
|
||||||
*/
|
*/
|
||||||
public function indexAction(Request $request, FormFactoryInterface $formFactory)
|
public function indexAction(Request $request, FormFactoryInterface $formFactory, SearchService $searchService)
|
||||||
{
|
{
|
||||||
$modelSearch = new ModelSearch();
|
$modelSearch = new ModelSearch();
|
||||||
|
|
||||||
$form = $formFactory->createNamedBuilder('', ModelSearchType::class, $modelSearch)->getForm();
|
$form = $formFactory->createNamedBuilder('', ModelSearchType::class, $modelSearch)->getForm();
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
$elasticaManager = $this->get('fos_elastica.manager');
|
|
||||||
$results = $elasticaManager->getRepository(Model::class)->search($modelSearch, 500);
|
|
||||||
|
|
||||||
/** @var Paginator $paginator */
|
/** @var Paginator $paginator */
|
||||||
$paginator = $this->get('knp_paginator');
|
$paginator = $this->get('knp_paginator');
|
||||||
$models = $paginator->paginate(
|
$models = $paginator->paginate(
|
||||||
$results,
|
$searchService->searchModels($modelSearch),
|
||||||
$request->query->getInt('page', 1)/*page number*/,
|
$request->query->getInt('page', 1)/*page number*/,
|
||||||
$request->query->getInt('limit', 30)/*limit per page*/
|
$request->query->getInt('limit', 30)/*limit per page*/
|
||||||
);
|
);
|
||||||
@ -60,7 +59,7 @@ class ModelController extends Controller
|
|||||||
* @Route("/{id}", name="model_detail")
|
* @Route("/{id}", name="model_detail")
|
||||||
* @Method("GET")
|
* @Method("GET")
|
||||||
*/
|
*/
|
||||||
public function detailAction($id, ModelService $modelService)
|
public function detailAction($id, ModelService $modelService, SetService $setService)
|
||||||
{
|
{
|
||||||
if ($model = $modelService->findModel($id)) {
|
if ($model = $modelService->findModel($id)) {
|
||||||
try {
|
try {
|
||||||
@ -68,6 +67,7 @@ class ModelController extends Controller
|
|||||||
'model' => $model,
|
'model' => $model,
|
||||||
'siblings' => $modelService->getSiblings($model),
|
'siblings' => $modelService->getSiblings($model),
|
||||||
'submodels' => $modelService->getSubmodels($model),
|
'submodels' => $modelService->getSubmodels($model),
|
||||||
|
'setCount' => count($setService->getAllByModel($model)),
|
||||||
]);
|
]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->addFlash('error', $e->getMessage());
|
$this->addFlash('error', $e->getMessage());
|
||||||
|
@ -24,7 +24,7 @@ class PartController extends Controller
|
|||||||
*
|
*
|
||||||
* @Route("/{id}", name="part_detail")
|
* @Route("/{id}", name="part_detail")
|
||||||
*/
|
*/
|
||||||
public function detailAction(Part $part, RebrickableManager $rebrickableManager)
|
public function detailAction(Part $part, RebrickableManager $rebrickableManager, SetService $setService)
|
||||||
{
|
{
|
||||||
$apiPart = null;
|
$apiPart = null;
|
||||||
if ($part) {
|
if ($part) {
|
||||||
@ -43,6 +43,7 @@ class PartController extends Controller
|
|||||||
return $this->render('part/detail.html.twig', [
|
return $this->render('part/detail.html.twig', [
|
||||||
'part' => $part,
|
'part' => $part,
|
||||||
'apiPart' => $apiPart,
|
'apiPart' => $apiPart,
|
||||||
|
'setCount' => count($setService->getAllByPart($part))
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ use AppBundle\Model\ModelSearch;
|
|||||||
use AppBundle\Model\SetSearch;
|
use AppBundle\Model\SetSearch;
|
||||||
use AppBundle\Repository\Search\ModelRepository;
|
use AppBundle\Repository\Search\ModelRepository;
|
||||||
use AppBundle\Repository\Search\SetRepository;
|
use AppBundle\Repository\Search\SetRepository;
|
||||||
|
use AppBundle\Service\SearchService;
|
||||||
use FOS\ElasticaBundle\HybridResult;
|
use FOS\ElasticaBundle\HybridResult;
|
||||||
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||||
@ -25,24 +26,13 @@ class SearchController extends Controller
|
|||||||
/**
|
/**
|
||||||
* @Route("/", name="search_results")
|
* @Route("/", name="search_results")
|
||||||
*/
|
*/
|
||||||
public function searchAction(Request $request)
|
public function searchAction(Request $request, SearchService $searchService)
|
||||||
{
|
{
|
||||||
$query = trim(strip_tags($request->get('query')));
|
$query = trim(strip_tags($request->get('query')));
|
||||||
|
|
||||||
/** var FOS\ElasticaBundle\Manager\RepositoryManager */
|
|
||||||
$repositoryManager = $this->get('fos_elastica.manager');
|
|
||||||
|
|
||||||
/** @var SetRepository $setRepository */
|
|
||||||
$setRepository = $repositoryManager->getRepository(Set::class);
|
|
||||||
/** @var ModelRepository $modelRepository */
|
|
||||||
$modelRepository = $repositoryManager->getRepository(Model::class);
|
|
||||||
|
|
||||||
$setsResult = $setRepository->search(new SetSearch($query), 1000);
|
|
||||||
$modelResult = $modelRepository->search(new ModelSearch($query), 1000);
|
|
||||||
|
|
||||||
return $this->render('search/index.html.twig', [
|
return $this->render('search/index.html.twig', [
|
||||||
'sets' => $setsResult,
|
'sets' => $searchService->searchSetsByQuery($query),
|
||||||
'models' => $modelResult,
|
'models' => $searchService->searchModelsByQuery($query),
|
||||||
'query' => $query,
|
'query' => $query,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@ -50,24 +40,15 @@ class SearchController extends Controller
|
|||||||
/**
|
/**
|
||||||
* @Route("/autocomplete", name="search_autocomplete")
|
* @Route("/autocomplete", name="search_autocomplete")
|
||||||
*/
|
*/
|
||||||
public function autocompleteAction(Request $request)
|
public function autocompleteAction(Request $request, SearchService $searchService)
|
||||||
{
|
{
|
||||||
$query = trim(strip_tags($request->get('query')));
|
$query = trim(strip_tags($request->get('query')));
|
||||||
|
|
||||||
/** @var CacheManager $liip */
|
/** @var CacheManager $liip */
|
||||||
$liip = $this->get('liip_imagine.cache.manager');
|
$liip = $this->get('liip_imagine.cache.manager');
|
||||||
|
|
||||||
/** var FOS\ElasticaBundle\Manager\RepositoryManager */
|
$setsResult = $searchService->searchSetsHighlightedByQuery($query, 4);
|
||||||
$repositoryManager = $this->get('fos_elastica.manager');
|
$modelResult = $searchService->searchModelsHighlightedByQuery($query, 4);
|
||||||
|
|
||||||
/** @var SetRepository $setRepository */
|
|
||||||
$setRepository = $repositoryManager->getRepository(Set::class);
|
|
||||||
/** @var ModelRepository $modelRepository */
|
|
||||||
$modelRepository = $repositoryManager->getRepository(Model::class);
|
|
||||||
|
|
||||||
// Option 1. Returns all users who have example.net in any of their mapped fields
|
|
||||||
$setsResult = $setRepository->findHighlighted($query, 4);
|
|
||||||
$modelResult = $modelRepository->findHighlighted($query, 4);
|
|
||||||
|
|
||||||
$models = [];
|
$models = [];
|
||||||
/** @var HybridResult $model */
|
/** @var HybridResult $model */
|
||||||
|
@ -8,6 +8,7 @@ use AppBundle\Entity\Rebrickable\Set;
|
|||||||
use AppBundle\Form\Search\SetSearchType;
|
use AppBundle\Form\Search\SetSearchType;
|
||||||
use AppBundle\Model\SetSearch;
|
use AppBundle\Model\SetSearch;
|
||||||
use AppBundle\Repository\Search\SetRepository;
|
use AppBundle\Repository\Search\SetRepository;
|
||||||
|
use AppBundle\Service\SearchService;
|
||||||
use AppBundle\Service\SetService;
|
use AppBundle\Service\SetService;
|
||||||
use AppBundle\Service\ZipService;
|
use AppBundle\Service\ZipService;
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||||
@ -30,20 +31,16 @@ class SetController extends Controller
|
|||||||
*
|
*
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function indexAction(Request $request, FormFactoryInterface $formFactory)
|
public function indexAction(Request $request, FormFactoryInterface $formFactory, SearchService $searchService)
|
||||||
{
|
{
|
||||||
$setSearch = new SetSearch();
|
$setSearch = new SetSearch();
|
||||||
|
|
||||||
$form = $formFactory->createNamedBuilder('', SetSearchType::class, $setSearch)->getForm();
|
$form = $formFactory->createNamedBuilder('', SetSearchType::class, $setSearch)->getForm();
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
/** @var SetRepository $setRepository */
|
|
||||||
$setRepository = $this->get('fos_elastica.manager')->getRepository(Set::class);
|
|
||||||
$results = $setRepository->search($setSearch, 500);
|
|
||||||
|
|
||||||
$paginator = $this->get('knp_paginator');
|
$paginator = $this->get('knp_paginator');
|
||||||
$sets = $paginator->paginate(
|
$sets = $paginator->paginate(
|
||||||
$results,
|
$searchService->searchSets($setSearch, 500),
|
||||||
$request->query->getInt('page', 1)/*page number*/,
|
$request->query->getInt('page', 1)/*page number*/,
|
||||||
$request->query->getInt('limit', 20)/*limit per page*/
|
$request->query->getInt('limit', 20)/*limit per page*/
|
||||||
);
|
);
|
||||||
|
99
src/AppBundle/Service/SearchService.php
Normal file
99
src/AppBundle/Service/SearchService.php
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Service;
|
||||||
|
|
||||||
|
|
||||||
|
use AppBundle\Entity\LDraw\Model;
|
||||||
|
use AppBundle\Entity\Rebrickable\Set;
|
||||||
|
use AppBundle\Model\ModelSearch;
|
||||||
|
use AppBundle\Model\SetSearch;
|
||||||
|
use AppBundle\Repository\Search\ModelRepository;
|
||||||
|
use AppBundle\Repository\Search\SetRepository;
|
||||||
|
use FOS\ElasticaBundle\HybridResult;
|
||||||
|
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
|
||||||
|
|
||||||
|
class SearchService
|
||||||
|
{
|
||||||
|
/** @var ModelRepository */
|
||||||
|
private $modelRepository;
|
||||||
|
|
||||||
|
/** @var SetRepository */
|
||||||
|
private $setRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SearchService constructor.
|
||||||
|
* @param RepositoryManagerInterface $repositoryManager
|
||||||
|
*/
|
||||||
|
public function __construct(RepositoryManagerInterface $repositoryManager)
|
||||||
|
{
|
||||||
|
$this->modelRepository = $repositoryManager->getRepository(Model::class);
|
||||||
|
$this->setRepository = $repositoryManager->getRepository(Set::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find matching sets by query
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @param int $limit
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function searchSetsByQuery($query, $limit = 1000) {
|
||||||
|
return $this->setRepository->search(new SetSearch($query), $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find matching sets by query with highlights
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @param int $limit
|
||||||
|
* @return HybridResult[]
|
||||||
|
*/
|
||||||
|
public function searchSetsHighlightedByQuery($query, $limit = 4) {
|
||||||
|
return $this->setRepository->findHighlighted($query, $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find matching sets by rules in SetSearch class
|
||||||
|
*
|
||||||
|
* @param SetSearch $setSearch
|
||||||
|
* @param int $limit
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function searchSets(SetSearch $setSearch, $limit = 1000) {
|
||||||
|
return $this->setRepository->search($setSearch, $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find matching models by query
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @param int $limit
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function searchModelsByQuery($query, $limit = 1000) {
|
||||||
|
return $this->modelRepository->search(new ModelSearch($query), $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find matching models by query with highlights
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @param int $limit
|
||||||
|
* @return HybridResult[]
|
||||||
|
*/
|
||||||
|
public function searchModelsHighlightedByQuery($query, $limit = 4) {
|
||||||
|
return $this->modelRepository->findHighlighted($query, $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find matching models by rules in ModelSearch class
|
||||||
|
*
|
||||||
|
* @param ModelSearch $modelSearch
|
||||||
|
* @param int $limit
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function searchModels(ModelSearch $modelSearch, $limit = 1000) {
|
||||||
|
return $this->modelRepository->search($modelSearch, $limit);
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ namespace AppBundle\Service;
|
|||||||
use AppBundle\Entity\LDraw\Model;
|
use AppBundle\Entity\LDraw\Model;
|
||||||
use AppBundle\Entity\Rebrickable\Set;
|
use AppBundle\Entity\Rebrickable\Set;
|
||||||
use League\Flysystem\Filesystem;
|
use League\Flysystem\Filesystem;
|
||||||
|
use League\Flysystem\FilesystemInterface;
|
||||||
|
|
||||||
class ZipService
|
class ZipService
|
||||||
{
|
{
|
||||||
@ -29,11 +30,11 @@ class ZipService
|
|||||||
/**
|
/**
|
||||||
* ZipService constructor.
|
* ZipService constructor.
|
||||||
*
|
*
|
||||||
* @param Filesystem $mediaFilesystem
|
* @param FilesystemInterface $mediaFilesystem
|
||||||
* @param ModelService $modelService
|
* @param ModelService $modelService
|
||||||
* @param SetService $setService
|
* @param SetService $setService
|
||||||
*/
|
*/
|
||||||
public function __construct(Filesystem $mediaFilesystem, ModelService $modelService, SetService $setService)
|
public function __construct(FilesystemInterface $mediaFilesystem, ModelService $modelService, SetService $setService)
|
||||||
{
|
{
|
||||||
$this->mediaFilesystem = $mediaFilesystem;
|
$this->mediaFilesystem = $mediaFilesystem;
|
||||||
$this->setService = $setService;
|
$this->setService = $setService;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user