Skip to content
Snippets Groups Projects
Commit deb7b593 authored by Pierre Fleutot's avatar Pierre Fleutot
Browse files

API : MetaInfo : ajout requête recherche par id graphie ou id template....

API : MetaInfo : ajout requête recherche par id graphie ou id template. MetaInfoTemplate : ajout requête modification et liste
parent 399189f0
No related branches found
No related tags found
No related merge requests found
......@@ -30,99 +30,6 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
*/
class ApiMetaInfoController extends ApiBaseController
{
/**
*
* Crée un modèle de méta-information.
*
* @Route("/create-template", name="api_meta_info_template_create", methods={"POST"})
*
* @OA\Response(response=200, description="success", @OA\JsonContent(type="string"))
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* required={"description", "category"},
* @OA\Property(property="description", type="string", example="Nombre de fois où a été rencontré dans MW"),
* @OA\Property(property="category", type="string", example="integer OR string OR boolean"),
* @OA\Property(property="default", type="string", example="10 OR true OR false OR string value"),
* @OA\Property(property="choices", type="array", description="Optionnel. Tableau des valeurs possibles quand category = string",
* example={"connu", "non connu"},
* @OA\Items(
* type="string"
* )
* )
* )
* )
* @OA\Tag(name="MetaInfos")
* @Security(name="OAuth2")
*/
public function createMetaInfoTemplate(Request $request): Response
{
$data = json_decode($request->getContent(), true);
if (!$data) {
return $this->createJsonResponse(401, ['error' => sprintf("Json non valide")]);
}
if ($missingFields = $this->getMissingFields($data, ['description', 'category'])) {
return $this->createJsonResponse(401, ['error' => sprintf("Veuillez fournir une valeur pour: %s", implode(', ', $missingFields))]);
}
if (!in_array($data['category'], MetaInfo::META_INFO_LIST_CATEGORIES)) {
return $this->createJsonResponse(401, ['error' => sprintf("La catégorie de méta-information %s n'existe pas", $data['category'])]);
}
$metaInfoTemplate = new MetaInfoTemplate($data['category']);
$metaInfoTemplate->setCreatedBy($this->getUser());
$metaInfoTemplate->setDescription($data['description']);
if ($data['default'] ?? null) {
$metaInfoTemplate->setDefault($data['default']);
}
if ($data['choices'] ?? null && $data['category'] === MetaInfo::META_INFO_CATEGORY_STRING) {
if (is_array($data['choices'])) {
$metaInfoTemplate->setComplementStringChoices($data['choices']);
} else {
return $this->createJsonResponse(401, ['error' => sprintf("Le paramètre choices doit être un tableau")]);
}
}
$this->doctrine->getManager()->persist($metaInfoTemplate);
$this->doctrine->getManager()->flush();
$this->success[] = sprintf("Modèle de Méta information id %s créé.", $metaInfoTemplate->getId());
return $this->createJsonResponse(200);
}
/**
* Supprime le metaInfoTemplate
*
* @Route("/delete-template/{id}", name="api_meta_info_template_delete", methods={"DELETE"})
*
* @OA\Response(
* response=200,
* description="Success"
* )
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
*
* @OA\Parameter(
* name="id",
* in="path",
* description="id of the meta info template to be removed",
* @OA\Schema(type="integer")
* )
* @OA\Tag(name="MetaInfos")
* @Security(name="OAuth2")
*/
public function delete(MetaInfoTemplate $metaInfoTemplate)
{
$response = $this->createJsonResponse(200, ['success' => sprintf("Modèle de Méta information « %s » (id: %s) supprimé", $metaInfoTemplate->getDescription(), $metaInfoTemplate->getId())]);
$this->doctrine->getManager()->remove($metaInfoTemplate);
$this->doctrine->getManager()->flush();
return $response;
}
/**
*
* Si la graphie possède déjà une méta information de la même catégorie que le modèle template_id, on la met à jour sinon on crée une nouvelle méta-info.
......@@ -193,22 +100,22 @@ class ApiMetaInfoController extends ApiBaseController
}
/**
* Recherche de metaInfos par date ou user ou action ou origin
* Recherche de metaInfos id graphie ou id de modèle méta information
*
* @Route("/search", name="api_meta_info_search", methods={"POST"})
*
* @OA\Response(response=200, description="success", @OA\JsonContent(type="string"))
* @OA\Response(response=200, description="success",
* @OA\JsonContent(type="array",
* @OA\Items(ref=@Model(type=MetaInfo::class, groups={"metaInfo:read"})))
* )
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* @OA\Property(property="date", type="date"),
* @OA\Property(property="date_client", type="date"),
* @OA\Property(property="user_id", type="integer", example="4"),
* @OA\Property(property="origin", type="string", example="Prisms"),
* @OA\Property(property="action", type="string", example="action"),
* @OA\Property(property="graphy_id", type="integer"),
* @OA\Property(property="template_id", type="integer")
* )
* )
* @OA\Tag(name="MetaInfos")
......@@ -220,46 +127,22 @@ class ApiMetaInfoController extends ApiBaseController
if (!$data) {
return $this->createJsonResponse(401, ['error' => sprintf("Json non valide")]);
}
if (!isset($data['date']) && !isset($data['date_client']) && !isset($data['user_id']) && !isset($data['origin']) && !isset($data['action'])) {
return $this->createJsonResponse(401, ['error' => sprintf("Veuillez spécifier au moins un paramètre de recherche parmi: date, user_id, action, origin")]);
if (!isset($data['date']) && !isset($data['graphy_id']) && !isset($data['template_id'])) {
return $this->createJsonResponse(401, ['error' => sprintf("Veuillez spécifier au moins un paramètre de recherche parmi: graphy_id, template_id")]);
}
$filter = [];
if ($data['date'] ?? null) {
$date = date_create_from_format('Y-m-d', $data['date']);
if ($date === false) {
return $this->createJsonResponse(401, ['error' => sprintf("Veuillez fournir une date au format aaaa-mm-jj pour le paramètre «date»")]);
} else {
$filter['date'] = $date;
}
}
if ($data['date_client'] ?? null) {
$dateClient = date_create_from_format('Y-m-d', $data['date_client']);
if ($dateClient === false) {
return $this->createJsonResponse(401, ['error' => sprintf("Veuillez fournir une date au format aaaa-mm-jj pour le paramètre «date client»")]);
} else {
$filter['date_client'] = $dateClient;
}
}
if ($data["user_id"] ?? null) {
$user = $this->doctrine->getRepository(User::class)->find($data['user_id']);
if (!$user) {
return $this->createJsonResponse(401, ['error' => sprintf("Pas d'utilisateur trouvé pour l'id %s", $data['user_id'])]);
} else {
$filter['user'] = $user;
}
}
if ($data['origin'] ?? null) {
$filter['origin'] = $data['origin'];
if ($data['graphy_id'] ?? null) {
$filter['graphy'] = $data['graphy_id'];
}
if ($data['action'] ?? null) {
$filter['action'] = $data['action'];
if ($data['template_id'] ?? null) {
$filter['metaInfoTemplate'] = $data['template_id'];
}
$metaInfos = $this->doctrine->getRepository(MetaInfo::class)->search($filter);
$metaInfos = $this->doctrine->getRepository(MetaInfo::class)->findBy($filter, ['createdAt' => 'DESC']);
$content = $serializer->serialize($metaInfos, 'json', [
'groups' => ["metaInfo:read"],
'groups' => ["metaInfo:read", "user:read"],
'datetime_format' => 'Y-m-d h:i:s.u'
]);
......
<?php
namespace App\Controller;
use App\Entity\Entry;
use App\Entity\Graphy;
use App\Entity\Headword;
use App\Entity\Label;
use App\Entity\Lexicon;
use App\Entity\MetaInfo;
use App\Entity\MetaInfoTemplate;
use App\Entity\User;
use App\Manager\WiktionaryManager;
use App\Repository\LabelRepository;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Nelmio\ApiDocBundle\Annotation\Security;
use OpenApi\Annotations as OA;
use Nelmio\ApiDocBundle\Annotation\Model;
use Symfony\Component\Serializer\SerializerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
/**
* @Route("/api/meta-info-template")
*/
class ApiMetaInfoTemplateController extends ApiBaseController
{
/**
*
* Crée un modèle de méta-information.
*
* @Route("/create", name="api_meta_info_template_create", methods={"POST"})
*
* @OA\Response(response=200, description="success", @OA\JsonContent(type="string"))
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* required={"description", "category"},
* @OA\Property(property="description", type="string", example="Nombre de fois où a été rencontré dans MW"),
* @OA\Property(property="category", type="string", example="integer OR string OR boolean"),
* @OA\Property(property="default", type="string", example="10 OR true OR false OR string value"),
* @OA\Property(property="choices", type="array", description="Optionnel. Tableau des valeurs possibles quand category = string",
* example={"connu", "non connu"},
* @OA\Items(
* type="string"
* )
* )
* )
* )
* @OA\Tag(name="MetaInfoTemplates")
* @Security(name="OAuth2")
*/
public function createMetaInfoTemplate(Request $request): Response
{
$data = json_decode($request->getContent(), true);
if (!$data) {
return $this->createJsonResponse(401, ['error' => sprintf("Json non valide")]);
}
if ($missingFields = $this->getMissingFields($data, ['description', 'category'])) {
return $this->createJsonResponse(401, ['error' => sprintf("Veuillez fournir une valeur pour: %s", implode(', ', $missingFields))]);
}
if (!in_array($data['category'], MetaInfo::META_INFO_LIST_CATEGORIES)) {
return $this->createJsonResponse(401, ['error' => sprintf("La catégorie de méta-information %s n'existe pas", $data['category'])]);
}
$metaInfoTemplate = new MetaInfoTemplate($data['category']);
$metaInfoTemplate->setCreatedBy($this->getUser());
$metaInfoTemplate->setDescription($data['description']);
if ($data['default'] ?? null) {
$metaInfoTemplate->setDefault($data['default']);
}
if ($data['choices'] ?? null && $data['category'] === MetaInfo::META_INFO_CATEGORY_STRING) {
if (is_array($data['choices'])) {
$metaInfoTemplate->setComplementStringChoices($data['choices']);
} else {
return $this->createJsonResponse(401, ['error' => sprintf("Le paramètre choices doit être un tableau")]);
}
}
$this->doctrine->getManager()->persist($metaInfoTemplate);
$this->doctrine->getManager()->flush();
$this->success[] = sprintf("Modèle de Méta information id %s créé.", $metaInfoTemplate->getId());
return $this->createJsonResponse(200);
}
/**
* Supprime le metaInfoTemplate
*
* @Route("/delete/{id}", name="api_meta_info_template_delete", methods={"DELETE"})
*
* @OA\Response(
* response=200,
* description="Success"
* )
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
*
* @OA\Parameter(
* name="id",
* in="path",
* description="id of the meta info template to be removed",
* @OA\Schema(type="integer")
* )
* @OA\Tag(name="MetaInfoTemplates")
* @Security(name="OAuth2")
*/
public function delete(MetaInfoTemplate $metaInfoTemplate)
{
$response = $this->createJsonResponse(200, ['success' => sprintf("Modèle de Méta information « %s » (id: %s) supprimé", $metaInfoTemplate->getDescription(), $metaInfoTemplate->getId())]);
$this->doctrine->getManager()->remove($metaInfoTemplate);
$this->doctrine->getManager()->flush();
return $response;
}
/**
* Permet de modifier un modèle de méta information existant. On n'autorise pas la modification de la catégorie
*
* @Route("/edit/{id}", name="api_meta_info_template_edit", methods={"POST"})
*
* @OA\Response(response=200, description="success", @OA\JsonContent(type="string"))
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
*
* @OA\Parameter(
* name="id",
* in="path",
* description="id of the meta info template",
* @OA\Schema(type="integer")
* )
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* @OA\Property(property="description", type="string", example="Nombre de fois où a été rencontré dans MW"),
* @OA\Property(property="default", type="string", example="10 OR true OR false OR string value"),
* @OA\Property(property="choices", type="array", description="Optionnel. Tableau des valeurs possibles quand category = string",
* example={"connu", "non connu"},
* @OA\Items(
* type="string"
* )
* )
* )
* )
* @OA\Tag(name="MetaInfoTemplates")
* @Security(name="OAuth2")
*/
public function editMetaInfoTemplate(Request $request, MetaInfoTemplate $metaInfoTemplate = null): Response
{
if (!$metaInfoTemplate) {
return $this->createJsonResponse(401, ['error' => sprintf("Pas de modèle trouvé pour cette id")]);
}
$data = json_decode($request->getContent(), true);
if (!$data) {
return $this->createJsonResponse(401, ['error' => sprintf("Json non valide")]);
}
if ($data['description'] ?? null) {
$metaInfoTemplate->setDescription($data['description']);
}
if ($data['default'] ?? null) {
$metaInfoTemplate->setDefault($data['default']);
}
if ($data['choices'] ?? null && $data['category'] === MetaInfo::META_INFO_CATEGORY_STRING) {
if (is_array($data['choices'])) {
$metaInfoTemplate->setComplementStringChoices($data['choices']);
} else {
return $this->createJsonResponse(401, ['error' => sprintf("Le paramètre choices doit être un tableau")]);
}
}
$this->doctrine->getManager()->flush();
return $this->createJsonResponse(200, ['message' => sprintf("Modèle « %s » mis à jour.", $metaInfoTemplate)]);
}
/**
* Liste tous les modèles existants
*
* @Route("/list", name="api_meta_info_template_list", methods={"GET"})
*
* @OA\Response(response=200, description="success",
* @OA\JsonContent(type="array",
* @OA\Items(ref=@Model(type=MetaInfoTemplate::class, groups={"metaInfo:read"})))
* )
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
*
* @OA\Tag(name="MetaInfoTemplates")
* @Security(name="OAuth2")
*/
public function listMetaInfoTemplates(Request $request, SerializerInterface $serializer): Response
{
$metaInfoTemplates = $this->doctrine->getRepository(MetaInfoTemplate::class)->findAll();
$content = $serializer->serialize($metaInfoTemplates, 'json', ['groups' => ["metaInfo:read", "user:read"]]);
return new JsonResponse($content, 200, [], true);
}
}
......@@ -153,7 +153,10 @@ class ApiTraceController extends ApiBaseController
*
* @Route("/search", name="api_trace_search", methods={"POST"})
*
* @OA\Response(response=200, description="success", @OA\JsonContent(type="string"))
* @OA\Response(response=200, description="success",
* @OA\JsonContent(type="array",
* @OA\Items(ref=@Model(type=Trace::class, groups={"trace:read"})))
* )
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
......@@ -215,7 +218,7 @@ class ApiTraceController extends ApiBaseController
$traces = $this->doctrine->getRepository(Trace::class)->search($filter);
$content = $serializer->serialize($traces, 'json', [
'groups' => ["trace:read"],
'groups' => ["trace:read", "user:read"],
'datetime_format' => 'Y-m-d h:i:s.u'
]);
......
......@@ -29,6 +29,7 @@ class MetaInfo
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"metaInfo:read"})
*/
private $id;
......
......@@ -18,6 +18,7 @@ class MetaInfoTemplate
/**
* @ORM\Id
* @ORM\GeneratedValue
* @Groups({"metaInfo:read"})
* @ORM\Column(type="integer")
*/
private $id;
......@@ -60,6 +61,7 @@ class MetaInfoTemplate
/**
* @ORM\Column(type="array", nullable=true)
* @Groups({"metaInfo:read"})
* @OA\Property(type="array", @OA\Items(type="string"))
*/
private $complementStringChoices;
......
......@@ -22,12 +22,14 @@ class Trace
private $id;
/**
* @var \DateTimeImmutable
* @Groups({"trace:read"})
* @ORM\Column(type="datetimemicroseconds")
*/
private $createdAt;
/**
* @var \DateTime
* @Groups({"trace:read"})
* @ORM\Column(type="datetimemicroseconds")
*/
......
......@@ -37,6 +37,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"user:read"})
*/
private $id;
......@@ -60,6 +61,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
* @var string
* @ORM\Column(type="string", length=100)
* @Assert\Length(max={100})
* @Groups({"user:read"})
*/
private $pseudo;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment