Coverage for kwai/api/v1/portal/endpoints/news.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

1"""Module that implements news endpoints.""" 

2 

3from fastapi import APIRouter, Depends 

4 

5from kwai.api.dependencies import deps 

6from kwai.api.v1.portal.schemas.story import PortalStoryResource 

7from kwai.core.db.database import Database 

8from kwai.core.json_api import Meta, PaginationModel 

9from kwai.modules.news.get_stories import GetStories, GetStoriesCommand 

10from kwai.modules.news.stories.story_db_repository import StoryDbRepository 

11 

12router = APIRouter() 

13 

14 

15@router.get("/news") 

16async def get_news( 

17 pagination: PaginationModel = Depends(PaginationModel), db=deps.depends(Database) 

18) -> PortalStoryResource.get_document_model(): 

19 """Get news stories for the portal. 

20 

21 Only promoted news stories are returned for the portal. 

22 """ 

23 command = GetStoriesCommand( 

24 offset=pagination.offset or 0, limit=pagination.limit or 10, promoted=True 

25 ) 

26 count, story_iterator = await GetStories(StoryDbRepository(db)).execute(command) 

27 

28 document = PortalStoryResource.serialize_list( 

29 [PortalStoryResource(story) async for story in story_iterator] 

30 ) 

31 document.meta = Meta(count=count, offset=command.offset, limit=command.limit) 

32 

33 return document