Coverage for src/kwai/modules/portal/create_page.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module for defining the use case "Create Page"."""
3from kwai.core.domain.value_objects.owner import Owner
4from kwai.core.domain.value_objects.text import DocumentFormat, Locale, LocaleText
5from kwai.modules.portal.applications.application import ApplicationIdentifier
6from kwai.modules.portal.applications.application_repository import (
7 ApplicationRepository,
8)
9from kwai.modules.portal.page_command import PageCommand
10from kwai.modules.portal.pages.page import PageEntity
11from kwai.modules.portal.pages.page_repository import PageRepository
14CreatePageCommand = PageCommand
17class CreatePage:
18 """Use case "Create Page"."""
20 def __init__(
21 self,
22 repo: PageRepository,
23 application_repo: ApplicationRepository,
24 owner: Owner,
25 ):
26 """Initialize the use case.
28 Args:
29 repo: The repository for creating a page.
30 owner: The user that owns the page.
31 application_repo: The repository for getting the application.
32 """
33 self._repo = repo
34 self._application_repo = application_repo
35 self._owner = owner
37 async def execute(self, command: CreatePageCommand) -> PageEntity:
38 """Executes the use case.
40 Args:
41 command: the input for the use case.
43 Raises:
44 ApplicationNotFoundException: Raised when the application does not exist.
45 """
46 application = await self._application_repo.get_by_id(
47 ApplicationIdentifier(command.application)
48 )
49 page = PageEntity(
50 enabled=command.enabled,
51 application=application,
52 texts=[
53 LocaleText(
54 locale=Locale(text.locale),
55 format=DocumentFormat(text.format),
56 title=text.title,
57 content=text.content,
58 summary=text.summary,
59 author=self._owner,
60 )
61 for text in command.texts
62 ],
63 priority=command.priority,
64 remark=command.remark,
65 )
66 return await self._repo.create(page)