diff --git a/README.md b/README.md index 4f2c691..49a23e3 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,4 @@ Your installation should be complete, you can run `symfony local:server:start` t - [x] API documentation - [ ] Use a representation superclass (both db and php) - [ ] Use a php enum and a pgsql enum for place type +- [ ] Change get_floors (connectedFloors) to SQL request diff --git a/src/Controller/JsonReaderController.php b/src/Controller/JsonReaderController.php index 6a98735..350f8d5 100644 --- a/src/Controller/JsonReaderController.php +++ b/src/Controller/JsonReaderController.php @@ -5,10 +5,12 @@ namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; use Doctrine\Persistence\ManagerRegistry; use Doctrine\ORM\EntityManagerInterface; use App\Repository\SiteRepository; +use App\Repository\PlaceRepository; use App\Repository\BuildingRepository; use App\Repository\FloorRepository; use App\Entity\Building; @@ -37,6 +39,39 @@ class JsonReaderController extends AbstractController $response->setStatusCode(Response::HTTP_OK); return $response; } + + #[Route('/api/debug/add_connection/{id1}/{id2}')] + public function add_connection(PlaceRepository $rep, int $id1, int $id2): JsonResponse + { + $place1 = $rep->findPlaceById($id1)[0]; + $place2 = $rep->findPlaceById($id2)[0]; + if (!($place1 && $place2)) { return $this->json("Error"); } + if (!$place1->addConnection($place2)) { return $this->json("Error 2"); } + $rep->flush(); + return $this->json("Added"); + } + #[Route('/api/debug/remove_connection/{id1}/{id2}')] + public function remove_connection(PlaceRepository $rep, int $id1, int $id2): JsonResponse + { + $place1 = $rep->findPlaceById($id1)[0]; + $place2 = $rep->findPlaceById($id2)[0]; + if (!($place1 && $place2)) { return $this->json("Error"); } + if (!$place1->removeConnection($place2)) { return $this->json("Error 2"); } + $rep->flush(); + return $this->json("Removed"); + } + #[Route('/api/debug/show_connection/{id1}')] + public function show_connection(PlaceRepository $rep, int $id1): JsonResponse + { + $place1 = $rep->findPlaceById($id1)[0]; + if (!$place1) { return $this->json("Error"); } + $response = array(); + foreach ($place1->getConnectedPlaces() as $pl) { + $response[] = $pl->getId(); + } + return $this->json($response); + } + #[Route('/api/debug/initialData')] public function init(Request $request, SiteRepository $srep, BuildingRepository $brep, FloorRepository $frep, ManagerRegistry $mr): Response diff --git a/src/Controller/MapApiController.php b/src/Controller/MapApiController.php index aad6475..626d846 100644 --- a/src/Controller/MapApiController.php +++ b/src/Controller/MapApiController.php @@ -94,11 +94,22 @@ class MapApiController extends AbstractController $jsonPlaces = array(); if ($representation == 'Cylinder' || $representation == 'PolySurface'){ foreach ($places as $place){ + $connectedFloors = new ArrayCollection(); + $connectedFloors[] = $floor->getId(); + foreach ($place->getConnectedPlaces() as $cp) { + foreach ($cp->getFloors() as $cf) { + if (!$connectedFloors->contains($cf->getId())){ + $connectedFloors[] = $cf->getId(); + } + } + } + $connectedFloors->removeElement($floor->getId()); $jsonPlaces[] = array( 'id' => $place->getId(), 'names' => $place->getJoinedNames(), 'type' => $place->getType(), - 'surface' => $place->getTwoDRepresentation($representation, $floor->getAltitude()) + 'surface' => $place->getTwoDRepresentation($representation, $floor->getAltitude()), + 'connectedFloors' => $connectedFloors->toArray() #$rep->getFloorsIdConnectedToPlaceID($place->getId()) // Will be added later ); } } @@ -165,7 +176,8 @@ class MapApiController extends AbstractController return $this->json([ 'idPlace' => $places[0]->getId(), 'names' => $places[0]->getJoinedNames(), - 'users' => $places[0]->getJoinedRoomUsersNames() + 'users' => $places[0]->getJoinedRoomUsersNames(), + 'floors' => $places[0]->getFloorsId() ]); } } diff --git a/src/Entity/Place.php b/src/Entity/Place.php index 4070528..8c817d8 100644 --- a/src/Entity/Place.php +++ b/src/Entity/Place.php @@ -28,8 +28,6 @@ class Place private $floors; #[ORM\ManyToMany(targetEntity: Place::class, inversedBy: "connectedPlaces")] - private $connection; - #[ORM\ManyToMany(targetEntity: Place::class, mappedBy: "connection")] private $connectedPlaces; #[ORM\OneToMany(targetEntity: ThreeDObjectFile::class, mappedBy: "place")] @@ -46,7 +44,6 @@ class Place public function __construct() { - $this->connection = new ArrayCollection(); $this->connectedPlaces = new ArrayCollection(); $this->names = new ArrayCollection(); $this->users = new ArrayCollection(); @@ -77,12 +74,12 @@ class Place /** * @return Collection */ - public function getNames(): Collection + public function getNames(): Collection //of PlaceName { return $this->names; } - public function getJoinedNames(): array + public function getJoinedNames(): array //of sting { $names = array(); foreach ($this->names as $plName) { @@ -160,6 +157,14 @@ class Place { return $this->floors; } + public function getFloorsId(): array + { + $response = array(); + foreach ($this->floors as $f) { + $response[] = $f->getId(); + } + return $response; + } public function addFloor(Floor $floor): self { @@ -174,27 +179,6 @@ class Place $this->floors->removeElement($floor); return $this; } - /** - * @return Collection - */ - public function getConnection(): Collection - { - return $this->connection; - } - - public function addConnection(self $connection): self - { - if (!$this->connection->contains($connection)) { - $this->connection[] = $connection; - } - return $this; - } - - public function removeConnection(self $connection): self - { - $this->connection->removeElement($connection); - return $this; - } /** * @return Collection @@ -204,20 +188,29 @@ class Place return $this->connectedPlaces; } - public function addConnectedPlace(self $connectedPlace): self + public function addConnection(self $connection): self { - if (!$this->connectedPlaces->contains($connectedPlace)) { - $this->connectedPlaces[] = $connectedPlace; - $connectedPlace->addConnection($this); + $this->addConnectedPlace($connection); + $connection->addConnectedPlace($this); + return $this; + } + private function addConnectedPlace(Place $place_to_connect): self + { + if (!$this->connectedPlaces->contains($place_to_connect)) { + $this->connectedPlaces[] = $place_to_connect; } return $this; } - - public function removeConnectedPlace(self $connectedPlace): self + + public function removeConnection(self $connection): self { - if ($this->connectedPlaces->removeElement($connectedPlace)) { - $connectedPlace->removeConnection($this); - } + $this->removeConnectedPlace($connection); + $connection->removeConnectedPlace($this); + return $this; + } + private function removeConnectedPlace(Place $place_to_deconnect): self + { + $this->connectedPlaces->removeElement($place_to_deconnect); return $this; } diff --git a/src/Repository/FloorRepository.php b/src/Repository/FloorRepository.php index b23f5c5..1152756 100644 --- a/src/Repository/FloorRepository.php +++ b/src/Repository/FloorRepository.php @@ -52,6 +52,25 @@ class FloorRepository extends ServiceEntityRepository ->getResult() ; } +//Will be implemented later + /** + * @return int[] Returns an array of FloorId int + */ + // public function getFloorsIdConnectedToPlaceID(int $id): array + // { + // return $this->createQueryBuilder('f') + // ->select('f.id') + // ->join('App\Entity\Place', 'p', 'WITH', 'p IN f.places') + // ->andWhere(':val = p.id') + // ->setParameter('val', $id) + // ->orderBy('f.id', 'ASC') + // ->getQuery() + // ->getResult() + // ; + // } + + + // public function findByExampleField($value): array // { // return $this->createQueryBuilder('f') diff --git a/src/Repository/PlaceRepository.php b/src/Repository/PlaceRepository.php index c553e7c..7fcfdbb 100644 --- a/src/Repository/PlaceRepository.php +++ b/src/Repository/PlaceRepository.php @@ -40,6 +40,11 @@ class PlaceRepository extends ServiceEntityRepository } } + public function flush(): void + { + $this->getEntityManager()->flush(); + } + /** * @return Place[] Returns an array of Place objects */