2022-11-15 20:48:42 -05:00
|
|
|
"""
|
|
|
|
DBFactory performs database create, delete and update operations
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project CoderPeter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.city_model_structure.city import City
|
2023-02-02 06:12:24 -05:00
|
|
|
from hub.persistence import City as CityRepository
|
2023-02-02 13:00:58 -05:00
|
|
|
from hub.persistence import SimulationResults
|
2022-11-15 20:48:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DBFactory:
|
|
|
|
"""
|
|
|
|
DBFactory class
|
|
|
|
"""
|
|
|
|
|
2023-01-17 20:04:56 -05:00
|
|
|
def __init__(self, db_name, dotenv_path, app_env):
|
2023-02-02 06:12:24 -05:00
|
|
|
self._city_repository = CityRepository(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
|
2023-02-02 13:00:58 -05:00
|
|
|
self._simulation_results = SimulationResults(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
|
2022-11-15 20:48:42 -05:00
|
|
|
|
2023-02-13 05:17:25 -05:00
|
|
|
def persist_city(self, city: City, pickle_path, application_id: int, user_id: int):
|
2022-11-15 20:48:42 -05:00
|
|
|
"""
|
|
|
|
Persist city into postgres database
|
2023-02-02 13:00:58 -05:00
|
|
|
:param city: City to be stored
|
2023-02-13 05:17:25 -05:00
|
|
|
:param pickle_path: Path to save the pickle file
|
2023-02-02 13:00:58 -05:00
|
|
|
:param application_id: Application id owning this city
|
|
|
|
:param user_id: User who create the city
|
2022-11-15 20:48:42 -05:00
|
|
|
"""
|
2023-02-13 05:17:25 -05:00
|
|
|
return self._city_repository.insert(city, pickle_path, application_id, user_id)
|
2022-11-15 20:48:42 -05:00
|
|
|
|
|
|
|
def update_city(self, city_id, city):
|
|
|
|
"""
|
|
|
|
Update an existing city in postgres database
|
|
|
|
:param city_id: the id of the city to update
|
|
|
|
:param city: the updated city object
|
|
|
|
"""
|
2023-02-02 06:12:24 -05:00
|
|
|
return self._city_repository.update(city_id, city)
|
2022-11-15 20:48:42 -05:00
|
|
|
|
|
|
|
def delete_city(self, city_id):
|
|
|
|
"""
|
|
|
|
Deletes a single city from postgres
|
|
|
|
:param city_id: the id of the city to get
|
|
|
|
"""
|
2023-02-02 06:12:24 -05:00
|
|
|
self._city_repository.delete(city_id)
|
2023-02-02 13:00:58 -05:00
|
|
|
|
|
|
|
def add_simulation_results(self, name, values, city_id=None, city_object_id=None):
|
|
|
|
"""
|
|
|
|
Add simulation results to the city or to the city_object
|
|
|
|
:param name: simulation and simulation engine name
|
|
|
|
:param values: simulation values in json format
|
|
|
|
:param city_id: city id or None
|
|
|
|
:param city_object_id: city object id or None
|
|
|
|
"""
|
|
|
|
self._simulation_results.insert(name, values,city_id, city_object_id)
|