From a3a382dae992eb01171bef7c607df043acbe67d2 Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 13 Mar 2023 14:41:25 -0400 Subject: [PATCH] Reintroduce function in business logic --- hub/exports/db_factory.py | 28 +++++++++++++++++---- hub/persistence/repositories/city_object.py | 2 +- hub/persistence/repositories/user.py | 16 ++++++------ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/hub/exports/db_factory.py b/hub/exports/db_factory.py index d8981af7..5f4fde9e 100644 --- a/hub/exports/db_factory.py +++ b/hub/exports/db_factory.py @@ -5,6 +5,7 @@ Copyright © 2022 Concordia CERC group Project CoderPeter Yefi peteryefi@gmail.com """ import json +from typing import Union, Dict from hub.persistence import City from hub.persistence import Application @@ -25,10 +26,11 @@ class DBFactory: self._city_object = CityObject(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) self._simulation_results = SimulationResults(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env) - def application_info(self, application_uuid): + def application_info(self, application_uuid) -> Union[Application, None]: """ Retrieve the application info for the given uuid :param application_uuid: the uuid for the application + :return: Application or None """ return self._application.get_by_uuid(application_uuid) @@ -38,6 +40,7 @@ class DBFactory: :param name: the user name :param password: the user password :param application_id: the application id + :return: User or None """ return self._user.get_by_name_application_id_and_password(name, password, application_id) @@ -47,13 +50,29 @@ class DBFactory: :param name: the user name :param password: the user password :param application_uuid: the application uuid + :return: User or None """ return self._user.get_by_name_application_uuid_and_password(name, password, application_uuid) def cities_by_user_and_application(self, user_id, application_id) -> [City]: + """ + Retrieve the cities belonging to the user and the application + :param user_id: User id + :param application_id: Application id + :return: [City] + """ return self._city.get_by_user_id_and_application_id(user_id, application_id) - def results(self, user_id, application_id, cities, result_names=[]): + def building_info(self, name, city_id) -> Union[CityObject, None]: + """ + Retrieve the building info + :param name: Building name + :param city_id: City Id + :return: CityObject or None + """ + return self._city_object.get_by_name_and_city(name, city_id) + + def results(self, user_id, application_id, cities, result_names=None) -> Dict: """ Retrieve the simulation results for the given cities :param user_id: the user id owning the results @@ -61,6 +80,8 @@ class DBFactory: :param cities: dictionary containing the city and building names for the results :param result_names: if given, filter the results to the selected names """ + if result_names is None: + result_names = [] results = {} for city in cities['cities']: city_name = next(iter(city)) @@ -83,6 +104,3 @@ class DBFactory: values["building"] = building_name results[city_name].append(values) return results - - - diff --git a/hub/persistence/repositories/city_object.py b/hub/persistence/repositories/city_object.py index ded9a1ad..7ca4d8e6 100644 --- a/hub/persistence/repositories/city_object.py +++ b/hub/persistence/repositories/city_object.py @@ -105,7 +105,7 @@ class CityObject(Repository): except SQLAlchemyError as err: logger.error(f'Error while deleting application: {err}') - def get_by_name_and_city(self, name, city_id) -> [Model]: + def get_by_name_and_city(self, name, city_id) -> Union[Model, None]: """ Fetch a city object based on name and city id :param name: city object name diff --git a/hub/persistence/repositories/user.py b/hub/persistence/repositories/user.py index 87005a4a..791a4c02 100644 --- a/hub/persistence/repositories/user.py +++ b/hub/persistence/repositories/user.py @@ -60,7 +60,7 @@ class User(Repository): :param name: the name of the user :param password: the password of the user :param role: the role of the user - :return: + :return: None, Dictionary """ try: self.session.query(Model).filter(Model.id == user_id).update({ @@ -86,7 +86,7 @@ class User(Repository): except SQLAlchemyError as err: logger.error(f'Error while fetching user: {err}') - def get_by_name_and_application(self, name: str, application_id: int) -> [Model]: + def get_by_name_and_application(self, name: str, application_id: int) -> Model: """ Fetch user based on the email address :param name: User name @@ -96,18 +96,17 @@ class User(Repository): try: return self.session.execute( select(Model).where(Model.name == name, Model.application_id == application_id) - ).first() + ).first()[0] except SQLAlchemyError as err: logger.error(f'Error while fetching user by name and application: {err}') - def get_by_name_application_id_and_password(self, name: str, password: str, application_id: int) -> [Model]: + def get_by_name_application_id_and_password(self, name: str, password: str, application_id: int) -> Union[Model, None]: """ Fetch user based on the email and password :param name: User name :param password: User password :param application_id: User password - - :return: [User] + :return: User """ try: user = self.session.execute( @@ -119,14 +118,13 @@ class User(Repository): except SQLAlchemyError as err: logger.error(f'Error while fetching user by email: {err}') - def get_by_name_application_uuid_and_password(self, name: str, password: str, application_uuid: str) -> [Model]: + def get_by_name_application_uuid_and_password(self, name: str, password: str, application_uuid: str) -> Union[Model, None]: """ Fetch user based on the email and password :param name: User name :param password: User password :param application_uuid: Application uuid - - :return: [User] + :return: User """ try: application = self.session.execute(