behaviour of /search path

add exception catching and message
create tests

refs #223 refs #377
This commit is contained in:
2015-01-04 00:13:00 +01:00
parent eea91de0e0
commit a6e118f37d
8 changed files with 223 additions and 7 deletions

View File

@@ -24,6 +24,9 @@ namespace Chill\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Chill\MainBundle\Search\UnknowSearchDomainException;
use Chill\MainBundle\Search\UnknowSearchNameException;
use Chill\MainBundle\Search\ParsingException;
/**
*
@@ -31,16 +34,52 @@ use Symfony\Component\HttpFoundation\Request;
* @author julien.fastre@champs-libres.coop
* @author marc@champs-libres.coop
*/
class SearchController extends Controller
{
public function searchAction(Request $request)
{
$results = $this->get('chill.main.search_provider')
->getResults($request->query->get('q'));
$pattern = $request->query->get('q', '');
if ($pattern === ''){
return $this->render('ChillMainBundle:Search:error.html.twig',
array(
'message' => $this->get('translator')->trans("Your search is empty. "
. "Please provide search terms.")
));
}
$name = $request->query->get('name', NULL);
try {
if ($name === NULL) {
$results = $this->get('chill.main.search_provider')
->getSearchResults($request->query->get('q'));
} else {
$results = [$this->get('chill.main.search_provider')
->getResultByName($pattern, $name)];
}
} catch (UnknowSearchDomainException $ex) {
return $this->render('ChillMainBundle:Search:error.html.twig',
array(
"message" => $this->get('translator')->trans("The domain %domain% "
. "is unknow. Please check your search.", array('%domain%' => $ex->getDomain()))
));
} catch (UnknowSearchNameException $ex) {
throw $this->createNotFoundException("The name ".$ex->getName()." is not found");
} catch (ParsingException $ex) {
return $this->render('ChillMainBundle:Search:error.html.twig',
array(
"message" => $this->get('translator')->trans('Invalid terms').
": ".$this->get('translator')->trans($ex->getMessage())
));
}
return $this->render('ChillMainBundle:Search:list.html.twig',
array('results' => $results)
);
}
}