diff --git a/app/controllers/bgroup.py b/app/controllers/bgroup.py index e9d112ad49f374056b158fd5a3e200d3afa9b8fc..ec911abd1a13961e5e7fb67e279aa56795608924 100644 --- a/app/controllers/bgroup.py +++ b/app/controllers/bgroup.py @@ -78,5 +78,23 @@ class BuildingBGroupController(RestController): GROUP BY building_bgroup.building_id, id '''.format(bgroup_id) - results = self.db.session.execute(query) + # Query for buildings that are not in the materialized view aka Baltimore right now + alt_query = ''' + SELECT + bg.id as id, + bg.building_id, + string_agg(concat(a.house_number, ' ', a.street_name) || ' ' || zipcode, ', ') AS address_list + FROM groups.building_bgroup bg + JOIN public.building b on bg.building_id = b.id + join public.building_address ba on b.id = ba.building_id + join public.address a on ba.address_id = a.id + WHERE bgroup_id = {} + GROUP BY bg.building_id, bg.id; + '''.format(bgroup_id) + + results = self.db.session.execute(query).fetchall() + + # Use alternate query for Baltimore group + if len(results) == 0: + results = self.db.session.execute(alt_query) return [dict(row) for row in results] diff --git a/app/controllers/building.py b/app/controllers/building.py index 84a197c48121997d70de000fe02877540785b140..ed63c05bf48886ca009262a732d38569cbf5c2fc 100644 --- a/app/controllers/building.py +++ b/app/controllers/building.py @@ -46,7 +46,6 @@ class BuildingController(RestController): Returns: dict: Building object """ - try: building_list = proc(self.Model, self.Model.PROCS['READ'], **{'building_id': id_}) except Exception as err: @@ -56,7 +55,16 @@ class BuildingController(RestController): ) if not building_list: - raise NotFound + # Hack to try using get by id for Baltimore by using new stored procedure aka postres function + try: + building_list = proc(self.Model, self.Model.PROCS['BALT'], **{'building_id': id_}) + except Exception as err: + raise ( + BadRequest(str(err)) if current_app.config['DEBUG'] else + BadRequest('Error while executing db call') + ) + if not building_list: + raise NotFound address_list = [] for building in building_list: address_list.append(building.street_address) diff --git a/app/models/building.py b/app/models/building.py index d95071aa818c406d0fafcdc93350aae1f8bbec24..1a72e366c065a14230fe513d4b3b60367fdf8e60 100644 --- a/app/models/building.py +++ b/app/models/building.py @@ -8,6 +8,7 @@ class Building(BaseModel): PROCS = { 'READ': 'building_search', + 'BALT': 'baltimore_building_get', } __table__ = ProcTable(