MAPG-140 adapt MapRepository and classes that use it to use persistent model
This commit is contained in:
parent
eafa98571e
commit
821a9d80c0
@ -1,7 +1,6 @@
|
||||
<?php namespace MapGuesser\Controller;
|
||||
|
||||
use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Util\Geo\Bounds;
|
||||
use MapGuesser\Response\HtmlContent;
|
||||
use MapGuesser\Response\JsonContent;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
@ -37,18 +36,16 @@ class GameController
|
||||
{
|
||||
$map = $this->mapRepository->getById($mapId);
|
||||
|
||||
$bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']);
|
||||
|
||||
$session = $this->request->session();
|
||||
|
||||
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
||||
$session->set('state', [
|
||||
'mapId' => $mapId,
|
||||
'area' => $map['area'],
|
||||
'area' => $map->getArea(),
|
||||
'rounds' => []
|
||||
]);
|
||||
}
|
||||
|
||||
return ['mapId' => $mapId, 'mapName' => $map['name'], 'bounds' => $bounds->toArray()];
|
||||
return ['mapId' => $mapId, 'mapName' => $map->getName(), 'bounds' => $map->getBounds()->toArray()];
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,8 @@ use MapGuesser\Interfaces\Authorization\ISecured;
|
||||
use MapGuesser\Interfaces\Database\IResultSet;
|
||||
use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
use MapGuesser\PersistentData\Model\Map;
|
||||
use MapGuesser\PersistentData\PersistentDataManager;
|
||||
use MapGuesser\Repository\MapRepository;
|
||||
use MapGuesser\Repository\PlaceRepository;
|
||||
use MapGuesser\Response\HtmlContent;
|
||||
@ -21,6 +23,8 @@ class MapAdminController implements ISecured
|
||||
|
||||
private IRequest $request;
|
||||
|
||||
private PersistentDataManager $pdm;
|
||||
|
||||
private MapRepository $mapRepository;
|
||||
|
||||
private PlaceRepository $placeRepository;
|
||||
@ -28,6 +32,7 @@ class MapAdminController implements ISecured
|
||||
public function __construct(IRequest $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->pdm = new PersistentDataManager();
|
||||
$this->mapRepository = new MapRepository();
|
||||
$this->placeRepository = new PlaceRepository();
|
||||
}
|
||||
@ -45,18 +50,14 @@ class MapAdminController implements ISecured
|
||||
|
||||
if ($mapId) {
|
||||
$map = $this->mapRepository->getById($mapId);
|
||||
$bounds = Bounds::createDirectly($map['bound_south_lat'], $map['bound_west_lng'], $map['bound_north_lat'], $map['bound_east_lng']);
|
||||
$places = $this->getPlaces($mapId);
|
||||
} else {
|
||||
$map = [
|
||||
'name' => self::$unnamedMapName,
|
||||
'description' => ''
|
||||
];
|
||||
$bounds = Bounds::createDirectly(-90.0, -180.0, 90.0, 180.0);
|
||||
$map = new Map();
|
||||
$map->setName(self::$unnamedMapName);
|
||||
$places = [];
|
||||
}
|
||||
|
||||
$data = ['mapId' => $mapId, 'mapName' => $map['name'], 'mapDescription' => str_replace('<br>', "\n", $map['description']), 'bounds' => $bounds->toArray(), 'places' => &$places];
|
||||
$data = ['mapId' => $mapId, 'mapName' => $map->getName(), 'mapDescription' => str_replace('<br>', "\n", $map->getDescription()), 'bounds' => $map->getBounds()->toArray(), 'places' => &$places];
|
||||
return new HtmlContent('admin/map_editor', $data);
|
||||
}
|
||||
|
||||
@ -76,8 +77,13 @@ class MapAdminController implements ISecured
|
||||
|
||||
\Container::$dbConnection->startTransaction();
|
||||
|
||||
if (!$mapId) {
|
||||
$mapId = $this->addNewMap();
|
||||
if ($mapId) {
|
||||
$map = $this->mapRepository->getById($mapId);
|
||||
} else {
|
||||
$map = new Map();
|
||||
$map->setName(self::$unnamedMapName);
|
||||
$this->pdm->saveToDb($map);
|
||||
$mapId = $map->getId();
|
||||
}
|
||||
|
||||
if (isset($_POST['added'])) {
|
||||
@ -116,22 +122,17 @@ class MapAdminController implements ISecured
|
||||
|
||||
$mapBounds = $this->calculateMapBounds($mapId);
|
||||
|
||||
$map = [
|
||||
'bound_south_lat' => $mapBounds->getSouthLat(),
|
||||
'bound_west_lng' => $mapBounds->getWestLng(),
|
||||
'bound_north_lat' => $mapBounds->getNorthLat(),
|
||||
'bound_east_lng' => $mapBounds->getEastLng(),
|
||||
'area' => $mapBounds->calculateApproximateArea(),
|
||||
];
|
||||
$map->setBounds($mapBounds);
|
||||
$map->setArea($mapBounds->calculateApproximateArea());
|
||||
|
||||
if (isset($_POST['name'])) {
|
||||
$map['name'] = $_POST['name'] ? $_POST['name'] : self::$unnamedMapName;
|
||||
$map->setName($_POST['name'] ? $_POST['name'] : self::$unnamedMapName);
|
||||
}
|
||||
if (isset($_POST['description'])) {
|
||||
$map['description'] = str_replace(["\n", "\r\n"], '<br>', $_POST['description']);
|
||||
$map->setDescription(str_replace(["\n", "\r\n"], '<br>', $_POST['description']));
|
||||
}
|
||||
|
||||
$this->saveMapData($mapId, $map);
|
||||
$this->pdm->saveToDb($map);
|
||||
|
||||
\Container::$dbConnection->commit();
|
||||
|
||||
@ -142,13 +143,13 @@ class MapAdminController implements ISecured
|
||||
public function deleteMap() {
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
|
||||
$map = $this->mapRepository->getById($mapId);
|
||||
|
||||
\Container::$dbConnection->startTransaction();
|
||||
|
||||
$this->deletePlaces($mapId);
|
||||
|
||||
$modify = new Modify(\Container::$dbConnection, 'maps');
|
||||
$modify->setId($mapId);
|
||||
$modify->delete();
|
||||
$this->pdm->deleteFromDb($map);
|
||||
|
||||
\Container::$dbConnection->commit();
|
||||
|
||||
@ -187,30 +188,6 @@ class MapAdminController implements ISecured
|
||||
return $bounds;
|
||||
}
|
||||
|
||||
private function addNewMap(): int
|
||||
{
|
||||
$modify = new Modify(\Container::$dbConnection, 'maps');
|
||||
$modify->fill([
|
||||
'name' => self::$unnamedMapName,
|
||||
'description' => '',
|
||||
'bound_south_lat' => 0.0,
|
||||
'bound_west_lng' => 0.0,
|
||||
'bound_north_lat' => 0.0,
|
||||
'bound_east_lng' => 0.0
|
||||
]);
|
||||
$modify->save();
|
||||
|
||||
return $modify->getId();
|
||||
}
|
||||
|
||||
private function saveMapData(int $mapId, array $map): void
|
||||
{
|
||||
$modify = new Modify(\Container::$dbConnection, 'maps');
|
||||
$modify->setId($mapId);
|
||||
$modify->fill($map);
|
||||
$modify->save();
|
||||
}
|
||||
|
||||
private function &getPlaces(int $mapId): array
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection, 'places');
|
||||
|
@ -19,6 +19,7 @@ class MapsController
|
||||
|
||||
public function getMaps(): IContent
|
||||
{
|
||||
//TODO: from repository
|
||||
$select = new Select(\Container::$dbConnection, 'maps');
|
||||
$select->columns([
|
||||
['maps', 'id'],
|
||||
|
@ -1,16 +1,19 @@
|
||||
<?php namespace MapGuesser\Repository;
|
||||
|
||||
use MapGuesser\Database\Query\Select;
|
||||
use MapGuesser\Interfaces\Database\IResultSet;
|
||||
use MapGuesser\PersistentData\Model\Map;
|
||||
use MapGuesser\PersistentData\PersistentDataManager;
|
||||
|
||||
class MapRepository
|
||||
{
|
||||
public function getById(int $mapId)
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection, 'maps');
|
||||
$select->columns(['id', 'name', 'description', 'bound_south_lat', 'bound_west_lng', 'bound_north_lat', 'bound_east_lng', 'area']);
|
||||
$select->whereId($mapId);
|
||||
private PersistentDataManager $pdm;
|
||||
|
||||
return $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
||||
public function __construct()
|
||||
{
|
||||
$this->pdm = new PersistentDataManager();
|
||||
}
|
||||
|
||||
public function getById(int $mapId): ?Map
|
||||
{
|
||||
return $this->pdm->selectFromDbById($mapId, Map::class);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user