Skip to content

Commit

Permalink
add support for retrieving posts by note IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichan044 committed Aug 5, 2024
1 parent fd5c1e9 commit dd8fa2f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ def get_notes(
@router.get("/posts", response_model=PostListResponse)
def get_posts(
post_id: Union[List[PostId], None] = Query(default=None),
note_id: Union[List[NoteId], None] = Query(default=None),
created_at_start: Union[None, TwitterTimestamp, str] = Query(default=None),
created_at_end: Union[None, TwitterTimestamp, str] = Query(default=None),
) -> PostListResponse:
if post_id is not None:
return PostListResponse(data=list(storage.get_posts_by_ids(post_ids=post_id)))
if note_id is not None:
return PostListResponse(data=list(storage.get_posts_by_note_ids(note_ids=note_id)))
if created_at_start is not None:
if created_at_end is not None:
return PostListResponse(
Expand Down
10 changes: 10 additions & 0 deletions common/birdxplorer_common/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ def get_posts_by_created_at_end(self, end: TwitterTimestamp) -> Generator[PostMo
for post_record in sess.query(PostRecord).filter(PostRecord.created_at < end).all():
yield self._post_record_to_model(post_record)

def get_posts_by_note_ids(self, note_ids: List[NoteId]) -> Generator[PostModel, None, None]:
query = (
select(PostRecord)
.join(NoteRecord, NoteRecord.post_id == PostRecord.post_id)
.where(NoteRecord.note_id.in_(note_ids))
)
with Session(self.engine) as sess:
for post_record in sess.execute(query).scalars().all():
yield self._post_record_to_model(post_record)


def gen_storage(settings: GlobalSettings) -> Storage:
engine = create_engine(settings.storage_settings.sqlalchemy_database_url)
Expand Down

0 comments on commit dd8fa2f

Please sign in to comment.