2022-11-03 15:29:09 -04:00
|
|
|
"""
|
|
|
|
Building repository with database CRUD operations
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Peter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
|
|
|
|
2022-11-07 21:48:24 -05:00
|
|
|
from city_model_structure.building import Building as CityBuilding
|
2022-11-08 21:03:48 -05:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2022-11-07 21:48:24 -05:00
|
|
|
from persistence.models import Building
|
2022-11-03 15:29:09 -04:00
|
|
|
from persistence import BaseRepo
|
2022-11-07 21:48:24 -05:00
|
|
|
from helpers.city_util import CityUtil
|
2022-11-08 21:03:48 -05:00
|
|
|
from sqlalchemy import select
|
2022-11-03 15:29:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
class BuildingRepo(BaseRepo):
|
|
|
|
|
2022-11-11 16:16:31 -05:00
|
|
|
def __init__(self, db_name):
|
|
|
|
super().__init__(db_name)
|
2022-11-07 21:48:24 -05:00
|
|
|
self._city_util = CityUtil()
|
2022-11-03 15:29:09 -04:00
|
|
|
|
2022-11-07 21:48:24 -05:00
|
|
|
def insert(self, building: CityBuilding, city_id: int) -> Building:
|
|
|
|
"""
|
|
|
|
Inserts a new building into the database
|
|
|
|
:param building: the building to insert
|
|
|
|
:param city_id: the city the building belongs to
|
|
|
|
:return:
|
|
|
|
"""
|
2022-11-08 21:03:48 -05:00
|
|
|
|
2022-11-07 21:48:24 -05:00
|
|
|
model_building = Building()
|
|
|
|
model_building.name = building.name
|
|
|
|
model_building.function = building.function
|
|
|
|
model_building.construction_year = building.year_of_construction
|
|
|
|
model_building.floor_area = building.floor_area
|
|
|
|
model_building.city_id = city_id
|
|
|
|
model_building.data = self._city_util.extract_building_data(building)
|
2022-11-08 21:03:48 -05:00
|
|
|
try:
|
|
|
|
self.session.add(model_building)
|
|
|
|
self.session.flush()
|
|
|
|
self.session.commit()
|
|
|
|
return model_building
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
print(f'Error while adding building: {err}')
|
2022-11-03 15:29:09 -04:00
|
|
|
|
2022-11-08 21:03:48 -05:00
|
|
|
def get_by_id(self, building_id: int) -> Building:
|
|
|
|
"""
|
|
|
|
Fetch a building based on the id
|
|
|
|
:param building_id: the building id
|
|
|
|
:return: a Building
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.session.execute(select(Building).where(Building.id == building_id)).first()[0]
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
print(f'Error while fetching building: {err}')
|
|
|
|
|
|
|
|
def get_by_name(self, building_name: str) -> [Building]:
|
|
|
|
"""
|
|
|
|
Fetch a building based on the name
|
|
|
|
:param building_name: the name of the building
|
|
|
|
:return: [Building] with the provided name
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
result_set = self.session.execute(select(Building).where(Building.name == building_name))
|
|
|
|
return [building[0] for building in result_set]
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
print(f'Error while fetching buildings: {err}')
|
|
|
|
|
|
|
|
def get_by_construction_year(self, year: int):
|
|
|
|
"""
|
|
|
|
Fetch a building based on the year of construction
|
|
|
|
:param year: the construction year of the building
|
|
|
|
:return: [Building]
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
result_set = self.session.execute(select(Building).where(Building.construction_year == year))
|
|
|
|
return [building[0] for building in result_set]
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
print(f'Error while fetching buildings: {err}')
|
2022-11-07 21:48:24 -05:00
|
|
|
|
|
|
|
def get_by_city(self, city_id: int):
|
2022-11-08 21:03:48 -05:00
|
|
|
"""
|
|
|
|
Get all the buildings in a city
|
|
|
|
:param city_id: the city id
|
|
|
|
:return: [Building]
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
result_set = self.session.execute(select(Building).where(Building.city_id == city_id))
|
|
|
|
return [building[0] for building in result_set]
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
print(f'Error while fetching buildings: {err}')
|
|
|
|
|
|
|
|
|