Commit 70f7ed59 authored by François Agneray's avatar François Agneray
Browse files

Merge branch '28-quand-le-numero-d-attribute-n-existe-pas-envoyer-une-erreur-400' into 'develop'

#28 => done

Closes #28

See merge request !25
parents c038dea1 ce31e59d
Pipeline #1238 passed with stages
in 4 minutes and 2 seconds
......@@ -134,41 +134,49 @@ final class SearchAction
$queryBuilder->from($dataset->getTableRef());
if (array_key_exists('c', $queryParams)) {
$this->where($queryBuilder, $dataset, explode(';', $queryParams['c']));
}
$listOfIds = explode(';', $queryParams['a']);
if ($args['type'] === 'data') {
$this->select($queryBuilder, $dataset, $listOfIds);
try {
$searchType = $this->getAndVerifySearchType($args['type']);
if (array_key_exists('o', $queryParams)) {
$this->order($queryBuilder, $dataset, explode(';', $queryParams['o']));
if (array_key_exists('c', $queryParams)) {
$this->where($queryBuilder, $dataset, explode(';', $queryParams['c']));
}
if (array_key_exists('p', $queryParams)) {
$this->limit($queryBuilder, $queryParams['p']);
}
$result = $this->fetchAll($queryBuilder);
} else if ($args['type'] === 'meta') {
$queryBuilder->select('COUNT(*) as nb');
$count = $this->fetchAll($queryBuilder);
$result = array();
$result['dataset_selected'] = $dataset->getLabel();
$attributesSelected = array();
foreach ($listOfIds as $id) {
$attribute = $this->getAttribute($dataset, (int) $id);
$attributesSelected[] = array(
'name' => $attribute->getName(),
'label' => $attribute->getLabel()
);
$listOfIds = explode(';', $queryParams['a']);
if ($searchType === 'data') {
$this->select($queryBuilder, $dataset, $listOfIds);
if (array_key_exists('o', $queryParams)) {
$this->order($queryBuilder, $dataset, explode(';', $queryParams['o']));
}
if (array_key_exists('p', $queryParams)) {
$this->limit($queryBuilder, $queryParams['p']);
}
$result = $this->fetchAll($queryBuilder);
} else if ($searchType === 'meta') {
$queryBuilder->select('COUNT(*) as nb');
$count = $this->fetchAll($queryBuilder);
$result = array();
$result['dataset_selected'] = $dataset->getLabel();
$attributesSelected = array();
foreach ($listOfIds as $id) {
$attribute = $this->getAttribute($dataset, (int) $id);
$attributesSelected[] = array(
'name' => $attribute->getName(),
'label' => $attribute->getLabel()
);
}
$result['attributes_selected'] = $attributesSelected;
$result['total_items'] = $count[0]['nb'];
}
$result['attributes_selected'] = $attributesSelected;
$result['total_items'] = $count[0]['nb'];
} else {
throw SearchException::typeOfSearchDoesNotExist($args['type']);
} catch (SearchException $e) {
return $this->dispatchHttpError(
$response,
'Invalid request',
$e->getMessage()
)->withStatus(400);
}
$this->logger->info('SQL: ' . $queryBuilder->getSQL());
......@@ -176,6 +184,14 @@ final class SearchAction
return $response->withJson($result);
}
private function getAndVerifySearchType(string $type)
{
if ($type !== 'data' && $type !== 'meta') {
throw SearchException::typeOfSearchDoesNotExist($type);
}
return $type;
}
private function select(QueryBuilder $queryBuilder, Dataset $dataset, array $listOfIds): void
{
$columns = array();
......@@ -190,7 +206,7 @@ final class SearchAction
{
$expressions = array();
foreach ($criteria as $criterion) {
$params = $this->getCriterionParams($criterion);
$params = $params = explode(':', $criterion);
$attribute = $this->getAttribute($dataset, (int) $params[0]);
$column = $attribute->getTableName() . '.' . $attribute->getName();
$columnType = $attribute->getType();
......@@ -209,11 +225,16 @@ final class SearchAction
{
foreach ($orders as $order) {
$o = explode(':', $order);
if (count($o) != 2) {
throw SearchException::badNumberOfParamsForOrder();
}
$attribute = $this->getAttribute($dataset, (int) $o[0]);
if ($o[1] === 'a') {
$aord = 'ASC';
} else if ($o[1] === 'd') {
$aord = 'DESC';
} else {
throw SearchException::typeOfOrderDoesNotExist($o[1]);
}
$queryBuilder->orderBy($attribute->getTableName() . '.' . $attribute->getName(), $aord);
}
......@@ -222,6 +243,9 @@ final class SearchAction
private function limit(QueryBuilder $queryBuilder, string $param): void
{
$p = explode(':', $param);
if (count($p) != 2) {
throw SearchException::badNumberOfParamsForLimit();
}
$limit = $p[0];
$offset = ($p[1] - 1) * $limit;
$queryBuilder
......@@ -235,16 +259,6 @@ final class SearchAction
return $stmt->fetchAll();
}
private function getCriterionParams(string $criterion): array
{
$params = explode(':', $criterion);
$numberOfParams = count($params);
if ($numberOfParams < 2) {
throw SearchException::numberOfCriterionParams($numberOfParams, $criterion);
}
return $params;
}
private function getAttribute(Dataset $dataset, int $id): Attribute
{
$attributes = $dataset->getAttributes();
......
......@@ -10,9 +10,9 @@
*/
namespace App\Utils\Operator;
use Exception;
use App\Utils\SearchException;
class OperatorException extends Exception
class OperatorException extends SearchException
{
/**
* @param string $operator
......@@ -21,6 +21,16 @@ class OperatorException extends Exception
*/
public static function unknownOperator($operator)
{
return new self("The given operateor '" . $operator . "' is unknown");
return new self("The given operator '" . $operator . "' is unknown");
}
public static function operatorBadNumberOfParameters($operator, $numberOfParametersRequired)
{
return new self("Operator '" . $operator . "' needs " . $numberOfParametersRequired . ' values to work');
}
public static function inBadNumberOfParameters($operator)
{
return new self("Operator '" . $operator . "' needs at least 1 parameter to work");
}
}
......@@ -18,42 +18,81 @@ class OperatorFactory implements IOperatorFactory
{
switch ($type) {
case 'bw':
if (count($parameters) != 2) {
throw OperatorException::operatorBadNumberOfParameters($type, 2);
}
return new Between($expr, $column, $columnType, $parameters[0], $parameters[1]);
case 'eq':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new Equal($expr, $column, $columnType, $parameters[0]);
case 'neq':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new NotEqual($expr, $column, $columnType, $parameters[0]);
case 'gt':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new GreaterThan($expr, $column, $columnType, $parameters[0]);
case 'gte':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new GreaterThanEqual($expr, $column, $columnType, $parameters[0]);
case 'lt':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new LessThan($expr, $column, $columnType, $parameters[0]);
case 'lte':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new LessThanEqual($expr, $column, $columnType, $parameters[0]);
case 'lk':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new Like($expr, $column, $columnType, $parameters[0]);
case 'nlk':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new NotLike($expr, $column, $columnType, $parameters[0]);
case 'in':
if (count($parameters) < 1) {
throw OperatorException::inBadNumberOfParameters($type);
}
return new In($expr, $column, $columnType, $parameters);
case 'nin':
if (count($parameters) < 1) {
throw OperatorException::inBadNumberOfParameters($type);
}
return new NotIn($expr, $column, $columnType, $parameters);
case 'nl':
if (count($parameters) != 0) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new OperatorNull($expr, $column, $columnType);
case 'nnl':
if (count($parameters) != 0) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new OperatorNotNull($expr, $column, $columnType);
default:
......
......@@ -21,7 +21,7 @@ class SearchException extends Exception
*/
public static function paramDoesNotExist($param)
{
return new self("The param '" . $param . "' does not exists in the URL");
return new self("The param '" . $param . "' does not exist in the URL");
}
public static function numberOfCriterionParams($numberOfParams, $criterion)
......@@ -35,13 +35,23 @@ class SearchException extends Exception
return new self("Attribute with the id " . $id . " is not found for the dataset " . $datasetLabel);
}
public static function listOfIdsIsNotCorrect()
public static function typeOfSearchDoesNotExist($param)
{
return new self("List of ids is not correct");
return new self("The type of search '" . $param . "' does not exist");
}
public static function typeOfSearchDoesNotExist($param)
public static function badNumberOfParamsForOrder()
{
return new self("Order (o) needs 2 params to work");
}
public static function typeOfOrderDoesNotExist($order)
{
return new self("The type of order '" . $order . "' does not exist");
}
public static function badNumberOfParamsForLimit()
{
return new self("The type of search '" . $param . "' does not exists");
return new self("Limit (p) needs 2 params to work");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment