Coverage for src/kwai/modules/portal/update_news_item.py: 90%

30 statements  

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

1"""Module for implementing the use case "Update News Item".""" 

2 

3from dataclasses import dataclass 

4 

5from kwai.core.domain.entity import Entity 

6from kwai.core.domain.value_objects.owner import Owner 

7from kwai.core.domain.value_objects.period import Period 

8from kwai.core.domain.value_objects.text import DocumentFormat, Locale, LocaleText 

9from kwai.core.domain.value_objects.timestamp import Timestamp 

10from kwai.modules.portal.applications.application import ApplicationIdentifier 

11from kwai.modules.portal.applications.application_repository import ( 

12 ApplicationRepository, 

13) 

14from kwai.modules.portal.news.news_item import ( 

15 NewsItemEntity, 

16 NewsItemIdentifier, 

17 Promotion, 

18) 

19from kwai.modules.portal.news.news_item_repository import NewsItemRepository 

20from kwai.modules.portal.news_item_command import NewsItemCommand 

21 

22 

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

24class UpdateNewsItemCommand(NewsItemCommand): 

25 """Input for the use case "Update News Item".""" 

26 

27 id: int 

28 

29 

30class UpdateNewsItem: 

31 """Use case for updating a news item.""" 

32 

33 def __init__( 

34 self, 

35 repo: NewsItemRepository, 

36 application_repo: ApplicationRepository, 

37 owner: Owner, 

38 ): 

39 """Initialize the use case. 

40 

41 Args: 

42 repo: A repository for updating news items. 

43 application_repo: A repository for getting the application. 

44 owner: The owner of the news item. 

45 """ 

46 self._repo = repo 

47 self._application_repo = application_repo 

48 self._owner = owner 

49 

50 async def execute(self, command: UpdateNewsItemCommand) -> NewsItemEntity: 

51 """Execute the use case. 

52 

53 Args: 

54 command: The input for this use case. 

55 

56 Raises: 

57 NewsItemNotFoundException: When the news item does not exist. 

58 ApplicationNotFoundException: When the application does not exist. 

59 """ 

60 news_item = await self._repo.get_by_id(NewsItemIdentifier(command.id)) 

61 application = await self._application_repo.get_by_id( 

62 ApplicationIdentifier(command.application) 

63 ) 

64 

65 if command.promotion > 0: 

66 if command.promotion_end_datetime is None: 

67 promotion = Promotion(priority=command.promotion) 

68 else: 

69 promotion = Promotion( 

70 priority=command.promotion, 

71 end_date=Timestamp.create_from_string( 

72 command.promotion_end_datetime 

73 ), 

74 ) 

75 else: 

76 promotion = None 

77 

78 news_item = Entity.replace( 

79 news_item, 

80 enabled=command.enabled, 

81 application=application, 

82 promotion=promotion, 

83 period=Period( 

84 start_date=Timestamp.create_from_string(command.publish_datetime), 

85 end_date=( 

86 None 

87 if command.end_datetime 

88 else Timestamp.create_from_string(command.end_datetime) 

89 ), 

90 ), 

91 texts=[ 

92 LocaleText( 

93 locale=Locale(text.locale), 

94 format=DocumentFormat(text.format), 

95 title=text.title, 

96 content=text.content, 

97 summary=text.summary, 

98 author=self._owner, 

99 ) 

100 for text in command.texts 

101 ], 

102 remark=command.remark, 

103 traceable_time=news_item.traceable_time.mark_for_update(), 

104 ) 

105 

106 await self._repo.update(news_item) 

107 

108 return news_item