feat(api) : add constructeur stats endpoints with entity counts
This commit is contained in:
84
src/Controller/ConstructeurStatsController.php
Normal file
84
src/Controller/ConstructeurStatsController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\ComposantConstructeurLink;
|
||||
use App\Entity\MachineConstructeurLink;
|
||||
use App\Entity\PieceConstructeurLink;
|
||||
use App\Entity\ProductConstructeurLink;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class ConstructeurStatsController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
#[Route('/api/constructeurs/{id}/stats', name: 'api_constructeur_stats', methods: ['GET'])]
|
||||
public function stats(string $id): JsonResponse
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_VIEWER');
|
||||
|
||||
$constructeur = $this->constructeurs->find($id);
|
||||
if (!$constructeur) {
|
||||
return new JsonResponse(
|
||||
['message' => 'Fournisseur introuvable.'],
|
||||
Response::HTTP_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
return new JsonResponse($this->buildStats($id));
|
||||
}
|
||||
|
||||
#[Route('/api/constructeurs/stats', name: 'api_constructeurs_stats_bulk', methods: ['GET'])]
|
||||
public function bulkStats(): JsonResponse
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_VIEWER');
|
||||
|
||||
$allIds = $this->em->createQuery(
|
||||
'SELECT c.id FROM App\Entity\Constructeur c',
|
||||
)->getSingleColumnResult();
|
||||
|
||||
$result = [];
|
||||
foreach ($allIds as $id) {
|
||||
$result[$id] = $this->buildStats($id);
|
||||
}
|
||||
|
||||
return new JsonResponse($result);
|
||||
}
|
||||
|
||||
/** @return array{composantCount: int, pieceCount: int, machineCount: int, productCount: int} */
|
||||
private function buildStats(string $constructeurId): array
|
||||
{
|
||||
$composantCount = (int) $this->em->createQuery(
|
||||
'SELECT COUNT(l.id) FROM '.ComposantConstructeurLink::class.' l WHERE l.constructeur = :id',
|
||||
)->setParameter('id', $constructeurId)->getSingleScalarResult();
|
||||
|
||||
$pieceCount = (int) $this->em->createQuery(
|
||||
'SELECT COUNT(l.id) FROM '.PieceConstructeurLink::class.' l WHERE l.constructeur = :id',
|
||||
)->setParameter('id', $constructeurId)->getSingleScalarResult();
|
||||
|
||||
$machineCount = (int) $this->em->createQuery(
|
||||
'SELECT COUNT(l.id) FROM '.MachineConstructeurLink::class.' l WHERE l.constructeur = :id',
|
||||
)->setParameter('id', $constructeurId)->getSingleScalarResult();
|
||||
|
||||
$productCount = (int) $this->em->createQuery(
|
||||
'SELECT COUNT(l.id) FROM '.ProductConstructeurLink::class.' l WHERE l.constructeur = :id',
|
||||
)->setParameter('id', $constructeurId)->getSingleScalarResult();
|
||||
|
||||
return [
|
||||
'composantCount' => $composantCount,
|
||||
'pieceCount' => $pieceCount,
|
||||
'machineCount' => $machineCount,
|
||||
'productCount' => $productCount,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user