diff --git a/src/Controller/MapAdminController.php b/src/Controller/MapAdminController.php index 7f64d0f..6a46f28 100644 --- a/src/Controller/MapAdminController.php +++ b/src/Controller/MapAdminController.php @@ -1,10 +1,8 @@ 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 ]; } diff --git a/src/Controller/MapsController.php b/src/Controller/MapsController.php index 3c90d56..4385e76 100644 --- a/src/Controller/MapsController.php +++ b/src/Controller/MapsController.php @@ -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'], diff --git a/src/Repository/PlaceRepository.php b/src/Repository/PlaceRepository.php index 16f2afe..50f7366 100644 --- a/src/Repository/PlaceRepository.php +++ b/src/Repository/PlaceRepository.php @@ -1,6 +1,8 @@ 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);