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

#46 => done

parent f4aa15c2
Pipeline #2198 passed with stages
in 3 minutes and 18 seconds
......@@ -74,7 +74,7 @@ final class SearchAction extends AbstractAction
if (is_null($dataset)) {
throw new HttpNotFoundException(
$request,
'Dataset with name ' . $args['name'] . ' is not found'
'Dataset with name ' . $args['dname'] . ' is not found'
);
}
......@@ -83,53 +83,56 @@ final class SearchAction extends AbstractAction
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->from($dataset->getTableRef());
if ($request->getMethod() === GET) {
$queryParams = $request->getQueryParams();
$queryParams = $request->getQueryParams();
// The parameter "a" is mandatory
if (!array_key_exists('a', $queryParams)) {
throw new HttpBadRequestException(
$request,
'Param a is required for this request'
);
// The parameter "a" is mandatory
if (!array_key_exists('a', $queryParams)) {
throw new HttpBadRequestException(
$request,
'Param a is required for this request'
);
}
try {
// The parameter a represents the SQL select clause
if ($queryParams['a'] === 'count') {
$attributes = array();
$queryBuilder->select('COUNT(*) as nb');
} else {
$attributes = $this->select($queryBuilder, $dataset, explode(';', $queryParams['a']));
}
try {
// The parameter a represents the SQL select clause
if ($queryParams['a'] === 'count') {
$attributes = array();
$queryBuilder->select('COUNT(*) as nb');
} else {
$attributes = $this->select($queryBuilder, $dataset, explode(';', $queryParams['a']));
}
// The parameter c is not mandatory and represents the SQL where clause
if (array_key_exists('c', $queryParams)) {
$this->where($queryBuilder, $dataset, explode(';', $queryParams['c']));
}
// The parameter c is not mandatory and represents the SQL where clause
if (array_key_exists('c', $queryParams)) {
$this->where($queryBuilder, $dataset, explode(';', $queryParams['c']));
}
// The parameter o is not mandatory and represents the SQL order clause
if (array_key_exists('o', $queryParams)) {
$this->order($queryBuilder, $dataset, explode(';', $queryParams['o']));
}
// The parameter o is not mandatory and represents the SQL order clause
if (array_key_exists('o', $queryParams)) {
$this->order($queryBuilder, $dataset, explode(';', $queryParams['o']));
}
// The parameter p is not mandatory and represents the SQL limit clause
if (array_key_exists('p', $queryParams)) {
$this->limit($queryBuilder, $queryParams['p']);
}
// The parameter p is not mandatory and represents the SQL limit clause
if (array_key_exists('p', $queryParams)) {
$this->limit($queryBuilder, $queryParams['p']);
}
} catch (SearchException $e) {
throw new HttpBadRequestException(
$request,
$e->getMessage()
);
// The parameter f is not mandatory and represents the output format
// By default the format is JSON
if (array_key_exists('f', $queryParams)) {
$format = $queryParams['f'];
} else {
$format = 'json';
}
$result = $this->fetchAll($queryBuilder, $attributes);
$payload = json_encode($result, JSON_UNESCAPED_SLASHES);
// Create the response according to the format
return $this->getFormattedResponse($response, $queryBuilder, $attributes, $format);
} catch (SearchException $e) {
throw new HttpBadRequestException(
$request,
$e->getMessage()
);
}
$response->getBody()->write($payload);
return $response;
}
/**
......@@ -246,11 +249,39 @@ final class SearchAction extends AbstractAction
throw SearchException::attributeNotFound($id, $dataset->getLabel());
}
private function fetchAll(QueryBuilder $queryBuilder, array $attributes): array
{
$jsonAttributes = $this->getAttributesOfTypeJson($attributes);
private function getFormattedResponse(
Response $response,
QueryBuilder $queryBuilder,
array $attributes,
string $format
): Response {
// First of all retrieve results from the database
$stmt = $queryBuilder->execute();
$rows = $stmt->fetchAll();
// Build and write the payload according to the format
// The return the response
switch ($format) {
case 'json':
$payload = json_encode($this->decodeNestedJson($rows, $attributes), JSON_UNESCAPED_SLASHES);
$response->getBody()->write($payload);
return $response;
case 'csv':
$payload = $this->transformArrayToCsv($rows, $attributes, ',');
$response->getBody()->write($payload);
return $response->withHeader('Content-Type', 'text/csv');
case 'ascii':
$payload = $this->transformArrayToCsv($rows, $attributes, ' ');
$response->getBody()->write($payload);
return $response->withHeader('Content-Type', 'text/plain');
default:
throw SearchException::typeOfFormatDoesNotExist($format);
}
}
private function decodeNestedJson(array $rows, array $attributes): array
{
$jsonAttributes = $this->getAttributesOfTypeJson($attributes);
foreach ($rows as &$row) {
foreach ($row as $key => &$column) {
if (array_search($key, $jsonAttributes)) {
......@@ -271,4 +302,16 @@ final class SearchAction extends AbstractAction
return $attribute->getType() === 'json';
}));
}
private function transformArrayToCsv(array $rows, array $attributes, string $delimiter): string
{
$attributesLabel = array_map(function ($attribute) {
return $attribute->getLabel();
}, $attributes);
$csv = implode($delimiter, $attributesLabel) . PHP_EOL;
foreach ($rows as $row) {
$csv .= implode($delimiter, $row) . PHP_EOL;
}
return $csv;
}
}
......@@ -86,4 +86,12 @@ class SearchException extends Exception
{
return new self("Limit (p) needs 2 params to work");
}
/**
* @return SearchException
*/
public static function typeOfFormatDoesNotExist(string $format): SearchException
{
return new self("The parameter (f) needs a supported format (json, csv, ascii)");
}
}
Markdown is supported
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