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

17 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

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

2 

3from fastapi import APIRouter, Depends 

4 

5from kwai.api.dependencies import create_database 

6from kwai.api.schemas.news_item import NewsItemDocument 

7from kwai.core.db.database import Database 

8from kwai.core.json_api import Meta, PaginationModel 

9from kwai.modules.portal.get_news_items import GetNewsItems, GetNewsItemsCommand 

10from kwai.modules.portal.news.news_item_db_repository import NewsItemDbRepository 

11 

12 

13router = APIRouter() 

14 

15 

16@router.get("/news") 

17async def get_news( 

18 pagination: PaginationModel = Depends(PaginationModel), 

19 db: Database = Depends(create_database), 

20) -> NewsItemDocument: 

21 """Get news items for the portal. 

22 

23 Only promoted news items are returned for the portal. 

24 """ 

25 command = GetNewsItemsCommand( 

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

27 ) 

28 

29 count, news_item_iterator = await GetNewsItems(NewsItemDbRepository(db)).execute( 

30 command 

31 ) 

32 

33 result = NewsItemDocument( 

34 meta=Meta(count=count, offset=command.offset, limit=command.limit), data=[] 

35 ) 

36 

37 async for news_item in news_item_iterator: 

38 news_document = NewsItemDocument.create(news_item) 

39 result.merge(news_document) 

40 

41 return result