This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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." |