Coverage for apps/kwai-api/src/kwai_api/v1/portal/applications/endpoints.py: 94%
35 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 that implements applications endpoints."""
3from fastapi import APIRouter, Depends, HTTPException, status
4from kwai_bc_identity.users.user import UserEntity
5from kwai_bc_portal.applications.application_db_repository import (
6 ApplicationDbRepository,
7)
8from kwai_bc_portal.applications.application_repository import (
9 ApplicationNotFoundException,
10)
11from kwai_bc_portal.get_application import GetApplication, GetApplicationCommand
12from kwai_bc_portal.get_applications import GetApplications, GetApplicationsCommand
13from kwai_bc_portal.update_application import (
14 UpdateApplication,
15 UpdateApplicationCommand,
16)
18from kwai_api.dependencies import create_database, get_current_user
19from kwai_api.v1.portal.applications.presenters import (
20 JsonApiApplicationPresenter,
21 JsonApiApplicationsPresenter,
22)
23from kwai_api.v1.portal.applications.schemas import (
24 ApplicationDocument,
25 ApplicationsDocument,
26)
29router = APIRouter()
32@router.get("/applications")
33async def get_applications(
34 db=Depends(create_database),
35) -> ApplicationsDocument:
36 """Get all applications of kwai."""
37 command = GetApplicationsCommand()
38 presenter = JsonApiApplicationsPresenter()
39 await GetApplications(ApplicationDbRepository(db), presenter).execute(command)
40 return presenter.get_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)
50 presenter = JsonApiApplicationPresenter()
51 try:
52 await GetApplication(ApplicationDbRepository(db), presenter).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 presenter.get_document()
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 )
80 presenter = JsonApiApplicationPresenter()
82 try:
83 await UpdateApplication(ApplicationDbRepository(db), presenter).execute(command)
84 except ApplicationNotFoundException as ex:
85 raise HTTPException(
86 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex)
87 ) from ex
89 return presenter.get_document()