system_assignation/persistence/repositories/BuildingRepo.py

46 lines
1.4 KiB
Python
Raw Normal View History

"""
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
"""
import json
from city_model_structure.building import Building as CityBuilding
from persistence.models import City
from persistence.models import Building
from persistence import BaseRepo
from helpers.city_util import CityUtil
class BuildingRepo(BaseRepo):
def __init__(self):
super().__init__()
self._city_util = CityUtil()
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:
"""
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)
self.session.add(model_building)
self.session.flush()
self.session.commit()
return model_building
def get_by_id(self, building_id: int):
return self.session.query(Building).filter(Building.id == building_id)
def get_by_city(self, city_id: int):
return self.session.query(Building).filter(Building.city_id == city_id)