Skip to content

Commit

Permalink
bases routes GET API #3307
Browse files Browse the repository at this point in the history
  • Loading branch information
numew committed Dec 5, 2024
1 parent 89b5e7f commit f0150d1
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 3 deletions.
119 changes: 119 additions & 0 deletions src/Controller/Api/SignalementController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace App\Controller\Api;

use App\Dto\Api\Response\SignalementResponse;
use App\Repository\SignalementRepository;
use App\Service\Signalement\SignalementDesordresProcessor;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Attributes as OA;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

#[When('dev')]
#[When('test')]
#[Route('/api')]
class SignalementController extends AbstractController
{
#[Route('/signalements', name: 'api_signalements', methods: ['GET'])]
#[OA\Get(
path: '/api/signalements',
description: 'Retourne les {{ limit }} derniers signalements',
summary: 'Liste des signalements',
security: [['bearerAuth' => []]],
tags: ['Signalements'],
)]
#[OA\Parameter(
name: 'limit',
description: 'Nombre de signalements à retourner (défaut : 20, max : 100)',
in: 'query',
required: false,
schema: new OA\Schema(type: 'limit', example: '10')
)]
#[OA\Response(
response: Response::HTTP_OK,
description: 'Une liste de signalements',
content: new OA\JsonContent(
type: 'array',
items: new OA\Items(ref: new Model(type: SignalementResponse::class))
)
)]
public function getSignalementList(
SignalementRepository $signalementRepository,
SignalementDesordresProcessor $signalementDesordresProcessor,
UrlGeneratorInterface $urlGenerator,
#[MapQueryParameter] int $limit = 20,
): JsonResponse {
if ($limit > 100) {
$limit = 100;
}
$signalements = $signalementRepository->findForAPI(user: $this->getUser(), limit: $limit);
$resources = [];
foreach ($signalements as $signalement) {
$resources[] = new SignalementResponse($signalement, $signalementDesordresProcessor, $urlGenerator); // sinon comment acceder à ses services pour generer les responses ?
}

return new JsonResponse($resources, Response::HTTP_OK);
}

#[Route('/signalements/{uuid}', name: 'api_signalement_uuid', methods: ['GET'])]
#[OA\Get(
path: '/api/signalements/{uuid}',
description: 'Retourne un signalement récupéré par son UUID',
summary: 'Signalement par UUID',
security: [['bearerAuth' => []]],
tags: ['Signalements']
)]
#[OA\Response(
response: Response::HTTP_OK,
description: 'Un signalement',
content: new OA\JsonContent(ref: '#/components/schemas/SignalementResponse')
)]
public function getSignalementByUuid(
SignalementRepository $signalementRepository,
SignalementDesordresProcessor $signalementDesordresProcessor,
UrlGeneratorInterface $urlGenerator,
string $uuid,
): JsonResponse {
$signalements = $signalementRepository->findForAPI(user : $this->getUser(), uuid : $uuid);
if (!count($signalements)) {
return new JsonResponse(['message' => 'Signalement introuvable'], Response::HTTP_NOT_FOUND);
}
$resource = new SignalementResponse($signalements[0], $signalementDesordresProcessor, $urlGenerator);

return new JsonResponse($resource, Response::HTTP_OK);
}

#[Route('/signalements/reference/{reference}', name: 'api_signalement_reference', methods: ['GET'])]
#[OA\Get(
path: '/api/signalements/reference/{reference}',
description: 'Retourne un signalement récupéré par sa reference',
summary: 'Signalement par référence',
security: [['bearerAuth' => []]],
tags: ['Signalements']
)]
#[OA\Response(
response: Response::HTTP_OK,
description: 'Un signalement',
content: new OA\JsonContent(ref: '#/components/schemas/SignalementResponse')
)]
public function getSignalementByReference(
SignalementRepository $signalementRepository,
SignalementDesordresProcessor $signalementDesordresProcessor,
UrlGeneratorInterface $urlGenerator,
string $reference,
): JsonResponse {
$signalements = $signalementRepository->findForAPI(user : $this->getUser(), reference : $reference);
if (!count($signalements)) {
return new JsonResponse(['message' => 'Signalement introuvable'], Response::HTTP_NOT_FOUND);
}
$resource = new SignalementResponse($signalements[0], $signalementDesordresProcessor, $urlGenerator);

return new JsonResponse($resource, Response::HTTP_OK);
}
}
2 changes: 1 addition & 1 deletion src/DataFixtures/Files/User.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ users:
-
email: [email protected]
roles: "[\"ROLE_API_USER\"]"
partner: "Administrateurs Histologe ALL"
partner: "Partenaire 13-01"
statut: 1
is_generique: 0
is_mailing_active: 0
Expand Down
26 changes: 26 additions & 0 deletions src/Dto/Api/Response/AffectationResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Dto\Api\Response;

