Coverage for bc/kwai-bc-portal/src/kwai_bc_portal/get_authors.py: 100%

15 statements  

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

1"""Module that implements the 'get authors' use case.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai_core.domain.presenter import AsyncPresenter, IterableResult 

6 

7from kwai_bc_portal.domain.author import AuthorEntity 

8from kwai_bc_portal.repositories.author_repository import AuthorRepository 

9 

10 

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

12class GetAuthorsCommand: 

13 """Input for the use case.""" 

14 

15 limit: int = 0 

16 offset: int = 0 

17 

18 

19class GetAuthors: 

20 """Use case for getting authors.""" 

21 

22 def __init__( 

23 self, 

24 author_repo: AuthorRepository, 

25 presenter: AsyncPresenter[IterableResult[AuthorEntity]], 

26 ): 

27 """Initialize the use case. 

28 

29 Args: 

30 author_repo: The repository for getting the authors. 

31 presenter: The presenter for authors. 

32 """ 

33 self._author_repo = author_repo 

34 self._presenter = presenter 

35 

36 async def execute(self, command: GetAuthorsCommand): 

37 """Execute the use case. 

38 

39 Args: 

40 command: Input for the use case. 

41 """ 

42 query = self._author_repo.create_query() 

43 

44 await self._presenter.present( 

45 IterableResult( 

46 count=await query.count(), 

47 limit=command.limit, 

48 offset=command.offset, 

49 iterator=self._author_repo.get_all( 

50 query, limit=command.limit, offset=command.offset 

51 ), 

52 ) 

53 )