From 320d9b3a121fc640f46dc6f4fc9be8a5b432ef90 Mon Sep 17 00:00:00 2001 From: fbraem Date: Mon, 2 Oct 2023 22:05:09 +0200 Subject: [PATCH] feat: add use case Create News Item --- .../kwai/modules/portal/create_news_item.py | 83 +++++++++++++++++++ .../kwai/modules/portal/news_item_command.py | 18 ++++ .../modules/portal/test_create_news_item.py | 40 +++++++++ 3 files changed, 141 insertions(+) create mode 100644 backend/src/kwai/modules/portal/create_news_item.py create mode 100644 backend/src/kwai/modules/portal/news_item_command.py create mode 100644 backend/src/tests/modules/portal/test_create_news_item.py diff --git a/backend/src/kwai/modules/portal/create_news_item.py b/backend/src/kwai/modules/portal/create_news_item.py new file mode 100644 index 000000000..2e424b5ae --- /dev/null +++ b/backend/src/kwai/modules/portal/create_news_item.py @@ -0,0 +1,83 @@ +"""Module for defining the use case "Create News Item".""" +from kwai.core.domain.value_objects.local_timestamp import LocalTimestamp +from kwai.core.domain.value_objects.owner import Owner +from kwai.core.domain.value_objects.period import Period +from kwai.core.domain.value_objects.text import DocumentFormat, Locale, LocaleText +from kwai.modules.portal.applications.application import ApplicationIdentifier +from kwai.modules.portal.applications.application_repository import ( + ApplicationRepository, +) +from kwai.modules.portal.news.news_item import NewsItemEntity, Promotion +from kwai.modules.portal.news.news_item_repository import NewsItemRepository +from kwai.modules.portal.news_item_command import NewsItemCommand + +CreateNewsItemCommand = NewsItemCommand + + +class CreateNewsItem: + """Use case "Create News Item".""" + + def __init__( + self, + repo: NewsItemRepository, + application_repo: ApplicationRepository, + owner: Owner, + ): + """Initialize the use case. + + Args: + repo: The repository to create the news item. + application_repo: The repository to get the application entity. + owner: The owner of the news item. + """ + self._repo = repo + self._application_repo = application_repo + self._owner = owner + + async def execute(self, command: CreateNewsItemCommand) -> NewsItemEntity: + """Execute the use case. + + Args: + command: The input for this use case. + + Raises: + ApplicationNotFoundException: raised when the application does not exist. + """ + application = await self._application_repo.get_by_id( + ApplicationIdentifier(command.application) + ) + if command.promotion > 0: + if command.promotion_end_datetime is None: + promotion = Promotion(priority=command.promotion) + else: + promotion = Promotion( + priority=command.promotion, + end_date=LocalTimestamp.create_from_string( + command.promotion_end_datetime + ), + ) + else: + promotion = None + news_item = NewsItemEntity( + enabled=command.enabled, + promotion=promotion, + period=Period( + start_date=LocalTimestamp.create_from_string(command.publish_datetime), + end_date=LocalTimestamp.create_from_string(command.end_datetime), + ), + application=application, + texts=[ + LocaleText( + locale=Locale(text.locale), + format=DocumentFormat(text.format), + title=text.title, + content=text.content, + summary=text.summary, + author=self._owner, + ) + for text in command.texts + ], + remark=command.remark, + ) + + return await self._repo.create(news_item) diff --git a/backend/src/kwai/modules/portal/news_item_command.py b/backend/src/kwai/modules/portal/news_item_command.py new file mode 100644 index 000000000..c63d56cca --- /dev/null +++ b/backend/src/kwai/modules/portal/news_item_command.py @@ -0,0 +1,18 @@ +"""Module that defines common command for create/update news items.""" +from dataclasses import dataclass + +from kwai.core.domain.use_case import TextCommand + + +@dataclass(kw_only=True, frozen=True, slots=True) +class NewsItemCommand: + """Common input for the use cases "Create News Items" and "Update News Items".""" + + enabled: bool + texts: list[TextCommand] + application: int + publish_datetime: str + end_datetime: str | None + promotion: int + promotion_end_datetime: str | None + remark: str diff --git a/backend/src/tests/modules/portal/test_create_news_item.py b/backend/src/tests/modules/portal/test_create_news_item.py new file mode 100644 index 000000000..e4f7b5ff0 --- /dev/null +++ b/backend/src/tests/modules/portal/test_create_news_item.py @@ -0,0 +1,40 @@ +"""Module for testing the use case "Create News Item".""" +from kwai.core.domain.use_case import TextCommand +from kwai.core.domain.value_objects.owner import Owner +from kwai.modules.portal.applications.application import ApplicationEntity +from kwai.modules.portal.applications.application_repository import ( + ApplicationRepository, +) +from kwai.modules.portal.create_news_item import CreateNewsItem, CreateNewsItemCommand +from kwai.modules.portal.news.news_item_repository import NewsItemRepository + + +async def test_create_news_item( + news_item_repo: NewsItemRepository, + application: ApplicationEntity, + application_repo: ApplicationRepository, + owner: Owner, +): + """Test "Create News Item" use case.""" + command = CreateNewsItemCommand( + enabled=True, + texts=[ + TextCommand( + locale="en", + format="md", + title="Test", + summary="This is a test", + content="This is a test", + ) + ], + application=application.id.value, + publish_datetime="2023-01-01 00:00:00", + end_datetime=None, + promotion=0, + promotion_end_datetime=None, + remark="Created with test_create_news_item", + ) + news_item = await CreateNewsItem(news_item_repo, application_repo, owner).execute( + command + ) + assert news_item is not None, "There should be a news item."