Coverage for src/kwai/api/v1/portal/endpoints/applications.py: 94%
35 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 that implements applications endpoints."""
3from fastapi import APIRouter, Depends, HTTPException, status
5from kwai.api.dependencies import create_database, get_current_user
6from kwai.api.schemas.application import ApplicationDocument
7from kwai.core.json_api import Meta
8from kwai.modules.identity.users.user import UserEntity
9from kwai.modules.portal.applications.application_db_repository import (
10 ApplicationDbRepository,
11)
12from kwai.modules.portal.applications.application_repository import (
13 ApplicationNotFoundException,
14)
15from kwai.modules.portal.get_application import GetApplication, GetApplicationCommand
16from kwai.modules.portal.get_applications import GetApplications, GetApplicationsCommand
17from kwai.modules.portal.update_application import (
18 UpdateApplication,
19 UpdateApplicationCommand,
20)
23router = APIRouter()
26@router.get("/applications")
27async def get_applications(
28 db=Depends(create_database),
29) -> ApplicationDocument:
30 """Get all applications of kwai."""
31 command = GetApplicationsCommand()
32 count, application_iterator = await GetApplications(
33 ApplicationDbRepository(db)
34 ).execute(command)
36 document = ApplicationDocument(meta=Meta(count=count), data=[])
37 async for application in application_iterator:
38 document.merge(ApplicationDocument.create(application))
40 return document
43@router.get("/applications/{id}")
44async def get_application(
45 id: int,
46 db=Depends(create_database),
47) -> ApplicationDocument:
48 """Get application."""
49 command = GetApplicationCommand(id=id)
51 try:
52 application = await GetApplication(ApplicationDbRepository(db)).execute(command)
53 except ApplicationNotFoundException as ex:
54 raise HTTPException(
55 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex)
56 ) from ex
58 return ApplicationDocument.create(application)
61@router.patch("/applications/{id}")
62async def update_application(
63 id: int,
64 resource: ApplicationDocument,
65 db=Depends(create_database),
66 user: UserEntity = Depends(get_current_user),
67) -> ApplicationDocument:
68 """Get application."""
69 command = UpdateApplicationCommand(
70 id=id,
71 title=resource.data.attributes.title,
72 short_description=resource.data.attributes.short_description,
73 description=resource.data.attributes.description,
74 remark=resource.data.attributes.remark,
75 weight=resource.data.attributes.weight,
76 events=resource.data.attributes.events,
77 pages=resource.data.attributes.pages,
78 news=resource.data.attributes.news,
79 )
81 try:
82 application = await UpdateApplication(ApplicationDbRepository(db)).execute(
83 command
84 )
85 except ApplicationNotFoundException as ex:
86 raise HTTPException(
87 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex)
88 ) from ex
90 return ApplicationDocument.create(application)