MAPG-140 adapt MapRepository and classes that use it to use persistent model

This commit is contained in:
Bence Pőcze 2020-06-19 18:52:07 +02:00
parent eafa98571e
commit 821a9d80c0
4 changed files with 37 additions and 59 deletions

View File

@ -1,7 +1,6 @@
<?php namespace MapGuesser\Controller; <?php namespace MapGuesser\Controller;
use MapGuesser\Interfaces\Request\IRequest; use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\Response\HtmlContent; use MapGuesser\Response\HtmlContent;
use MapGuesser\Response\JsonContent; use MapGuesser\Response\JsonContent;
use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Interfaces\Response\IContent;
@ -37,18 +36,16 @@ class GameController
{ {
$map = $this->mapRepository->getById($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']);
$session = $this->request->session(); $session = $this->request->session();
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) { if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
$session->set('state', [ $session->set('state', [
'mapId' => $mapId, 'mapId' => $mapId,
'area' => $map['area'], 'area' => $map->getArea(),
'rounds' => [] 'rounds' => []
]); ]);
} }
return ['mapId' => $mapId, 'mapName' => $map['name'], 'bounds' => $bounds->toArray()]; return ['mapId' => $mapId, 'mapName' => $map->getName(), 'bounds' => $map->getBounds()->toArray()];
} }
} }

View File

@ -8,6 +8,8 @@ use MapGuesser\Interfaces\Authorization\ISecured;
use MapGuesser\Interfaces\Database\IResultSet; use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Interfaces\Request\IRequest; use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Interfaces\Response\IContent;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\PersistentDataManager;
use MapGuesser\Repository\MapRepository; use MapGuesser\Repository\MapRepository;
use MapGuesser\Repository\PlaceRepository; use MapGuesser\Repository\PlaceRepository;
use MapGuesser\Response\HtmlContent; use MapGuesser\Response\HtmlContent;
@ -21,6 +23,8 @@ class MapAdminController implements ISecured
private IRequest $request; private IRequest $request;
private PersistentDataManager $pdm;
private MapRepository $mapRepository; private MapRepository $mapRepository;
private PlaceRepository $placeRepository; private PlaceRepository $placeRepository;
@ -28,6 +32,7 @@ class MapAdminController implements ISecured
public function __construct(IRequest $request) public function __construct(IRequest $request)
{ {
$this->request = $request; $this->request = $request;
$this->pdm = new PersistentDataManager();
$this->mapRepository = new MapRepository(); $this->mapRepository = new MapRepository();
$this->placeRepository = new PlaceRepository(); $this->placeRepository = new PlaceRepository();
} }
@ -45,18 +50,14 @@ class MapAdminController implements ISecured
if ($mapId) { if ($mapId) {
$map = $this->mapRepository->getById($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); $places = $this->getPlaces($mapId);
} else { } else {
$map = [ $map = new Map();
'name' => self::$unnamedMapName, $map->setName(self::$unnamedMapName);
'description' => ''
];
$bounds = Bounds::createDirectly(-90.0, -180.0, 90.0, 180.0);
$places = []; $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); return new HtmlContent('admin/map_editor', $data);
} }
@ -76,8 +77,13 @@ class MapAdminController implements ISecured
\Container::$dbConnection->startTransaction(); \Container::$dbConnection->startTransaction();
if (!$mapId) { if ($mapId) {
$mapId = $this->addNewMap(); $map = $this->mapRepository->getById($mapId);
} else {
$map = new Map();
$map->setName(self::$unnamedMapName);
$this->pdm->saveToDb($map);
$mapId = $map->getId();
} }
if (isset($_POST['added'])) { if (isset($_POST['added'])) {
@ -116,22 +122,17 @@ class MapAdminController implements ISecured
$mapBounds = $this->calculateMapBounds($mapId); $mapBounds = $this->calculateMapBounds($mapId);
$map = [ $map->setBounds($mapBounds);
'bound_south_lat' => $mapBounds->getSouthLat(), $map->setArea($mapBounds->calculateApproximateArea());
'bound_west_lng' => $mapBounds->getWestLng(),
'bound_north_lat' => $mapBounds->getNorthLat(),
'bound_east_lng' => $mapBounds->getEastLng(),
'area' => $mapBounds->calculateApproximateArea(),
];
if (isset($_POST['name'])) { if (isset($_POST['name'])) {
$map['name'] = $_POST['name'] ? $_POST['name'] : self::$unnamedMapName; $map->setName($_POST['name'] ? $_POST['name'] : self::$unnamedMapName);
} }
if (isset($_POST['description'])) { 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(); \Container::$dbConnection->commit();
@ -142,13 +143,13 @@ class MapAdminController implements ISecured
public function deleteMap() { public function deleteMap() {
$mapId = (int) $this->request->query('mapId'); $mapId = (int) $this->request->query('mapId');
$map = $this->mapRepository->getById($mapId);
\Container::$dbConnection->startTransaction(); \Container::$dbConnection->startTransaction();
$this->deletePlaces($mapId); $this->deletePlaces($mapId);
$modify = new Modify(\Container::$dbConnection, 'maps'); $this->pdm->deleteFromDb($map);
$modify->setId($mapId);
$modify->delete();
\Container::$dbConnection->commit(); \Container::$dbConnection->commit();
@ -187,30 +188,6 @@ class MapAdminController implements ISecured
return $bounds; 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 private function &getPlaces(int $mapId): array
{ {
$select = new Select(\Container::$dbConnection, 'places'); $select = new Select(\Container::$dbConnection, 'places');

View File

@ -19,6 +19,7 @@ class MapsController
public function getMaps(): IContent public function getMaps(): IContent
{ {
//TODO: from repository
$select = new Select(\Container::$dbConnection, 'maps'); $select = new Select(\Container::$dbConnection, 'maps');
$select->columns([ $select->columns([
['maps', 'id'], ['maps', 'id'],

View File

@ -1,16 +1,19 @@
<?php namespace MapGuesser\Repository; <?php namespace MapGuesser\Repository;
use MapGuesser\Database\Query\Select; use MapGuesser\PersistentData\Model\Map;
use MapGuesser\Interfaces\Database\IResultSet; use MapGuesser\PersistentData\PersistentDataManager;
class MapRepository class MapRepository
{ {
public function getById(int $mapId) private PersistentDataManager $pdm;
{
$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);
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);
} }
} }