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

#45 => done

parent e655cb44
Pipeline #2163 passed with stages
in 3 minutes and 33 seconds
......@@ -148,6 +148,10 @@ $container->set('App\Action\AttributeAction', function (ContainerInterface $c) {
return new App\Action\AttributeAction($c->get('em'));
});
$container->set('App\Action\AttributeDistinctAction', function (ContainerInterface $c) {
return new App\Action\AttributeDistinctAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\SearchAction', function (ContainerInterface $c) {
return new App\Action\SearchAction(
$c->get('em'),
......
......@@ -34,4 +34,5 @@ $app->map([OPTIONS, GET, POST], '/output-family/{id}/output-category', App\Actio
$app->map([OPTIONS, GET, PUT, DELETE], '/output-category/{id}', App\Action\OutputCategoryAction::class);
$app->map([OPTIONS, GET], '/dataset/{name}/attribute', App\Action\AttributeListAction::class);
$app->map([OPTIONS, GET, PUT], '/dataset/{name}/attribute/{id}', App\Action\AttributeAction::class);
$app->map([OPTIONS, GET, PUT], '/dataset/{name}/attribute/{id}/distinct', App\Action\AttributeDistinctAction::class);
$app->get('/search/{dname}', App\Action\SearchAction::class);
......@@ -6,8 +6,8 @@ services:
working_dir: /project
environment:
docker: "true"
DISPLAY_ERROR_DETAILS: "false"
DATABASE_DEV_MODE: 0
DISPLAY_ERROR_DETAILS: "true"
DATABASE_DEV_MODE: 1
DATABASE_CO_DRIVER: "pdo_pgsql"
DATABASE_CO_HOST: "db"
DATABASE_CO_PORT: 5432
......
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpNotFoundException;
use Doctrine\ORM\EntityManagerInterface;
use App\Utils\DBALConnectionFactory;
use PDO;
final class AttributeDistinctAction extends AbstractAction
{
private $connectionFactory;
/**
* Create the classe before call __invoke to execute the action
*
* @param EntityManagerInterface $em Doctrine Entity Manager Interface
* @param DBALConnectionFactory $connectionFactory Factory used to construct connection to business database
*/
public function __construct(EntityManagerInterface $em, DBALConnectionFactory $connectionFactory)
{
parent::__construct($em);
$this->connectionFactory = $connectionFactory;
}
/**
* `GET` Returns a list of distinct values found for this attribute into the business database
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
}
$attribute = $this->em->getRepository('App\Entity\Attribute')->findOneBy(
array('dataset' => $args['name'], 'id' => $args['id'])
);
// If attribute is not found 404
if (is_null($attribute)) {
throw new HttpNotFoundException(
$request,
'Attribute with dataset name ' . $args['name'] . ' and attribute id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
// Create query builder with from clause using dataset information
// With select, group by and order by clauses using attribute information
$dataset = $attribute->getDataset();
$column = $attribute->getTableName() . '.' . $attribute->getName();
$connection = $this->connectionFactory->create($dataset->getProject()->getDatabase());
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->select($column . ' as ' . $attribute->getLabel());
$queryBuilder->from($dataset->getTableRef());
$queryBuilder->groupBy($column);
$queryBuilder->orderBy($column);
// Execute query and returns response
$stmt = $queryBuilder->execute();
$rows = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
$payload = json_encode($rows);
}
$response->getBody()->write($payload);
return $response;
}
}
......@@ -274,6 +274,11 @@ class Attribute implements \JsonSerializable
return $this->id;
}
public function getDataset()
{
return $this->dataset;
}
public function getName()
{
return $this->name;
......
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