Skip to content

Commit

Permalink
Add error handling to incident search (#304)
Browse files Browse the repository at this point in the history
* Add error handling to incident search

* Flake8 Fix
  • Loading branch information
DMalone87 authored Aug 30, 2023
1 parent 7088d4c commit 8902098
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions backend/routes/incidents.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,41 @@ def search_incidents():
body: SearchIncidentsSchema = request.context.json
query = db.session.query(Incident)

if body.location:
# TODO: Replace with .match, which uses `@@ to_tsquery` for full-text
# search
#
# TODO: eventually replace with geosearch. Geocode records and integrate
# PostGIS
query = query.filter(Incident.location.ilike(f"%{body.location}%"))
if body.dateStart:
query = query.filter(
Incident.time_of_incident >=
datetime.strptime(body.dateStart, "%Y-%m-%d"))
if body.dateEnd:
query = query.filter(
Incident.time_of_incident <=
datetime.strptime(body.dateEnd, "%Y-%m-%d"))
if body.description:
query = query.filter(
Incident.description.ilike(f"%{body.description}%")
)
try:
if body.location:
# TODO: Replace with .match, which uses `@@ to_tsquery`
# for full-text search
#
# TODO: eventually replace with geosearch. Geocode
# records and integrate PostGIS
query = query.filter(Incident.location.ilike(f"%{body.location}%"))
if body.dateStart:
query = query.filter(
Incident.time_of_incident >=
datetime.strptime(body.dateStart, "%Y-%m-%d"))
if body.dateEnd:
query = query.filter(
Incident.time_of_incident <=
datetime.strptime(body.dateEnd, "%Y-%m-%d"))
if body.description:
query = query.filter(
Incident.description.ilike(f"%{body.description}%")
)
except Exception as e:
abort(422, description=str(e))

results = query.paginate(
page=body.page, per_page=body.perPage, max_per_page=100
)

return {
"results": [incident_orm_to_json(result) for result in results.items],
"page": results.page,
"totalPages": results.pages,
"totalResults": results.total,
}
try:
return {
"results": [
incident_orm_to_json(result) for result in results.items
],
"page": results.page,
"totalPages": results.pages,
"totalResults": results.total,
}
except Exception as e:
abort(500, description=str(e))

0 comments on commit 8902098

Please sign in to comment.