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