Gestion des erreurs + renommage en api/map/ au lieu de map/api/

This commit is contained in:
Kouril42 2023-02-14 11:56:05 +01:00
parent 7048a47f16
commit aeff7fef0c
2 changed files with 39 additions and 30 deletions

View File

@ -4,11 +4,15 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Exception;
use ValueError;
use App\Repository\SiteRepository; use App\Repository\SiteRepository;
use App\Repository\FloorRepository; use App\Repository\FloorRepository;
use App\Repository\PlaceRepository; use App\Repository\PlaceRepository;
@ -26,7 +30,7 @@ class MapApiController extends AbstractController
{ {
//function get_Name($rep) {return $rep->getName();} //function get_Name($rep) {return $rep->getName();}
#[Route('/map/api/test')] #[Route('/api/map/test')]
public function test(PlaceNameRepository $pNRep, RoomUserNameRepository $uNRep, PlaceRepository $pRep, SiteRepository $sRep): JsonResponse public function test(PlaceNameRepository $pNRep, RoomUserNameRepository $uNRep, PlaceRepository $pRep, SiteRepository $sRep): JsonResponse
{ {
$place0 = $pRep->findPlaceById(1)[0]; $place0 = $pRep->findPlaceById(1)[0];
@ -42,13 +46,14 @@ class MapApiController extends AbstractController
]); ]);
} }
#[Route('/map/api/get_floor/{id}/{representation}')] #[Route('/api/map/get_floor/{id}/{representation}')]
public function get_floor(FloorRepository $rep, int $id, string $representation='Cylinder'): JsonResponse 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 //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 //it defines which representation should we use to project it on 2 dimension
$floors = $rep->findFloorById($id); $floors = $rep->findFloorById($id);
if (sizeof($floors)!=1) { return $this->json(["Error : None or Too Many Floors have this 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]; $floor = $floors[0];
$places = $floor->getPlaces(); $places = $floor->getPlaces();
$jsonPlaces = new ArrayCollection(); $jsonPlaces = new ArrayCollection();
@ -68,11 +73,12 @@ class MapApiController extends AbstractController
return $this->json($jsonPlaces->toArray()); return $this->json($jsonPlaces->toArray());
} }
#[Route('/map/api/find_place_by_name/{name}')] #[Route('/api/map/find_place_by_name/{name}')]
public function find_place_by_name(PlaceRepository $rep, string $name): JsonResponse public function find_place_by_name(PlaceRepository $rep, string $name): JsonResponse
{ {
$places = $rep->findPlaceByName($name); $places = $rep->findPlaceByName($name);
if (sizeof($places)!=1) { return $this->json([0]); } if (sizeof($places)==0){ throw new NotFoundHttpException('Error: \''.$name.'\' doesn\'t correspond to any placeName.'); }
elseif (sizeof($places)>1) { throw new UnprocessableEntityHttpException('Error: \''.$name.'\' refers to more than one place ; the API can\'t decide which one should be provided.'); }
else { else {
$place = $places[0]; $place = $places[0];
$floorsId = array(); $floorsId = array();
@ -87,13 +93,12 @@ class MapApiController extends AbstractController
} }
#[Route('/map/api/show_place_info/{id}')] #[Route('/api/map/show_place_info/{id}')]
public function show_place_info(PlaceRepository $rep, int $id): JsonResponse public function show_place_info(PlaceRepository $rep, int $id): JsonResponse
{ {
$places = $rep->findPlaceById($id); $places = $rep->findPlaceById($id);
if (sizeof($places)!=1) { if (sizeof($places) == 0) { throw new NotFoundHttpException('Error: '.$id.' doesn\'t correspond to any place.'); }
return $this->json([0]); elseif (sizeof($places)>1) { throw new Exception('Internal Error: '.$id.' refers to more than one placeId ; the BDD is corrupted.'); }
}
else { else {
return $this->json([ return $this->json([
'idPlace' => $places[0]->getId(), 'idPlace' => $places[0]->getId(),
@ -103,13 +108,11 @@ class MapApiController extends AbstractController
} }
} }
#[Route('/map/api/get_all_editions')] #[Route('/api/map/get_all_editions')]
public function getEditions(EditionRepository $rep): JsonResponse public function getEditions(EditionRepository $rep): JsonResponse
{ {
$edits = $rep->getAllEditions(); $edits = $rep->getAllEditions();
if (sizeof($edits)<1) { if (sizeof($edits)<1) { throw new NotFoundHttpException('Warning: There is no more edition to check.'); }
return $this->json([0]);
}
else { else {
$listEdits = array(); $listEdits = array();
foreach ($edits as $edit){ foreach ($edits as $edit){
@ -125,34 +128,36 @@ class MapApiController extends AbstractController
return $this->json($listEdits); return $this->json($listEdits);
} }
#[Route('/map/api/create_edition/{mode}/{placeId}/{value}')] #[Route('/api/map/create_edition/{mode}/{placeId}/{value}')]
public function createEdition(EditionRepository $Erep, PlaceRepository $Prep, string $mode, int $placeId, string $value): JsonResponse 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 //mode is the type of modification, can be AddPlaceName ; DelPlaceName ; AddRoomUserName or DelRoomUserName
$edit = new Edition(); $edit = new Edition();
if ($edit->initEdition($Prep, $mode, $placeId, $value) == null) {return $this->json(["Error during initEdition : None or Too Many Places found with id"]);} $edit->initEdition($Prep, $mode, $placeId, $value);
$Erep->add($edit, true); $Erep->add($edit, true);
return $this->json(["added"]); return $this->json(["added"]);
} }
#[Route('/map/api/remove_edition/{id}')] #[Route('/api/map/remove_edition/{id}')]
public function removeEdition(EditionRepository $rep, int $id): JsonResponse public function removeEdition(EditionRepository $rep, int $id): JsonResponse
{ {
$edit = $rep->findEditionById($id); $edit = $rep->findEditionById($id);
if (sizeof($edit) == 0) {return $this->json([0]);} 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); $rep->remove($edit[0], true);
return $this->json(["removed"]); return $this->json(["removed"]);
} }
#[Route('/map/api/add_place_name/{placeId}/{value}')] #[Route('/api/map/add_place_name/{placeId}/{value}')]
public function addPlaceName(PlaceRepository $pRep, PlaceNameRepository $pNRep, int $placeId, string $value): JsonResponse public function addPlaceName(PlaceRepository $pRep, PlaceNameRepository $pNRep, int $placeId, string $value): JsonResponse
{ {
$places = $pRep->findPlaceById($placeId); $places = $pRep->findPlaceById($placeId);
if (sizeof($places) != 1) { return $this->json([0]); } 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{ else{
$names = new ArrayCollection(); $names = new ArrayCollection();
foreach($places[0]->getNames() as $plName) { $names[] = $plName->getName(); } foreach($places[0]->getNames() as $plName) { $names[] = $plName->getName(); }
if ($names->contains($value)) { return $this->json([2]); } if ($names->contains($value)) { throw new ValueError('Warning: \''.$value.'\' is already a name for place '.$placeId);}
else { else {
$pN = new PlaceName(); $pN = new PlaceName();
$pN->setPlace($places[0]); $pN->setPlace($places[0]);
@ -162,25 +167,26 @@ class MapApiController extends AbstractController
} }
} }
} }
#[Route('/map/api/del_place_name/{placeId}/{value}')] #[Route('/api/map/del_place_name/{placeId}/{value}')]
public function delPlaceName(PlaceNameRepository $pNRep, int $placeId, string $value): JsonResponse public function delPlaceName(PlaceNameRepository $pNRep, int $placeId, string $value): JsonResponse
{ {
$pN = $pNRep->findPlaceName($placeId, $value); $pN = $pNRep->findPlaceName($placeId, $value);
if (sizeof($pN)==0) { return $this->json([0]); } if (sizeof($pN)==0) { throw new NotFoundHttpException('Error: \''.$value.'\' isn\'t registered as a name for the place '.$placeId); }
$pNRep->remove($pN[0], true); $pNRep->remove($pN[0], true);
return $this->json(["removed"]); return $this->json(["removed"]);
} }
#[Route('/map/api/add_room_user_name/{placeId}/{value}')] #[Route('/api/map/add_room_user_name/{placeId}/{value}')]
public function addRoomUserName(PlaceRepository $pRep, RoomUserNameRepository $rURep, ManagerRegistry $mr, int $placeId, string $value): JsonResponse public function addRoomUserName(PlaceRepository $pRep, RoomUserNameRepository $rURep, int $placeId, string $value): JsonResponse
{ {
$places = $pRep->findPlaceById($placeId); $places = $pRep->findPlaceById($placeId);
if (sizeof($places) != 1) { return $this->json([0]); } 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{ else{
$names = new ArrayCollection(); $names = new ArrayCollection();
foreach($places[0]->getRoomUsers() as $rUser) { $names[] = $rUser->getUserName(); } foreach($places[0]->getRoomUsers() as $rUser) { $names[] = $rUser->getUserName(); }
if ($names->contains($value)) { return $this->json([2]); } if ($names->contains($value)) { throw new ValueError('Warning: \''.$value.'\' is already a name for place '.$placeId);}
else { else {
$rU = new RoomUserName(); $rU = new RoomUserName();
$rU->setPlace($places[0]); $rU->setPlace($places[0]);
@ -190,11 +196,11 @@ class MapApiController extends AbstractController
} }
} }
} }
#[Route('/map/api/del_room_user_name/{placeId}/{value}')] #[Route('/api/map/del_room_user_name/{placeId}/{value}')]
public function delRoomUserName(RoomUserNameRepository $uRep, int $placeId, string $value): JsonResponse public function delRoomUserName(RoomUserNameRepository $uRep, int $placeId, string $value): JsonResponse
{ {
$rU = $uRep->findRoomUserName($placeId, $value); $rU = $uRep->findRoomUserName($placeId, $value);
if (sizeof($rU)==0) { return $this->json([0]); } if (sizeof($rU)==0) { throw new NotFoundHttpException('Error: \''.$value.'\' isn\'t registered as a user for the place '.$placeId); }
$uRep->remove($rU[0], true); $uRep->remove($rU[0], true);
return $this->json(["removed"]); return $this->json(["removed"]);
} }

View File

@ -7,6 +7,8 @@ use App\Repository\PlaceRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Exception;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
#[ORM\Entity(repositoryClass: EditionRepository::class)] #[ORM\Entity(repositoryClass: EditionRepository::class)]
class Edition class Edition
@ -60,7 +62,8 @@ class Edition
public function initEdition(PlaceRepository $rep, string $mode, int $id, string $val): self public function initEdition(PlaceRepository $rep, string $mode, int $id, string $val): self
{ {
$places = $rep->findPlaceById($id); $places = $rep->findPlaceById($id);
if (sizeof($places)!=1) { return null; } 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.'); }
$this->mode = $mode; $this->mode = $mode;
$this->editedPlace = $places[0]; $this->editedPlace = $places[0];
$this->val = $val; $this->val = $val;