Modify Connections to be symetric and to be beautiful + add connectedFloors to get_floor

This commit is contained in:
Kouril42 2023-02-21 20:05:01 +01:00
parent 5cc241bdfa
commit 1e67114d82
6 changed files with 102 additions and 37 deletions

View File

@ -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

View File

@ -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;
@ -38,6 +40,39 @@ class JsonReaderController extends AbstractController
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
{

View File

@ -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()
]);
}
}

View File

@ -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<int, self>
*/
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<int, self>
*/
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<int, self>
@ -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;
}

View File

@ -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')

View File

@ -40,6 +40,11 @@ class PlaceRepository extends ServiceEntityRepository
}
}
public function flush(): void
{
$this->getEntityManager()->flush();
}
/**
* @return Place[] Returns an array of Place objects
*/