From 4afbc8ba8ab83cd4ad00510c3ca19bfd8822d65a Mon Sep 17 00:00:00 2001 From: r-dev Date: Sat, 4 Apr 2026 17:26:06 +0200 Subject: [PATCH] feat(api) : add constructeur stats endpoints with entity counts --- .../ConstructeurStatsController.php | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/Controller/ConstructeurStatsController.php diff --git a/src/Controller/ConstructeurStatsController.php b/src/Controller/ConstructeurStatsController.php new file mode 100644 index 0000000..8efdb7b --- /dev/null +++ b/src/Controller/ConstructeurStatsController.php @@ -0,0 +1,84 @@ +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, + ]; + } +}