Merged in feature/MAPG-193-replace-direct-selects-to-repository-calls (pull request #173)

Feature/MAPG-193 replace direct selects to repository calls
This commit is contained in:
Bence Pőcze 2020-07-05 13:20:08 +00:00
commit 776561a41c
4 changed files with 28 additions and 45 deletions

View File

@ -1,10 +1,8 @@
<?php namespace MapGuesser\Controller;
use DateTime;
use MapGuesser\Database\Query\Select;
use MapGuesser\Interfaces\Authentication\IUser;
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;
@ -15,7 +13,6 @@ use MapGuesser\Repository\PlaceRepository;
use MapGuesser\Response\HtmlContent;
use MapGuesser\Response\JsonContent;
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\Util\Geo\Position;
use MapGuesser\Util\Panorama\Pov;
class MapAdminController implements ISecured
@ -183,34 +180,17 @@ class MapAdminController implements ISecured
private function deletePlaces(Map $map): void
{
//TODO: relations?
$select = new Select(\Container::$dbConnection, 'places');
$select->columns(Place::getFields());
$select->where('map_id', '=', $map->getId());
$result = $select->execute();
while ($placeData = $result->fetch(IResultSet::FETCH_ASSOC)) {
$place = new Place();
$this->pdm->fillWithData($placeData, $place);
foreach ($this->placeRepository->getAllForMap($map) as $place) {
$this->pdm->deleteFromDb($place);
}
}
private function calculateMapBounds(Map $map): Bounds
{
//TODO: from repository or relations
$select = new Select(\Container::$dbConnection, 'places');
$select->columns(['lat', 'lng']);
$select->where('map_id', '=', $map->getId());
$result = $select->execute();
$bounds = new Bounds();
while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) {
$bounds->extend(new Position($place['lat'], $place['lng']));
foreach ($this->placeRepository->getAllForMap($map) as $place) {
$bounds->extend($place->getPosition());
}
return $bounds;
@ -218,28 +198,19 @@ class MapAdminController implements ISecured
private function &getPlaces(Map $map): array
{
//TODO: from repository or relations
$select = new Select(\Container::$dbConnection, 'places');
$select->columns(['id', 'lat', 'lng', 'pano_id_cached', 'pano_id_cached_timestamp', 'pov_heading', 'pov_pitch', 'pov_zoom']);
$select->where('map_id', '=', $map->getId());
$result = $select->execute();
$places = [];
while ($place = $result->fetch(IResultSet::FETCH_ASSOC)) {
$noPano = $place['pano_id_cached_timestamp'] && $place['pano_id_cached'] === null;
foreach ($this->placeRepository->getAllForMap($map) as $place) {
$noPano = $place->getPanoIdCachedTimestampDate() !== null && $place->getPanoIdCached() === null;
$places[$place['id']] = [
'id' => $place['id'],
'lat' => $place['lat'],
'lng' => $place['lng'],
$placeId = $place->getId();
$places[$placeId] = [
'id' => $placeId,
'lat' => $place->getLat(),
'lng' => $place->getLng(),
'panoId' => null,
'pov' => [
'heading' => (float) $place['pov_heading'],
'pitch' => (float) $place['pov_pitch'],
'zoom' => (float) $place['pov_zoom']
],
'pov' => $place->getPov()->toArray(),
'noPano' => $noPano
];
}

View File

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

View File

@ -27,8 +27,9 @@ class PersistentDataManager
public function selectMultipleFromDb(Select $select, string $type, bool $withRelations = false): Generator
{
$select = $this->createSelect($select, $type, $withRelations);
$result = $select->execute();
while ($data = $select->execute()->fetch(IResultSet::FETCH_ASSOC)) {
while ($data = $result->fetch(IResultSet::FETCH_ASSOC)) {
$model = new $type();
$this->fillWithData($data, $model);

View File

@ -1,6 +1,8 @@
<?php namespace MapGuesser\Repository;
use Generator;
use MapGuesser\Database\Query\Select;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\PersistentDataManager;
@ -18,6 +20,15 @@ class PlaceRepository
return $this->pdm->selectFromDbById($placeId, Place::class);
}
public function getAllForMap(Map $map): Generator
{
$select = new Select(\Container::$dbConnection);
$select->where('map_id', '=', $map->getId());
yield from $this->pdm->selectMultipleFromDb($select, Place::class);
}
//TODO: use Map instead of id
public function getRandomForMapWithValidPano(int $mapId, array $exclude = [], array &$placesWithoutPano): Place
{
$placesWithoutPano = [];
@ -37,7 +48,7 @@ class PlaceRepository
private function selectRandomFromDbForMap(int $mapId, array $exclude): ?Place
{
$select = new Select(\Container::$dbConnection, 'places');
$select = new Select(\Container::$dbConnection);
$select->where('id', 'NOT IN', $exclude);
$select->where('map_id', '=', $mapId);