Encartes-backend/src/Controller/MapApiController.php
2023-02-19 12:44:44 +01:00

271 lines
12 KiB
PHP

<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Common\Collections\ArrayCollection;
use Exception;
use App\Repository\SiteRepository;
use App\Repository\BuildingRepository;
use App\Repository\FloorRepository;
use App\Repository\PlaceRepository;
use App\Repository\PlaceNameRepository;
use App\Repository\RoomUserNameRepository;
use App\Repository\EditionRepository;
use App\Entity\PlaceName;
use App\Entity\RoomUserName;
use App\Entity\Edition;
class MapApiController extends AbstractController
{
//function get_Name($rep) {return $rep->getName();}
#[Route('/api/teapot')]
public function teapot(PlaceNameRepository $pNRep, RoomUserNameRepository $uNRep, PlaceRepository $pRep, SiteRepository $sRep): JsonResponse
{
throw new HttpException(418);
}
#[Route('/api/map/test')]
public function test(PlaceNameRepository $pNRep, RoomUserNameRepository $uNRep, PlaceRepository $pRep, SiteRepository $sRep): JsonResponse
{
$place0 = $pRep->findPlaceById(1)[0];
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/MapApiController.php',
'allUnames' => $uNRep->allNames(),
'allPnames' => $pNRep->allNames(),
'Place0Id' => $place0->getId(),
'Place0UserNames0' => $place0->getRoomUsers()[0]->getUserName(),
'Place0Names0' => $place0->getNames()[0]->getName(),
'sites' => $sRep->allSites()
]);
}
#[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 site.'); }
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 building.'); }
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 floor.'); }
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
{
//id is the floor's id, representation being Cylinder or PolySurface or other if implemented
//it defines which representation should we use to project it on 2 dimension
$floors = $rep->findFloorById($id);
if (sizeof($floors)==0){ throw new NotFoundHttpException('Error: '.$id.' doesn\'t correspond to any floor.'); }
elseif (sizeof($floors)>1) { throw new Exception('Internal Error: '.$id.' refers to more than one floorId ; the BDD is corrupted.'); }
$floor = $floors[0];
$places = $floor->getPlaces();
$jsonPlaces = array();
if ($representation == 'Cylinder' || $representation == 'PolySurface'){
foreach ($places as $place){
$jsonPlaces[] = array(
'id' => $place->getId(),
'names' => $place->getJoinedNames(),
'type' => $place->getType(),
'surface' => $place->getTwoDRepresentation($representation, $floor->getAltitude())
);
}
}
else {
return $this->json(["Error on represantation attribute"]);
}
$building = $floor->getBuilding();
return $this->json(['idSite' => $building->getSite()->getId(),
'idBuilding' => $building->getId(),
'places' => $jsonPlaces
]);
}
#[Route('/api/map/get_all_floors/')]
public function get_all_floors(SiteRepository $sRep): JsonResponse
{
$sites = $sRep->allSites();
$response = array();
foreach ($sites as $s) {
$siteJSON = array();
foreach ($s->getBuildings() as $b) {
$buildJSON = array();
foreach ($b->getFloors() as $f){
$buildJSON[] = array(['name' => $f->getName(), 'id' => $f->getId()]);
}
$siteJSON[] = array(['name' => $b->getName(), 'id' => $b->getId(), 'floors' => $buildJSON]);
}
$response[] = array(['name' => $s->getName(), 'id' => $s->getId(), 'buildings' => $siteJSON]);
}
return $this->json($response);
}
#[Route('/api/map/find_place_by_name/{name}')]
public function find_place_by_name(PlaceRepository $rep, string $name): JsonResponse
{
$places = $rep->findPlaceByName($name);
if (sizeof($places)==0){ throw new NotFoundHttpException('Error: \''.$name.'\' doesn\'t correspond to any placeName.'); }
else {
$all = array();
foreach ($places as $place){
$floorsId = array();
foreach ($place->getFloors() as $floor) {
$floorsId[] = $floor->getId();
}
$building = $place->getFloors()[0]->getBuilding();
$all[] = array('idRoom' => $place->getId(),
'idSite' => $building->getSite()->getId(),
'idBuilding' => $building->getId(),
'idFloors' => $floorsId
);
}
return $this->json($all);
}
}
#[Route('/api/map/show_place_info/{id}')]
public function show_place_info(PlaceRepository $rep, int $id): JsonResponse
{
$places = $rep->findPlaceById($id);
if (sizeof($places) == 0) { throw new NotFoundHttpException('Error: '.$id.' doesn\'t correspond to any place.'); }
elseif (sizeof($places)>1) { throw new Exception('Internal Error: '.$id.' refers to more than one placeId ; the BDD is corrupted.'); }
else {
return $this->json([
'idPlace' => $places[0]->getId(),
'names' => $places[0]->getJoinedNames(),
'users' => $places[0]->getJoinedRoomUsersNames()
]);
}
}
#[Route('/api/map/get_all_editions')]
public function getEditions(EditionRepository $rep): JsonResponse
{
$edits = $rep->getAllEditions();
if (sizeof($edits)<1) { throw new NotFoundHttpException('Warning: There is no more edition to check.'); }
else {
$listEdits = array();
foreach ($edits as $edit){
$listEdits[] = array(
'id' => $edit->getId(),
'mode' => $edit->getMode(),
'placeNames' => $edit->getEditedPlace()->getJoinedNames(),
'placeId' => $edit->getEditedPlace()->getId(),
'editorValue' => $edit->getValue()
);
}
}
return $this->json($listEdits);
}
#[Route('/api/map/create_edition/{mode}/{placeId}/{value}')]
public function createEdition(EditionRepository $Erep, PlaceRepository $Prep, string $mode, int $placeId, string $value): JsonResponse
{
//mode is the type of modification, can be AddPlaceName ; DelPlaceName ; AddRoomUserName or DelRoomUserName
$edit = new Edition();
$edit->initEdition($Prep, $mode, $placeId, $value);
$Erep->add($edit, true);
return $this->json(["added"]);
}
#[Route('/api/map/remove_edition/{id}')]
public function removeEdition(EditionRepository $rep, int $id): JsonResponse
{
$edit = $rep->findEditionById($id);
if (sizeof($edit) == 0) {throw new NotFoundHttpException('Error: '.$id.' doesn\'t correspond to any edit.'); }
elseif (sizeof($places)>1) { throw new Exception('Internal Error: '.$id.' refers to more than one edit ; the BDD is corrupted.'); }
$rep->remove($edit[0], true);
return $this->json(["removed"]);
}
#[Route('/api/map/add_place_name/{placeId}/{value}')]
public function addPlaceName(PlaceRepository $pRep, PlaceNameRepository $pNRep, int $placeId, string $value): JsonResponse
{
$places = $pRep->findPlaceById($placeId);
if (sizeof($places) == 0) { throw new NotFoundHttpException('Error: '.$placeId.' doesn\'t correspond to any place.'); }
elseif (sizeof($places)>1) { throw new Exception('Internal Error: '.$placeId.' refers to more than one placeId ; the BDD is corrupted.'); }
else{
$names = new ArrayCollection();
foreach($places[0]->getNames() as $plName) { $names[] = $plName->getName(); }
if ($names->contains($value)) { throw new PreconditionFailedHttpException('Warning: \''.$value.'\' is already a name for place '.$placeId);}
else {
$pN = new PlaceName();
$pN->setPlace($places[0]);
$pN->setName($value);
$pNRep->add($pN, true);
return $this->json(["added"]);
}
}
}
#[Route('/api/map/del_place_name/{placeId}/{value}')]
public function delPlaceName(PlaceNameRepository $pNRep, int $placeId, string $value): JsonResponse
{
$pN = $pNRep->findPlaceName($placeId, $value);
if (sizeof($pN)==0) { throw new NotFoundHttpException('Error: \''.$value.'\' isn\'t registered as a name for the place '.$placeId); }
$pNRep->remove($pN[0], true);
return $this->json(["removed"]);
}
#[Route('/api/map/add_room_user_name/{placeId}/{value}')]
public function addRoomUserName(PlaceRepository $pRep, RoomUserNameRepository $rURep, int $placeId, string $value): JsonResponse
{
$places = $pRep->findPlaceById($placeId);
if (sizeof($places) == 0) { throw new NotFoundHttpException('Error: '.$placeId.' doesn\'t correspond to any placeId.'); }
elseif (sizeof($places)>1) { throw new Exception('Internal Error: '.$placeId.' refers to more than one placeId ; the BDD is corrupted.'); }
else{
$names = new ArrayCollection();
foreach($places[0]->getRoomUsers() as $rUser) { $names[] = $rUser->getUserName(); }
if ($names->contains($value)) { throw new PreconditionFailedHttpException('Warning: \''.$value.'\' is already a name for place '.$placeId);}
else {
$rU = new RoomUserName();
$rU->setPlace($places[0]);
$rU->setUserName($value);
$rURep->add($rU, true);
return $this->json(["added"]);
}
}
}
#[Route('/api/map/del_room_user_name/{placeId}/{value}')]
public function delRoomUserName(RoomUserNameRepository $uRep, int $placeId, string $value): JsonResponse
{
$rU = $uRep->findRoomUserName($placeId, $value);
if (sizeof($rU)==0) { throw new NotFoundHttpException('Error: \''.$value.'\' isn\'t registered as a user for the place '.$placeId); }
$uRep->remove($rU[0], true);
return $this->json(["removed"]);
}
}