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, + ]; + } +}