From 317e5030d0ba3161814e6065f4bfd8c0944982fe Mon Sep 17 00:00:00 2001 From: yu23ki14 Date: Sun, 3 Nov 2024 15:58:05 +0900 Subject: [PATCH] add search text for notes --- api/birdxplorer_api/routers/data.py | 3 +++ api/tests/conftest.py | 10 +++++++++- common/birdxplorer_common/storage.py | 6 ++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/api/birdxplorer_api/routers/data.py b/api/birdxplorer_api/routers/data.py index 8dbde93..371df33 100644 --- a/api/birdxplorer_api/routers/data.py +++ b/api/birdxplorer_api/routers/data.py @@ -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) @@ -223,6 +224,7 @@ def get_notes( language=language, offset=offset, limit=limit, + search_text=search_text, ) ) total_count = storage.get_number_of_notes( @@ -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] diff --git a/api/tests/conftest.py b/api/tests/conftest.py index cdb8301..36a2df7 100644 --- a/api/tests/conftest.py +++ b/api/tests/conftest.py @@ -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]: @@ -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 @@ -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 diff --git a/common/birdxplorer_common/storage.py b/common/birdxplorer_common/storage.py index 7aaf51e..8f575df 100644 --- a/common/birdxplorer_common/storage.py +++ b/common/birdxplorer_common/storage.py @@ -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]: @@ -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) @@ -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) @@ -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(