use App\Entity\Affectation;

class AffectationResponse
{
public string $dateCreation;
public ?string $dateReponse;
public int $statut;
public PartnerResponse $partnerResponse;
public ?string $motifCloture;
public ?string $motifRefus;

public function __construct(
Affectation $affectation,
) {
$this->dateCreation = $affectation->getCreatedAt()->format(\DATE_ATOM);
$this->dateReponse = $affectation->getAnsweredAt()?->format(\DATE_ATOM);
$this->statut = $affectation->getStatut(); // envoyer un libellé ?
$this->partnerResponse = new PartnerResponse($affectation->getPartner());
$this->motifCloture = $affectation->getMotifCloture()?->label();
$this->motifRefus = $affectation->getMotifRefus()?->label();
}
}
31 changes: 31 additions & 0 deletions src/Dto/Api/Response/DesordreResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Dto\Api\Response;

use App\Entity\Criticite;
use App\Entity\DesordrePrecision;

class DesordreResponse
{
public string $categorie;
public ?string $zone;
public array $details = [];

public function __construct(
string $categorie,
array $data,
?string $zone = null,
) {
$this->categorie = $categorie;
$this->zone = $zone;
foreach ($data as $label => $unused) {
$details = $label;
if ($unused instanceof DesordrePrecision && $unused->getLabel()) {
$details .= ' : '.$unused->getLabel();
} elseif ($unused instanceof Criticite && $unused->getLabel()) {
$details .= ' : '.$unused->getLabel();
}
$this->details[] = $details;
}
}
}
25 changes: 25 additions & 0 deletions src/Dto/Api/Response/FileResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Dto\Api\Response;

use App\Entity\File;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class FileResponse
{
public string $uuid;
public string $titre;
public string $documentType;
public string $url;

public function __construct(
File $file,
UrlGeneratorInterface $urlGenerator,
) {
$this->uuid = $file->getUuid();
$this->titre = $file->getTitle();
$this->documentType = $file->getDocumentType()->value;
$this->url = $urlGenerator->generate('show_file', ['uuid' => $file->getUuid()], UrlGeneratorInterface::ABSOLUTE_URL);
// besoin d'exposer plus d'élements ?
}
}
10 changes: 10 additions & 0 deletions src/Dto/Api/Response/GeolocalisationResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Dto\Api\Response;

class GeolocalisationResponse
{
public function __construct(public ?float $latitude, public ?float $longitude)
{
}
}
31 changes: 31 additions & 0 deletions src/Dto/Api/Response/InterventionResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Dto\Api\Response;

use App\Entity\Intervention;

class InterventionResponse
{
public string $dateIntervention;
public ?string $type;
public ?string $statut;
public ?PartnerResponse $partner;
public ?string $details;
public array $conclusions = [];
public ?bool $occupantPresent;
public ?bool $proprietairePresent;

public function __construct(
Intervention $intervention,
) {
$this->dateIntervention = $intervention->getScheduledAt()->format(\DATE_ATOM);
$this->type = $intervention->getType()?->label();
$this->statut = $intervention->getStatus();
$this->partner = $intervention->getPartner() ? new PartnerResponse($intervention->getPartner()) : null;
$this->details = $intervention->getDetails(); // traitement de suppression du html
$this->conclusions = $intervention->getConcludeProcedure() ?? [];
$this->occupantPresent = $intervention->isOccupantPresent();
$this->proprietairePresent = $intervention->isProprietairePresent();
// besoin d'exposer plus d'élements ?
}
}
21 changes: 21 additions & 0 deletions src/Dto/Api/Response/PartnerResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Dto\Api\Response;

use App\Entity\Partner;

class PartnerResponse
{
public string $nom;
public ?string $type;
public array $competences = [];

public function __construct(
Partner $partner,
) {
$this->nom = $partner->getNom();
$this->type = $partner->getType()?->label();
$this->competences = $partner->getCompetence() ?? [];
// besoin d'exposer plus d'élements ?
}
}
Loading

0 comments on commit f0150d1

Please sign in to comment.