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

Merge branch 'change-routes' into '3.1'

Change routes

See merge request !41
parents 2cef2811 b314077e
......@@ -75,14 +75,6 @@ $container->set('App\Action\FamilyAction', function (ContainerInterface $c) {
return new App\Action\FamilyAction($c->get('em'));
});
$container->set('App\Action\OutputCategoryListAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryListAction($c->get('em'));
});
$container->set('App\Action\OutputCategoryAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryAction($c->get('em'));
});
$container->set('App\Action\InstanceListAction', function (ContainerInterface $c) {
return new App\Action\InstanceListAction($c->get('em'));
});
......@@ -91,6 +83,18 @@ $container->set('App\Action\InstanceAction', function (ContainerInterface $c) {
return new App\Action\InstanceAction($c->get('em'));
});
$container->set('App\Action\DatasetFamilyListAction', function (ContainerInterface $c) {
return new App\Action\DatasetFamilyListAction($c->get('em'));
});
$container->set('App\Action\DatasetListByInstanceAction', function (ContainerInterface $c) {
return new App\Action\DatasetListByInstanceAction($c->get('em'));
});
$container->set('App\Action\DatasetFamilyAction', function (ContainerInterface $c) {
return new App\Action\DatasetFamilyAction($c->get('em'));
});
$container->set('App\Action\DatasetListAction', function (ContainerInterface $c) {
return new App\Action\DatasetListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
......@@ -99,6 +103,30 @@ $container->set('App\Action\DatasetAction', function (ContainerInterface $c) {
return new App\Action\DatasetAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\CriteriaFamilyListAction', function (ContainerInterface $c) {
return new App\Action\CriteriaFamilyListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\CriteriaFamilyAction', function (ContainerInterface $c) {
return new App\Action\CriteriaFamilyAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\OutputFamilyListAction', function (ContainerInterface $c) {
return new App\Action\OutputFamilyListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\OutputFamilyAction', function (ContainerInterface $c) {
return new App\Action\OutputFamilyAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\OutputCategoryListAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryListAction($c->get('em'));
});
$container->set('App\Action\OutputCategoryAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryAction($c->get('em'));
});
$container->set('App\Action\AttributeListAction', function (ContainerInterface $c) {
return new App\Action\AttributeListAction($c->get('em'));
});
......
......@@ -18,14 +18,19 @@ $app->map([OPTIONS, GET, PUT, DELETE], '/database/{id}', App\Action\DatabaseActi
$app->map([OPTIONS, GET], '/database/{id}/table', App\Action\TableListAction::class);
$app->map([OPTIONS, GET, POST], '/project', App\Action\ProjectListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/project/{name}', App\Action\ProjectAction::class);
$app->map([OPTIONS, GET, POST], '/family/{type}', App\Action\FamilyListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/family/{type}/{id}', App\Action\FamilyAction::class);
$app->map([OPTIONS, GET, POST], '/output-category', App\Action\OutputCategoryListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/output-category/{id}', App\Action\OutputCategoryAction::class);
$app->map([OPTIONS, GET, POST], '/instance', App\Action\InstanceListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/instance/{name}', App\Action\InstanceAction::class);
$app->map([OPTIONS, GET, POST], '/instance/{name}/dataset', App\Action\DatasetListAction::class);
$app->map([OPTIONS, GET, POST], '/instance/{name}/dataset-family', App\Action\DatasetFamilyListAction::class);
$app->map([OPTIONS, GET], '/instance/{name}/dataset', App\Action\DatasetListByInstanceAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/dataset-family/{id}', App\Action\DatasetFamilyAction::class);
$app->map([OPTIONS, GET, POST], '/dataset-family/{id}/dataset', App\Action\DatasetListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/dataset/{name}', App\Action\DatasetAction::class);
$app->map([OPTIONS, GET, POST], '/dataset/{name}/criteria-family', App\Action\CriteriaFamilyListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/criteria-family/{id}', App\Action\CriteriaFamilyAction::class);
$app->map([OPTIONS, GET, POST], '/dataset/{name}/output-family', App\Action\OutputFamilyListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/output-family/{id}', App\Action\OutputFamilyAction::class);
$app->map([OPTIONS, GET, POST], '/output-family/{id}/output-category', App\Action\OutputCategoryListAction::class);
$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->get('/search/{dname}', App\Action\SearchAction::class);
<?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\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use App\Entity\CriteriaFamily;
final class CriteriaFamilyAction extends AbstractAction
{
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
// Search the correct criteria family with primary key
$criteriaFamily = $this->em->find('App\Entity\CriteriaFamily', $args['id']);
// If criteria family is not found 404
if (is_null($criteriaFamily)) {
throw new HttpNotFoundException(
$request,
'Criteria family with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($criteriaFamily);
}
if ($request->getMethod() === PUT) {
$parsedBody = $request->getParsedBody();
$fields = array('label', 'display');
foreach ($fields as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to edit the criteria family'
);
}
}
$this->editCriteriaFamily($criteriaFamily, $parsedBody);
$payload = json_encode($criteriaFamily);
}
if ($request->getMethod() === DELETE) {
$id = $criteriaFamily->getId();
$this->em->remove($criteriaFamily);
$this->em->flush();
$payload = json_encode(array(
'message' => 'Criteria family with id ' . $id . ' is removed!'
));
}
$response->getBody()->write($payload);
return $response;
}
private function editCriteriaFamily(CriteriaFamily $family, array $parsedBody): void
{
$family->setLabel($parsedBody['label']);
$family->setDisplay($parsedBody['display']);
$this->em->flush();
}
}
<?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\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use App\Entity\Dataset;
use App\Entity\CriteriaFamily;
final class CriteriaFamilyListAction extends AbstractAction
{
/**
* `GET` Returns a list of all criteria family for a given dataset
* `POST` Add a new dataset criteria family to a given dataset
*
* @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, POST, OPTIONS');
}
$dataset = $this->em->find('App\Entity\Dataset', $args['name']);
// Returns HTTP 404 if the dataset is not found
if (is_null($dataset)) {
throw new HttpNotFoundException(
$request,
'Dataset with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$families = $this->em->getRepository('App\Entity\CriteriaFamily')->findByDataset($dataset);
$payload = json_encode($families);
}
if ($request->getMethod() === POST) {
$parsedBody = $request->getParsedBody();
// To work this action needs information
foreach (array('label', 'display') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to add a new criteria family'
);
}
}
$family = $this->postCriteriaFamily($parsedBody, $dataset);
$payload = json_encode($family);
$response = $response->withStatus(201);
}
$response->getBody()->write($payload);
return $response;
}
private function postCriteriaFamily(array $parsedBody, Dataset $dataset): CriteriaFamily
{
$family = new CriteriaFamily($dataset);
$family->setLabel($parsedBody['label']);
$family->setDisplay($parsedBody['display']);
$this->em->persist($family);
$this->em->flush();
return $family;
}
}
<?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\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use App\Entity\DatasetFamily;
final class DatasetFamilyAction extends AbstractAction
{
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
// Search the correct dataset family with primary key
$datasetFamily = $this->em->find('App\Entity\DatasetFamily', $args['id']);
// If dataset family is not found 404
if (is_null($datasetFamily)) {
throw new HttpNotFoundException(
$request,
'Dataset family with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($datasetFamily);
}
if ($request->getMethod() === PUT) {
$parsedBody = $request->getParsedBody();
$fields = array('label', 'display');
foreach ($fields as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to edit the dataset family'
);
}
}
$this->editDatasetFamily($datasetFamily, $parsedBody);
$payload = json_encode($datasetFamily);
}
if ($request->getMethod() === DELETE) {
$id = $datasetFamily->getId();
$this->em->remove($datasetFamily);
$this->em->flush();
$payload = json_encode(array(
'message' => 'Dataset family with id ' . $id . ' is removed!'
));
}
$response->getBody()->write($payload);
return $response;
}
private function editDatasetFamily(DatasetFamily $family, array $parsedBody): void
{
$family->setLabel($parsedBody['label']);
$family->setDisplay($parsedBody['display']);
$this->em->flush();
}
}
<?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\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
final class DatasetFamilyListAction extends AbstractAction
{
/**
* `GET` Returns a list of all dataset family for a given instance
* `POST` Add a new dataset family to a given instance
*
* @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, POST, OPTIONS');
}
$instance = $this->em->find('App\Entity\Instance', $args['name']);
// Returns HTTP 404 if the dataset is not found
if (is_null($instance)) {
throw new HttpNotFoundException(
$request,
'Instance with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$families = $this->em->getRepository('App\Entity\DatasetFamily')->findByInstance($instance);
$payload = json_encode($families);
}
if ($request->getMethod() === POST) {
$parsedBody = $request->getParsedBody();
// To work this action needs information
foreach (array('label', 'display') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to add a new dataset family'
);
}
}
$family = $this->postDatasetFamily($parsedBody, $instance);
$payload = json_encode($family);
$response = $response->withStatus(201);
}
$response->getBody()->write($payload);
return $response;
}
private function postDatasetFamily(array $parsedBody, Instance $instance): DatasetFamily
{
$family = new DatasetFamily($instance);
$family->setLabel($parsedBody['label']);
$family->setDisplay($parsedBody['display']);
$this->em->persist($family);
$this->em->flush();
return $family;
}
}
......@@ -18,9 +18,7 @@ use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use Doctrine\ORM\EntityManagerInterface;
use App\Utils\DBALConnectionFactory;
use App\Entity\Database;
use App\Entity\Project;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
use App\Entity\Dataset;
use App\Entity\Attribute;
......@@ -42,7 +40,7 @@ final class DatasetListAction extends AbstractAction
}
/**
* `GET` Returns a list of all datasets listed in the metamodel database
* `GET` Returns a list of all datasets for a given dataset family
* `POST` Add a new dataset
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
......@@ -57,18 +55,18 @@ final class DatasetListAction extends AbstractAction
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
$instance = $this->em->find('App\Entity\Instance', $args['name']);
$datasetFamily = $this->em->find('App\Entity\DatasetFamily', $args['id']);
// Returns HTTP 404 if the instance is not found
if (is_null($instance)) {
// Returns HTTP 404 if the dataset family is not found
if (is_null($datasetFamily)) {
throw new HttpNotFoundException(
$request,
'Instance with name ' . $args['name'] . ' is not found'
'Dataset family with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$datasets = $this->em->getRepository('App\Entity\Dataset')->findByInstance($instance);
$datasets = $this->em->getRepository('App\Entity\Dataset')->findByDatasetFamily($datasetFamily);
$payload = json_encode($datasets);
}
......@@ -86,8 +84,7 @@ final class DatasetListAction extends AbstractAction
'vo',
'data_path',
'selectable_row',
'project_name',
'id_dataset_family'
'project_name'
);
foreach ($fields as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
......@@ -107,15 +104,7 @@ final class DatasetListAction extends AbstractAction
);
}
$family = $this->em->find('App\Entity\DatasetFamily', $parsedBody['id_dataset_family']);
if (is_null($family)) {
throw new HttpBadRequestException(
$request,
'Dataset family with id ' . $parsedBody['id_dataset_family'] . ' is not found'
);
}
$dataset = $this->postDataset($parsedBody, $project, $instance, $family);
$dataset = $this->postDataset($parsedBody, $project, $datasetFamily);
$payload = json_encode($dataset);
$response = $response->withStatus(201);
}
......@@ -136,8 +125,7 @@ final class DatasetListAction extends AbstractAction
private function postDataset(
array $parsedBody,
Project $project,
Instance $instance,
DatasetFamily $family
DatasetFamily $datasetFamily
): Dataset {
$dataset = new Dataset($parsedBody['name']);
$dataset->setTableRef($parsedBody['table_ref']);
......@@ -149,8 +137,7 @@ final class DatasetListAction extends AbstractAction
$dataset->setDataPath($parsedBody['data_path']);
$dataset->setSelectableRow($parsedBody['selectable_row']);
$dataset->setProject($project);
$dataset->setInstance($instance);
$dataset->setDatasetFamily($family);
$dataset->setDatasetFamily($datasetFamily);
$this->em->persist($dataset);
$this->postAttributes($dataset);
......
<?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;
final class DatasetListByInstanceAction extends AbstractAction
{
/**
* `GET` Returns a list of all datasets for a given instance
*
* @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');
}
$instance = $this->em->find('App\Entity\Instance', $args['name']);
// Returns HTTP 404 if the dataset is not found
if (is_null($instance)) {
throw new HttpNotFoundException(
$request,
'Instance with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$qb = $this->em->createQueryBuilder();
$qb->select('d')
->from('App\Entity\Dataset', 'd')
->join('d.datasetFamily', 'f')
->where($qb->expr()->eq('IDENTITY(f.instance)', ':instanceName'))
->setParameter('instanceName', $instance->getName());
$datasets = $qb->getQuery()->getResult();
$payload = json_encode($datasets);
}
$response->getBody()->write($payload);
return $response;
}
}
......@@ -14,6 +14,7 @@ namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpBadRequestException;
use App\Entity\OutputCategory;
use App\Entity\OutputFamily;
......@@ -36,8 +37,20 @@ final class OutputCategoryListAction extends AbstractAction
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
$outputFamily = $this->em->find('App\Entity\OutputFamily', $args['id']);
// Returns HTTP 404 if the output family is not found
if (is_null($outputFamily)) {
throw new HttpNotFoundException(
$request,
'Output family with id ' . $args['id'] . ' is not found'
);
}