Coverage for src/kwai/modules/portal/get_applications.py: 85%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.10, 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.modules.portal.applications.application_repository import ( 

6 ApplicationRepository, 

7) 

8 

9 

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

11class GetApplicationsCommand: 

12 """Input for the use case [GetApplications][kwai.modules.portal.get_applications.GetApplications]. 

13 

14 Attributes: 

15 name: Only return the application with the given name 

16 news: Only return applications that can contain news 

17 events: Only return applications that can contain events 

18 pages: Only return applications that can contain pages 

19 """ 

20 

21 name: str = "" 

22 news: bool = False 

23 events: bool = False 

24 pages: bool = False 

25 

26 

27class GetApplications: 

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

29 

30 def __init__(self, application_repo: ApplicationRepository): 

31 """Initialize the use case. 

32 

33 Args: 

34 application_repo: A repository for getting applications. 

35 """ 

36 self._application_repo = application_repo 

37 

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

39 """Execute the use case. 

40 

41 Args: 

42 command: The input for this use case. 

43 """ 

44 query = self._application_repo.create_query() 

45 if command.events: 

46 query.filter_only_events() 

47 if command.news: 

48 query.filter_only_news() 

49 if command.pages: 

50 query.filter_only_pages() 

51 

52 return await query.count(), self._application_repo.get_all(query)