Coverage for bc/kwai-bc-portal/src/kwai_bc_portal/create_news_item.py: 88%
26 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module for defining the use case "Create News Item"."""
3from kwai_core.domain.presenter import Presenter
4from kwai_core.domain.value_objects.owner import Owner
5from kwai_core.domain.value_objects.period import Period
6from kwai_core.domain.value_objects.text import DocumentFormat, Locale, LocaleText
7from kwai_core.domain.value_objects.timestamp import Timestamp
9from kwai_bc_portal.applications.application_repository import (
10 ApplicationRepository,
11)
12from kwai_bc_portal.domain.application import ApplicationIdentifier
13from kwai_bc_portal.domain.news_item import NewsItemEntity, Promotion
14from kwai_bc_portal.news.news_item_repository import NewsItemRepository
15from kwai_bc_portal.news_item_command import NewsItemCommand
18CreateNewsItemCommand = NewsItemCommand
21class CreateNewsItem:
22 """Use case "Create News Item"."""
24 def __init__(
25 self,
26 repo: NewsItemRepository,
27 application_repo: ApplicationRepository,
28 owner: Owner,
29 presenter: Presenter[NewsItemEntity],
30 ):
31 """Initialize the use case.
33 Args:
34 repo: The repository to create the news item.
35 application_repo: The repository to get the application entity.
36 owner: The owner of the news item.
37 presenter: A presenter for a news item.
38 """
39 self._repo = repo
40 self._application_repo = application_repo
41 self._owner = owner
42 self._presenter = presenter
44 async def execute(self, command: CreateNewsItemCommand):
45 """Execute the use case.
47 Args:
48 command: The input for this use case.
50 Raises:
51 ApplicationNotFoundException: raised when the application does not exist.
52 """
53 application = await self._application_repo.get_by_id(
54 ApplicationIdentifier(command.application)
55 )
56 if command.promotion > 0:
57 if command.promotion_end_datetime is None:
58 promotion = Promotion(priority=command.promotion)
59 else:
60 promotion = Promotion(
61 priority=command.promotion,
62 end_date=Timestamp.create_from_string(
63 command.promotion_end_datetime
64 ),
65 )
66 else:
67 promotion = Promotion()
69 news_item = NewsItemEntity(
70 enabled=command.enabled,
71 promotion=promotion,
72 period=Period(
73 start_date=Timestamp.create_from_string(command.publish_datetime),
74 end_date=Timestamp.create_from_string(command.end_datetime),
75 ),
76 application=application,
77 texts=tuple(
78 [
79 LocaleText(
80 locale=Locale(text.locale),
81 format=DocumentFormat(text.format),
82 title=text.title,
83 content=text.content,
84 summary=text.summary,
85 author=self._owner,
86 )
87 for text in command.texts
88 ]
89 ),
90 remark=command.remark,
91 )
93 self._presenter.present(await self._repo.create(news_item))