Skip to content

Commit

Permalink
feat: search stations api for destination (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsarrco authored Dec 16, 2023
2 parents 9e40ff3 + 54ed98a commit 14af96f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions server/base/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def __init__(self, name, emoji, session, typesense):
self.session = session
self.typesense = typesense

def search_stops(self, name=None, lat=None, lon=None, page=1, limit=4, all_sources=False) -> tuple[
list[Station], int]:
def search_stops(self, name=None, lat=None, lon=None, page=1, limit=4, all_sources=False,
hide_ids: list[str] = None) -> tuple[list[Station], int]:
search_config = {'per_page': limit, 'query_by': 'name', 'page': page}

limit_hits = None
Expand All @@ -78,6 +78,8 @@ def search_stops(self, name=None, lat=None, lon=None, page=1, limit=4, all_sourc
})
if not all_sources:
search_config['filter_by'] = f'source:{self.name}'
if hide_ids:
search_config['hidden_hits'] = ','.join(hide_ids)

results = self.typesense.collections['stations'].documents.search(search_config)

Expand Down
12 changes: 11 additions & 1 deletion server/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ async def home(request: Request) -> Response:
async def search_stations(request: Request) -> Response:
query = request.query_params.get('q', '')
limit = int(request.query_params.get('limit', 4))
hide_ids = request.query_params.get('hide_ids')
if hide_ids:
hide_ids = hide_ids.split(',')
only_source = request.query_params.get('only_source')
if only_source:
source = sources[only_source]
all_sources = False
else:
source = sources['aut']
all_sources = True
limit = max(1, min(limit, 10))
stations, count = sources['aut'].search_stops(name=query, all_sources=True, limit=limit)
stations, count = source.search_stops(name=query, all_sources=all_sources, limit=limit, hide_ids=hide_ids)
return JSONResponse([station.as_dict() for station in stations])


Expand Down

0 comments on commit 14af96f

Please sign in to comment.