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

Add and update groups => done

parent 5087fdd8
......@@ -76,6 +76,14 @@ $container->set('App\Action\ProjectAction', function (ContainerInterface $c) {
return new App\Action\ProjectAction($c->get('em'));
});
$container->set('App\Action\GroupListAction', function (ContainerInterface $c) {
return new App\Action\GroupListAction($c->get('em'));
});
$container->set('App\Action\GroupAction', function (ContainerInterface $c) {
return new App\Action\GroupAction($c->get('em'));
});
$container->set('App\Action\FamilyListAction', function (ContainerInterface $c) {
return new App\Action\FamilyListAction($c->get('em'));
});
......
......@@ -18,6 +18,8 @@ $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], '/group', App\Action\GroupListAction::class);
$app->map([OPTIONS, GET, POST], '/group/{id}', App\Action\GroupAction::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-family', App\Action\DatasetFamilyListAction::class);
......
......@@ -140,3 +140,7 @@ curl -d '{"id":9,"name":"zflg","table_name":"aspic_vipers_dr2_w1","label":"zflg"
curl -d '{"id":56,"name":"spec1d","table_name":"aspic_vipers_dr2_w1","label":"spec1d","form_label":"spec1d","description":null,"output_display":560,"criteria_display":560,"search_flag":"SPECTRUM_1D","search_type":null,"type":"text","operator":null,"min":null,"max":null,"placeholder_min":null,"placeholder_max":null,"renderer":"download","renderer_config":{"display":"icon-button","text":"DOWNLOAD","icon":"fas fa-download"},"display_detail":560,"selected":true,"order_by":false,"order_display":560,"detail":true,"renderer_detail":"spectra_graph","options":null,"vo_utype":null,"vo_ucd":null,"vo_unit":null,"vo_description":null,"vo_datatype":null,"vo_size":null,"id_criteria_family":null,"id_output_category":8}' -H "Content-Type: application/json" -X PUT http://localhost/dataset/vipers_dr2_w1/attribute/56
curl -d '{"id":57,"name":"spec1dnoise","table_name":"aspic_vipers_dr2_w1","label":"spec1dnoise","form_label":"spec1dnoise","description":null,"output_display":570,"criteria_display":570,"search_flag":null,"search_type":null,"type":"text","operator":null,"min":null,"max":null,"placeholder_min":null,"placeholder_max":null,"renderer":"download","renderer_config":{"display":"icon-button","text":"DOWNLOAD","icon":"fas fa-download"},"display_detail":570,"selected":true,"order_by":false,"order_display":570,"detail":true,"renderer_detail":null,"options":null,"vo_utype":null,"vo_ucd":null,"vo_unit":null,"vo_description":null,"vo_datatype":null,"vo_size":null,"id_criteria_family":null,"id_output_category":8}' -H "Content-Type: application/json" -X PUT http://localhost/dataset/vipers_dr2_w1/attribute/57
curl -d '{"id":58,"name":"spec1dsky","table_name":"aspic_vipers_dr2_w1","label":"spec1dsky","form_label":"spec1dsky","description":null,"output_display":580,"criteria_display":580,"search_flag":null,"search_type":null,"type":"text","operator":null,"min":null,"max":null,"placeholder_min":null,"placeholder_max":null,"renderer":"download","renderer_config":{"display":"icon-button","text":"DOWNLOAD","icon":"fas fa-download"},"display_detail":580,"selected":true,"order_by":false,"order_display":580,"detail":true,"renderer_detail":null,"options":null,"vo_utype":null,"vo_ucd":null,"vo_unit":null,"vo_description":null,"vo_datatype":null,"vo_size":null,"id_criteria_family":null,"id_output_category":8}' -H "Content-Type: application/json" -X PUT http://localhost/dataset/vipers_dr2_w1/attribute/58
# Add groups
curl -d '{"label":"SVOM"}' -H "Content-Type: application/json" -X POST http://localhost/group
curl -d '{"label":"ASPIC"}' -H "Content-Type: application/json" -X POST http://localhost/group
<?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\Group;
final class GroupAction extends AbstractAction
{
/**
* `GET` Returns the Group found
* `PUT` Full update the Group and returns the new version
* `DELETE` Delete the Group found and return a confirmation message
*
* @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, PUT, DELETE, OPTIONS');
}
// Search the correct group with primary key
$group = $this->em->find('App\Entity\Group', $args['id']);
// If group is not found 404
if (is_null($group)) {
throw new HttpNotFoundException(
$request,
'Group with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($group);
}
if ($request->getMethod() === PUT) {
$parsedBody = $request->getParsedBody();
// If mandatories empty fields 400
foreach (array('label') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to edit the group'
);
}
}
$this->editGroup($group, $parsedBody);
$payload = json_encode($group);
}
if ($request->getMethod() === DELETE) {
$id = $group->getId();
$this->em->remove($group);
$this->em->flush();
$payload = json_encode(array('message' => 'Group with id ' . $id . ' is removed!'));
}
$response->getBody()->write($payload);
return $response;
}
/**
* Update group object with setters
*
* @param Group $group The group to update
* @param array $parsedBody Contains the new values ​​of the group sent by the user
*/
private function editGroup(Group $group, array $parsedBody): void
{
$group->setLabel($parsedBody['label']);
$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 App\Entity\Group;
final class GroupListAction extends AbstractAction
{
/**
* `GET` Returns a list of all groups listed in the metamodel database
* `POST` Add a new group
*
* @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');
}
if ($request->getMethod() === GET) {
// Retrieve group with id
$groups = $this->em->getRepository('App\Entity\Group')->findAll();
$payload = json_encode($groups);
}
if ($request->getMethod() === POST) {
$parsedBody = $request->getParsedBody();
// To work this action needs group information
foreach (array('label') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to add a new group'
);
}
}
$group = $this->postDatabase($parsedBody);
$payload = json_encode($group);
$response = $response->withStatus(201);
}
$response->getBody()->write($payload);
return $response;
}
/**
* Add a new group into the metamodel
*
* @param array $parsedBody Contains the values ​​of the new group sent by the user
*/
private function postDatabase(array $parsedBody): Group
{
$group = new Group();
$group->setLabel($parsedBody['label']);
$this->em->persist($group);
$this->em->flush();
return $group;
}
}
......@@ -96,7 +96,7 @@ class Group implements \JsonSerializable
}
$datasets = array();
foreach ($this->datasets() as $dataset) {
foreach ($this->getDatasets() as $dataset) {
$datasets[] = $dataset->getName();
}
......
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