Skip to content

Commit

Permalink
add search text for notes
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Nov 3, 2024
1 parent c4f0d07 commit 317e503
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def get_notes(
post_ids: Union[List[PostId], None] = Query(default=None, **V1DataNotesDocs.params["post_ids"]),
current_status: Union[None, List[str]] = Query(default=None, **V1DataNotesDocs.params["current_status"]),
language: Union[LanguageIdentifier, None] = Query(default=None, **V1DataNotesDocs.params["language"]),
search_text: Union[None, str] = Query(default=None, **V1DataPostsDocs.params["search_text"]),
) -> NoteListResponse:
if created_at_from is not None and isinstance(created_at_from, str):
created_at_from = ensure_twitter_timestamp(created_at_from)
Expand All @@ -223,6 +224,7 @@ def get_notes(
language=language,
offset=offset,
limit=limit,
search_text=search_text,
)
)
total_count = storage.get_number_of_notes(
Expand All @@ -233,6 +235,7 @@ def get_notes(
post_ids=post_ids,
current_status=current_status,
language=language,
search_text=search_text,
)

baseurl = str(request.url).split("?")[0]
Expand Down
10 changes: 9 additions & 1 deletion api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def _get_notes(
post_ids: Union[List[PostId], None] = None,
current_status: Union[None, List[str]] = None,
language: Union[LanguageIdentifier, None] = None,
search_text: Union[str, None] = None,
offset: Union[int, None] = None,
limit: Union[int, None] = None,
) -> Generator[Note, None, None]:
Expand All @@ -349,6 +350,8 @@ def _get_notes(
continue
if language is not None and note.language != language:
continue
if search_text is not None and search_text not in note.summary:
continue
yield note

mock.get_notes.side_effect = _get_notes
Expand All @@ -361,9 +364,14 @@ def _get_number_of_notes(
post_ids: Union[List[PostId], None] = None,
current_status: Union[None, List[str]] = None,
language: Union[LanguageIdentifier, None] = None,
search_text: Union[str, None] = None,
) -> int:
return len(
list(_get_notes(note_ids, created_at_from, created_at_to, topic_ids, post_ids, current_status, language))
list(
_get_notes(
note_ids, created_at_from, created_at_to, topic_ids, post_ids, current_status, language, search_text
)
)
)

mock.get_number_of_notes.side_effect = _get_number_of_notes
Expand Down
6 changes: 6 additions & 0 deletions common/birdxplorer_common/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ def get_notes(
post_ids: Union[List[PostId], None] = None,
current_status: Union[None, List[str]] = None,
language: Union[LanguageIdentifier, None] = None,
search_text: Union[str, None] = None,
offset: Union[int, None] = None,
limit: int = 100,
) -> Generator[NoteModel, None, None]:
Expand Down Expand Up @@ -365,6 +366,8 @@ def get_notes(
query = query.filter(NoteRecord.language == language)
if current_status is not None:
query = query.filter(NoteRecord.current_status.in_(current_status))
if search_text is not None:
query = query.filter(NoteRecord.summary.like(f"%{search_text}%"))
if offset is not None:
query = query.offset(offset)
query = query.limit(limit)
Expand Down Expand Up @@ -399,6 +402,7 @@ def get_number_of_notes(
post_ids: Union[List[PostId], None] = None,
current_status: Union[None, List[str]] = None,
language: Union[LanguageIdentifier, None] = None,
search_text: Union[str, None] = None,
) -> int:
with Session(self.engine) as sess:
query = sess.query(NoteRecord)
Expand All @@ -424,6 +428,8 @@ def get_number_of_notes(
query = query.filter(NoteRecord.language == language)
if current_status is not None:
query = query.filter(NoteRecord.current_status.in_(current_status))
if search_text is not None:
query = query.filter(NoteRecord.summary.like(f"%{search_text}%"))
return query.count()

def get_posts(
Expand Down

0 comments on commit 317e503

Please sign in to comment.