From e3f16127af9a48cf37e535902f576a804e32628b Mon Sep 17 00:00:00 2001 From: Kouril42 Date: Sun, 19 Feb 2023 12:23:28 +0100 Subject: [PATCH] Add idBuilding & idSite to find_place_by_name + create get_?_name for building, site & floor --- API.md | 47 +++++++++++++++++++++++---- src/Controller/MapApiController.php | 35 +++++++++++++++++++- src/Repository/BuildingRepository.php | 14 ++++++++ src/Repository/SiteRepository.php | 14 ++++++++ 4 files changed, 103 insertions(+), 7 deletions(-) diff --git a/API.md b/API.md index fa78c1f..654bad9 100644 --- a/API.md +++ b/API.md @@ -76,18 +76,53 @@ __The json will look like :__ --------------------- ### /api/map/find_place_by_name/NAME ->Return a dict representing the place with NAME. +>Return a dict representing the list of places with NAME. __Format of the dict :__ ```json -{ - 'idPlace':int, //unique id of the place - 'idFloors': int[] // list of every floorID the place is in -} +[ + { + 'idPlace':int, //unique id of the place + 'idSite': int, //id of the place Site + 'idBuilding': int, //id of the place Building + 'idFloors': int[] //list of every floorID the place is in + }, + {...} +] ``` __Errors :__ - If _NAME_ doesn't correspond to any registered place : Return Error 404 "NotFoundHttpError". -- If _NAME_ has many occurence in the PlaceNameRepository : Return Error 500 "UnprocessableEntityHttpException" > The API can't decide which place should be provided. + +--------------------- + +### /api/map/get_site_name/ID + +> Return the name of the Site ID. + +__Errors :__ +- If _ID_ doesn't correspond to any registered Site : Return Error 404 "NotFoundHttpError". +- If _ID_ has many occurence in the SiteRepository : Return Error 500 > The Database is corrupted. + +--------------------- + +### /api/map/get_building_name/ID + +> Return the name of the Building ID. + +__Errors :__ +- If _ID_ doesn't correspond to any registered Building : Return Error 404 "NotFoundHttpError". +- If _ID_ has many occurence in the BuildingRepository : Return Error 500 > The Database is corrupted. + +--------------------- + +### /api/map/get_floor_name/ID + +> Return the name of the Floor ID. + +__Errors :__ +- If _ID_ doesn't correspond to any registered Floor : Return Error 404 "NotFoundHttpError". +- If _ID_ has many occurence in the FloorRepository : Return Error 500 > The Database is corrupted. + --------------------- ### /api/map/show_place_info/ID diff --git a/src/Controller/MapApiController.php b/src/Controller/MapApiController.php index e97f79b..42fc873 100644 --- a/src/Controller/MapApiController.php +++ b/src/Controller/MapApiController.php @@ -17,6 +17,7 @@ use Exception; use App\Repository\SiteRepository; +use App\Repository\BuildingRepository; use App\Repository\FloorRepository; use App\Repository\PlaceRepository; use App\Repository\PlaceNameRepository; @@ -53,6 +54,33 @@ class MapApiController extends AbstractController ]); } + #[Route('/api/map/get_site_name/{id}')] + public function get_site_name(SiteRepository $rep, int $id): JsonResponse + { + $sites = $rep->findSiteById($id); + if (sizeof($sites) == 0) { throw new NotFoundHttpException('Error: '.$id.' doesn\'t correspond to any place.'); } + elseif (sizeof($sites)>1) { throw new Exception('Internal Error: '.$id.' refers to more than one siteId ; the BDD is corrupted.'); } + return $this->json($sites[0]->getName()); + } + + #[Route('/api/map/get_building_name/{id}')] + public function get_building_name(BuildingRepository $rep, int $id): JsonResponse + { + $buildings = $rep->findBuildingById($id); + if (sizeof($buildings) == 0) { throw new NotFoundHttpException('Error: '.$id.' doesn\'t correspond to any place.'); } + elseif (sizeof($buildings)>1) { throw new Exception('Internal Error: '.$id.' refers to more than one buildingId ; the BDD is corrupted.'); } + return $this->json($buildings[0]->getName()); + } + + #[Route('/api/map/get_floor_name/{id}')] + public function get_floor_name(FloorRepository $rep, int $id): JsonResponse + { + $floors = $rep->findFloorById($id); + if (sizeof($floors) == 0) { throw new NotFoundHttpException('Error: '.$id.' doesn\'t correspond to any place.'); } + elseif (sizeof($floors)>1) { throw new Exception('Internal Error: '.$id.' refers to more than one floorId ; the BDD is corrupted.'); } + return $this->json($floors[0]->getName()); + } + #[Route('/api/map/get_floor/{id}/{representation}')] public function get_floor(FloorRepository $rep, int $id, string $representation='Cylinder'): JsonResponse { @@ -111,7 +139,12 @@ class MapApiController extends AbstractController foreach ($place->getFloors() as $floor) { $floorsId[] = $floor->getId(); } - $all[] = array('idRoom' => $place->getId(), 'idFloors' => $floorsId); + $building = $floorsId[0]->getBuilding(); + $all[] = array('idRoom' => $place->getId(), + 'idSite' => $building->getSite()->getId(), + 'idBuilding' => $building->getId(), + 'idFloors' => $floorsId + ); } return $this->json($all); } diff --git a/src/Repository/BuildingRepository.php b/src/Repository/BuildingRepository.php index 7b4ffef..3e43355 100644 --- a/src/Repository/BuildingRepository.php +++ b/src/Repository/BuildingRepository.php @@ -39,6 +39,20 @@ class BuildingRepository extends ServiceEntityRepository } } + /** + * @return Building[] Returns an array of Building objects + */ + public function findBuildingById(int $id): array + { + return $this->createQueryBuilder('b') + ->andWhere('b.id = :val') + ->setParameter('val', $id) + ->orderBy('b.id', 'ASC') + ->getQuery() + ->getResult() + ; + } + // /** // * @return Building[] Returns an array of Building objects // */ diff --git a/src/Repository/SiteRepository.php b/src/Repository/SiteRepository.php index 328b2c2..c455dba 100644 --- a/src/Repository/SiteRepository.php +++ b/src/Repository/SiteRepository.php @@ -44,6 +44,20 @@ class SiteRepository extends ServiceEntityRepository return $this->createQueryBuilder('s')->orderBy('s.id','ASC')->getQuery()->getResult(); } + + /** + * @return Site[] Returns an array of Site objects + */ + public function findSiteById(int $id): array + { + return $this->createQueryBuilder('s') + ->andWhere('s.id = :val') + ->setParameter('val', $id) + ->orderBy('s.id', 'ASC') + ->getQuery() + ->getResult() + ; + } // /** // * @return Site[] Returns an array of Site objects // */