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

16 statements  

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

1"""Module that defines the use case: update an application.""" 

2 

3from dataclasses import dataclass, replace 

4 

5from kwai_core.domain.presenter import Presenter 

6from kwai_core.domain.value_objects.identifier import IntIdentifier 

7 

8from kwai_bc_portal.applications.application_repository import ( 

9 ApplicationRepository, 

10) 

11from kwai_bc_portal.domain.application import ApplicationEntity 

12 

13 

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

15class UpdateApplicationCommand: 

16 """Input for the use case [UpdateApplication][kwai_bc_portal.update_application.UpdateApplication]. 

17 

18 Attributes: 

19 id: The id of the application 

20 """ 

21 

22 id: int 

23 title: str 

24 short_description: str 

25 description: str 

26 remark: str 

27 weight: int 

28 events: bool 

29 pages: bool 

30 news: bool 

31 

32 

33class UpdateApplication: 

34 """Implements the use case 'update an application'.""" 

35 

36 def __init__( 

37 self, 

38 application_repo: ApplicationRepository, 

39 presenter: Presenter[ApplicationEntity], 

40 ): 

41 """Initialize the use case. 

42 

43 Args: 

44 application_repo: A repository for updating an application. 

45 presenter: A presenter for an application. 

46 """ 

47 self._application_repo = application_repo 

48 self._presenter = presenter 

49 

50 async def execute(self, command: UpdateApplicationCommand): 

51 """Execute the use case. 

52 

53 Args: 

54 command: The input for this use case. 

55 """ 

56 application = await self._application_repo.get_by_id(IntIdentifier(command.id)) 

57 updated_application = replace( 

58 application, 

59 title=command.title, 

60 short_description=command.short_description, 

61 description=command.description, 

62 remark=command.remark, 

63 events=command.events, 

64 pages=command.pages, 

65 news=command.news, 

66 weight=command.weight, 

67 ) 

68 await self._application_repo.update(updated_application) 

69 self._presenter.present(updated_application)