MAPG-125 add deleteMap endpoint

This commit is contained in:
Bence Pőcze 2020-06-14 02:36:23 +02:00
parent d1d9dede9c
commit a6d5214166
2 changed files with 37 additions and 0 deletions

View File

@ -74,6 +74,8 @@ class MapAdminController implements ISecured
{ {
$mapId = (int) $this->request->query('mapId'); $mapId = (int) $this->request->query('mapId');
\Container::$dbConnection->startTransaction();
if (!$mapId) { if (!$mapId) {
$mapId = $this->addNewMap(); $mapId = $this->addNewMap();
} }
@ -131,10 +133,44 @@ class MapAdminController implements ISecured
$this->saveMapData($mapId, $map); $this->saveMapData($mapId, $map);
\Container::$dbConnection->commit();
$data = ['mapId' => $mapId, 'added' => $addedIds]; $data = ['mapId' => $mapId, 'added' => $addedIds];
return new JsonContent($data); return new JsonContent($data);
} }
public function deleteMap() {
$mapId = (int) $this->request->query('mapId');
\Container::$dbConnection->startTransaction();
$this->deletePlaces($mapId);
$modify = new Modify(\Container::$dbConnection, 'maps');
$modify->setId($mapId);
$modify->delete();
\Container::$dbConnection->commit();
$data = ['success' => true];
return new JsonContent($data);
}
private function deletePlaces(int $mapId): void
{
$select = new Select(\Container::$dbConnection, 'places');
$select->columns(['id']);
$select->where('map_id', '=', $mapId);
$result = $select->execute();
while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) {
$modify = new Modify(\Container::$dbConnection, 'places');
$modify->setId($place['id']);
$modify->delete();
}
}
private function calculateMapBounds(int $mapId): Bounds private function calculateMapBounds(int $mapId): Bounds
{ {
$select = new Select(\Container::$dbConnection, 'places'); $select = new Select(\Container::$dbConnection, 'places');

View File

@ -27,6 +27,7 @@ Container::$routeCollection->group('admin', function (MapGuesser\Routing\RouteCo
$routeCollection->get('admin.mapEditor', 'mapEditor/{mapId?}', [MapGuesser\Controller\MapAdminController::class, 'getMapEditor']); $routeCollection->get('admin.mapEditor', 'mapEditor/{mapId?}', [MapGuesser\Controller\MapAdminController::class, 'getMapEditor']);
$routeCollection->get('admin.place', 'place.json/{placeId}', [MapGuesser\Controller\MapAdminController::class, 'getPlace']); $routeCollection->get('admin.place', 'place.json/{placeId}', [MapGuesser\Controller\MapAdminController::class, 'getPlace']);
$routeCollection->post('admin.saveMap', 'saveMap/{mapId}/json', [MapGuesser\Controller\MapAdminController::class, 'saveMap']); $routeCollection->post('admin.saveMap', 'saveMap/{mapId}/json', [MapGuesser\Controller\MapAdminController::class, 'saveMap']);
$routeCollection->post('admin.deleteMap', 'deleteMap/{mapId}', [MapGuesser\Controller\MapAdminController::class, 'deleteMap']);
}); });
Container::$sessionHandler = new MapGuesser\Session\DatabaseSessionHandler(); Container::$sessionHandler = new MapGuesser\Session\DatabaseSessionHandler();