Coverage for bc/kwai-bc-portal/src/kwai_bc_portal/get_applications.py: 87%

23 statements  

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

1"""Module that defines the use case: get all applications for a portal.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai_core.domain.presenter import AsyncPresenter, IterableResult 

6 

7from kwai_bc_portal.applications.application_repository import ( 

8 ApplicationRepository, 

9) 

10from kwai_bc_portal.domain.application import ApplicationEntity 

11 

12 

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

14class GetApplicationsCommand: 

15 """Input for the use case [GetApplications][kwai_bc_portal.get_applications.GetApplications]. 

16 

17 Attributes: 

18 name: Only return the application with the given name 

19 news: Only return applications that can contain news 

20 events: Only return applications that can contain events 

21 pages: Only return applications that can contain pages 

22 """ 

23 

24 name: str = "" 

25 news: bool = False 

26 events: bool = False 

27 pages: bool = False 

28 

29 

30class GetApplications: 

31 """Implements the use case 'get applications'.""" 

32 

33 def __init__( 

34 self, 

35 application_repo: ApplicationRepository, 

36 presenter: AsyncPresenter[IterableResult[ApplicationEntity]], 

37 ): 

38 """Initialize the use case. 

39 

40 Args: 

41 application_repo: A repository for getting applications. 

42 presenter: A presenter for an application entity. 

43 """ 

44 self._application_repo = application_repo 

45 self._presenter = presenter 

46 

47 async def execute(self, command: GetApplicationsCommand): 

48 """Execute the use case. 

49 

50 Args: 

51 command: The input for this use case. 

52 """ 

53 query = self._application_repo.create_query() 

54 if command.events: 

55 query.filter_only_events() 

56 if command.news: 

57 query.filter_only_news() 

58 if command.pages: 

59 query.filter_only_pages() 

60 

61 await self._presenter.present( 

62 IterableResult( 

63 count=await query.count(), 

64 offset=0, 

65 limit=0, 

66 iterator=self._application_repo.get_all(query), 

67 ) 

68 )