Coverage for src/kwai/api/v1/pages/endpoints/pages.py: 88%
56 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 pages endpoint."""
3from fastapi import APIRouter, Depends, HTTPException, Query, status
4from pydantic import BaseModel, Field
6from kwai.api.dependencies import create_database, get_current_user
7from kwai.api.schemas.page import PageDocument
8from kwai.core.domain.use_case import TextCommand
9from kwai.core.domain.value_objects.owner import Owner
10from kwai.core.json_api import Meta, PaginationModel
11from kwai.modules.identity.users.user import UserEntity
12from kwai.modules.portal.applications.application_db_repository import (
13 ApplicationDbRepository,
14)
15from kwai.modules.portal.applications.application_repository import (
16 ApplicationNotFoundException,
17)
18from kwai.modules.portal.create_page import CreatePage, CreatePageCommand
19from kwai.modules.portal.delete_page import DeletePage, DeletePageCommand
20from kwai.modules.portal.get_page import GetPage, GetPageCommand
21from kwai.modules.portal.get_pages import GetPages, GetPagesCommand
22from kwai.modules.portal.pages.page_db_repository import PageDbRepository
23from kwai.modules.portal.pages.page_repository import PageNotFoundException
24from kwai.modules.portal.update_page import UpdatePage, UpdatePageCommand
27router = APIRouter()
30class PageFilter(BaseModel):
31 """Define the JSON:API filter for pages."""
33 application: str | None = Field(Query(default=None, alias="filter[application]"))
36@router.get("/pages")
37async def get_pages(
38 pagination: PaginationModel = Depends(PaginationModel),
39 page_filter: PageFilter = Depends(PageFilter),
40 db=Depends(create_database),
41) -> PageDocument:
42 """Get pages."""
43 command = GetPagesCommand(
44 offset=pagination.offset or 0,
45 limit=pagination.limit,
46 application=page_filter.application,
47 )
48 count, page_iterator = await GetPages(PageDbRepository(db)).execute(command)
50 result = PageDocument(
51 meta=Meta(count=count, offset=command.offset, limit=command.limit),
52 data=[],
53 included=set(),
54 )
56 async for page in page_iterator:
57 result.merge(PageDocument.create(page))
59 return result
62@router.get("/pages/{id}")
63async def get_page(
64 id: int,
65 db=Depends(create_database),
66) -> PageDocument:
67 """Get page."""
68 command = GetPageCommand(id=id)
69 page = await GetPage(PageDbRepository(db)).execute(command)
71 return PageDocument.create(page)
74@router.post("/pages", status_code=status.HTTP_201_CREATED)
75async def create_page(
76 resource: PageDocument,
77 db=Depends(create_database),
78 user: UserEntity = Depends(get_current_user),
79) -> PageDocument:
80 """Create a page."""
81 command = CreatePageCommand(
82 enabled=resource.data.attributes.enabled,
83 texts=[
84 TextCommand(
85 locale=text.locale,
86 format=text.format,
87 title=text.title,
88 summary=text.original_summary,
89 content=text.original_content,
90 )
91 for text in resource.data.attributes.texts
92 ],
93 application=int(resource.data.relationships.application.data.id),
94 priority=resource.data.attributes.priority,
95 remark=resource.data.attributes.remark,
96 )
98 try:
99 page = await CreatePage(
100 PageDbRepository(db),
101 ApplicationDbRepository(db),
102 Owner(id=user.id, uuid=user.uuid, name=user.name),
103 ).execute(command)
104 except ApplicationNotFoundException as ex:
105 raise HTTPException(
106 status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(ex)
107 ) from ex
109 return PageDocument.create(page)
112@router.patch("/pages/{id}")
113async def update_page(
114 id: int,
115 resource: PageDocument,
116 db=Depends(create_database),
117 user: UserEntity = Depends(get_current_user),
118) -> PageDocument:
119 """Update a page."""
120 command = UpdatePageCommand(
121 id=id,
122 enabled=resource.data.attributes.enabled,
123 texts=[
124 TextCommand(
125 locale=text.locale,
126 format=text.format,
127 title=text.title,
128 summary=text.original_summary,
129 content=text.original_content,
130 )
131 for text in resource.data.attributes.texts
132 ],
133 application=int(resource.data.relationships.application.data.id),
134 priority=resource.data.attributes.priority,
135 remark=resource.data.attributes.remark,
136 )
137 try:
138 page = await UpdatePage(
139 PageDbRepository(db),
140 ApplicationDbRepository(db),
141 Owner(id=user.id, uuid=user.uuid, name=user.name),
142 ).execute(command)
143 except ApplicationNotFoundException as ex:
144 raise HTTPException(
145 status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(ex)
146 ) from ex
148 return PageDocument.create(page)
151@router.delete(
152 "/pages/{id}",
153 status_code=status.HTTP_200_OK,
154 responses={status.HTTP_404_NOT_FOUND: {"description": "Page was not found."}},
155)
156async def delete_news_item(
157 id: int,
158 db=Depends(create_database),
159 user: UserEntity = Depends(get_current_user),
160):
161 """Delete a page."""
162 command = DeletePageCommand(id=id)
164 try:
165 await DeletePage(PageDbRepository(db)).execute(command)
166 except PageNotFoundException as ex:
167 raise HTTPException(
168 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex)
169 ) from ex