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

View File

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

View File

@ -27,8 +27,9 @@ class PersistentDataManager
public function selectMultipleFromDb(Select $select, string $type, bool $withRelations = false): Generator public function selectMultipleFromDb(Select $select, string $type, bool $withRelations = false): Generator
{ {
$select = $this->createSelect($select, $type, $withRelations); $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(); $model = new $type();
$this->fillWithData($data, $model); $this->fillWithData($data, $model);

View File

@ -1,6 +1,8 @@
<?php namespace MapGuesser\Repository; <?php namespace MapGuesser\Repository;
use Generator;
use MapGuesser\Database\Query\Select; use MapGuesser\Database\Query\Select;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\Model\Place; use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\PersistentDataManager; use MapGuesser\PersistentData\PersistentDataManager;
@ -18,6 +20,15 @@ class PlaceRepository
return $this->pdm->selectFromDbById($placeId, Place::class); 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 public function getRandomForMapWithValidPano(int $mapId, array $exclude = [], array &$placesWithoutPano): Place
{ {
$placesWithoutPano = []; $placesWithoutPano = [];
@ -37,7 +48,7 @@ class PlaceRepository
private function selectRandomFromDbForMap(int $mapId, array $exclude): ?Place 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('id', 'NOT IN', $exclude);
$select->where('map_id', '=', $mapId); $select->where('map_id', '=', $mapId);