Coverage for src/kwai/modules/portal/get_pages.py: 95%

19 statements  

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

1"""Module for the use case "Get Pages".""" 

2 

3from dataclasses import dataclass 

4 

5from kwai.core.domain.use_case import UseCaseBrowseResult 

6from kwai.modules.portal.pages.page_repository import PageRepository 

7 

8 

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

10class GetPagesCommand: 

11 """Input for the "Get Pages" use case.""" 

12 

13 offset: int | None = None 

14 limit: int | None = None 

15 application: int | str | None = None 

16 enabled: bool = True 

17 

18 

19class GetPages: 

20 """Use case "Get Pages".""" 

21 

22 def __init__(self, repo: PageRepository): 

23 """Initialize the use case. 

24 

25 Args: 

26 repo: A repository to get pages. 

27 """ 

28 self._repo = repo 

29 

30 async def execute(self, command: GetPagesCommand) -> UseCaseBrowseResult: 

31 """Executes the use case.""" 

32 query = self._repo.create_query() 

33 

34 if command.enabled: 

35 query.filter_by_active() 

36 

37 if command.application is not None: 

38 query.filter_by_application(command.application) 

39 

40 return UseCaseBrowseResult( 

41 await query.count(), 

42 self._repo.get_all(query, limit=command.limit, offset=command.offset), 

43 )