Coverage for kwai/modules/news/get_stories.py: 90%

31 statements  

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

1"""Implement the use case: get news stories.""" 

2from dataclasses import dataclass 

3 

4from kwai.core.domain.use_case import UseCaseBrowseResult 

5from kwai.core.domain.value_objects.unique_id import UniqueId 

6from kwai.modules.news.stories.story_repository import StoryRepository 

7 

8 

9@dataclass(kw_only=True, frozen=True, slots=True) 

10class GetStoriesCommand: 

11 """Input for use case: [GetStories][kwai.modules.news.get_stories.GetStories]. 

12 

13 Attributes: 

14 offset: Offset to use. Default is None. 

15 limit: The max. number of elements to return. Default is None, which means all. 

16 enabled: When False, also stories that are not activated will be returned. 

17 """ 

18 

19 offset: int | None = None 

20 limit: int | None = None 

21 enabled: bool = True 

22 publish_year: int = 0 

23 publish_month: int = 0 

24 application: int | str | None = None 

25 promoted: bool = False 

26 author_uuid: str | None = None 

27 

28 

29class GetStories: 

30 """Implementation of the use case. 

31 

32 Use this use case for getting news stories. 

33 """ 

34 

35 def __init__(self, repo: StoryRepository): 

36 """Initialize the use case. 

37 

38 Args: 

39 repo: A repository for getting the news stories. 

40 """ 

41 self._repo = repo 

42 

43 async def execute(self, command: GetStoriesCommand) -> UseCaseBrowseResult: 

44 """Execute the use case. 

45 

46 Args: 

47 command: The input for this use case. 

48 

49 Returns: 

50 A tuple with the number of entities and an iterator for story entities. 

51 """ 

52 query = self._repo.create_query() 

53 

54 if command.enabled: 

55 query.filter_by_active() 

56 

57 if command.publish_year > 0: 

58 query.filter_by_publication_date( 

59 command.publish_year, command.publish_month 

60 ) 

61 

62 if command.promoted: 

63 query.filter_by_promoted() 

64 

65 if command.application is not None: 

66 query.filter_by_application(command.application) 

67 

68 if command.author_uuid is not None: 

69 query.filter_by_user(UniqueId.create_from_string(command.author_uuid)) 

70 

71 query.order_by_publication_date() 

72 

73 return UseCaseBrowseResult( 

74 count=await query.count(), 

75 iterator=self._repo.get_all( 

76 query=query, offset=command.offset, limit=command.limit 

77 ), 

78 )