siteRepository->find($id); if (!$site) { return $this->json(['success' => false, 'error' => 'Site not found.'], 404); } $documents = $this->documentRepository->findBy(['site' => $site]); return $this->json($this->normalizeDocuments($documents)); } #[Route('/machine/{id}', name: 'documents_by_machine', methods: ['GET'])] public function listByMachine(string $id): JsonResponse { $machine = $this->machineRepository->find($id); if (!$machine) { return $this->json(['success' => false, 'error' => 'Machine not found.'], 404); } $documents = $this->documentRepository->findBy(['machine' => $machine]); return $this->json($this->normalizeDocuments($documents)); } #[Route('/composant/{id}', name: 'documents_by_composant', methods: ['GET'])] public function listByComposant(string $id): JsonResponse { $composant = $this->composantRepository->find($id); if (!$composant) { return $this->json(['success' => false, 'error' => 'Composant not found.'], 404); } $documents = $this->documentRepository->findBy(['composant' => $composant]); return $this->json($this->normalizeDocuments($documents)); } #[Route('/piece/{id}', name: 'documents_by_piece', methods: ['GET'])] public function listByPiece(string $id): JsonResponse { $piece = $this->pieceRepository->find($id); if (!$piece) { return $this->json(['success' => false, 'error' => 'Piece not found.'], 404); } $documents = $this->documentRepository->findBy(['piece' => $piece]); return $this->json($this->normalizeDocuments($documents)); } #[Route('/product/{id}', name: 'documents_by_product', methods: ['GET'])] public function listByProduct(string $id): JsonResponse { $product = $this->productRepository->find($id); if (!$product) { return $this->json(['success' => false, 'error' => 'Product not found.'], 404); } $documents = $this->documentRepository->findBy(['product' => $product]); return $this->json($this->normalizeDocuments($documents)); } /** * @param Document[] $documents */ private function normalizeDocuments(array $documents): array { return array_map(static function (Document $document): array { return [ 'id' => $document->getId(), 'name' => $document->getName(), 'filename' => $document->getFilename(), 'path' => $document->getPath(), 'mimeType' => $document->getMimeType(), 'size' => $document->getSize(), 'siteId' => $document->getSite()?->getId(), 'machineId' => $document->getMachine()?->getId(), 'composantId' => $document->getComposant()?->getId(), 'pieceId' => $document->getPiece()?->getId(), 'productId' => $document->getProduct()?->getId(), 'createdAt' => $document->getCreatedAt()->format(DATE_ATOM), 'updatedAt' => $document->getUpdatedAt()->format(DATE_ATOM), ]; }, $documents); } }