Coverage for bc/kwai-bc-portal/src/kwai_bc_portal/create_page.py: 100%
19 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 Page"."""
3from kwai_core.domain.presenter import Presenter
4from kwai_core.domain.value_objects.owner import Owner
5from kwai_core.domain.value_objects.text import DocumentFormat, Locale, LocaleText
7from kwai_bc_portal.applications.application_repository import (
8 ApplicationRepository,
9)
10from kwai_bc_portal.domain.application import ApplicationIdentifier
11from kwai_bc_portal.domain.page import PageEntity
12from kwai_bc_portal.page_command import PageCommand
13from kwai_bc_portal.pages.page_repository import PageRepository
16CreatePageCommand = PageCommand
19class CreatePage:
20 """Use case "Create Page"."""
22 def __init__(
23 self,
24 repo: PageRepository,
25 application_repo: ApplicationRepository,
26 owner: Owner,
27 presenter: Presenter[PageEntity],
28 ):
29 """Initialize the use case.
31 Args:
32 repo: The repository for creating a page.
33 owner: The user that owns the page.
34 application_repo: The repository for getting the application.
35 presenter: A presenter for a page entity.
36 """
37 self._repo = repo
38 self._application_repo = application_repo
39 self._owner = owner
40 self._presenter = presenter
42 async def execute(self, command: CreatePageCommand):
43 """Executes the use case.
45 Args:
46 command: the input for the use case.
48 Raises:
49 ApplicationNotFoundException: Raised when the application does not exist.
50 """
51 application = await self._application_repo.get_by_id(
52 ApplicationIdentifier(command.application)
53 )
54 page = PageEntity(
55 enabled=command.enabled,
56 application=application,
57 texts=tuple(
58 LocaleText(
59 locale=Locale(text.locale),
60 format=DocumentFormat(text.format),
61 title=text.title,
62 content=text.content,
63 summary=text.summary,
64 author=self._owner,
65 )
66 for text in command.texts
67 ),
68 priority=command.priority,
69 remark=command.remark,
70 )
72 self._presenter.present(await self._repo.create(page))