230 lines
8.6 KiB
Python
230 lines
8.6 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
|
|
from .deps import get_content_preview_mode, get_content_service
|
|
from .errors import (
|
|
AmbiguousTemplateCodeError,
|
|
ContentProjectionError,
|
|
InvalidPresentationContractError,
|
|
InvalidSearchFieldError,
|
|
InvalidSearchOperatorError,
|
|
InvalidSearchValueError,
|
|
InvalidSortFieldError,
|
|
ModuleIdentityConflictError,
|
|
ModuleNotFoundError,
|
|
PresentationContractNotFoundError,
|
|
TemplateNotCollectionError,
|
|
TemplateNotFoundError,
|
|
TemplateNotInModuleError,
|
|
)
|
|
from .schema import (
|
|
ContentDetailResponse,
|
|
ContentSearchRequest,
|
|
ContentSearchResponse,
|
|
ModuleContentSearchRequest,
|
|
ModuleContentSearchResponse,
|
|
ModuleSearchContractResponse,
|
|
ModuleContentListResponse,
|
|
ModuleListResponse,
|
|
ModuleRequest,
|
|
ModuleTemplateContentDetailResponse,
|
|
ModuleTemplateContentRequest,
|
|
ModuleTemplateContentResponse,
|
|
PublishContentResponse,
|
|
TemplateAttributeDefinitionsResponse,
|
|
TemplateContractResponse,
|
|
)
|
|
from .service import ContentService
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter(prefix="/contents", tags=["Content"])
|
|
|
|
|
|
@router.get("/modules", response_model=ModuleListResponse)
|
|
async def get_modules(
|
|
preview: bool = Depends(get_content_preview_mode),
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> ModuleListResponse:
|
|
return await service.get_modules(preview=preview)
|
|
|
|
|
|
@router.post("/module-contents", response_model=ModuleContentListResponse)
|
|
async def get_module_contents(
|
|
payload: ModuleRequest,
|
|
preview: bool = Depends(get_content_preview_mode),
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> ModuleContentListResponse:
|
|
return await service.get_module_contents(
|
|
request=payload,
|
|
preview=preview,
|
|
)
|
|
|
|
|
|
@router.post("/module-template-contents", response_model=ModuleTemplateContentResponse)
|
|
async def get_module_template_contents(
|
|
payload: ModuleTemplateContentRequest,
|
|
preview: bool = Depends(get_content_preview_mode),
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> ModuleTemplateContentResponse:
|
|
return await service.get_module_contents_by_template(
|
|
request=payload,
|
|
preview=preview,
|
|
)
|
|
|
|
|
|
@router.post("/module-template-content-details", response_model=ModuleTemplateContentDetailResponse)
|
|
async def get_module_template_content_details(
|
|
payload: ModuleTemplateContentRequest,
|
|
preview: bool = Depends(get_content_preview_mode),
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> ModuleTemplateContentDetailResponse:
|
|
return await service.get_module_content_details_by_template(
|
|
request=payload,
|
|
preview=preview,
|
|
)
|
|
|
|
|
|
@router.get("/templates/{template_code}/attributes", response_model=TemplateAttributeDefinitionsResponse)
|
|
async def get_template_attributes(
|
|
template_code: str,
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> TemplateAttributeDefinitionsResponse:
|
|
return await service.get_template_attribute_definitions(template_code=template_code)
|
|
|
|
|
|
@router.get(
|
|
"/templates/{template_code}/contract",
|
|
response_model=TemplateContractResponse,
|
|
response_model_exclude_none=True,
|
|
)
|
|
async def get_template_contract(
|
|
template_code: str,
|
|
client_type: str = Query(default="mobile", min_length=1, max_length=32),
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> TemplateContractResponse:
|
|
try:
|
|
return await service.get_template_contract(
|
|
template_code=template_code,
|
|
client_type=client_type,
|
|
)
|
|
except TemplateNotFoundError as exc:
|
|
raise HTTPException(404, detail={"code": "TEMPLATE_NOT_FOUND", "template_code": exc.template_code}) from exc
|
|
except AmbiguousTemplateCodeError as exc:
|
|
raise HTTPException(409, detail={"code": "AMBIGUOUS_TEMPLATE_CODE", "template_code": exc.template_code, "matches": exc.matches}) from exc
|
|
except PresentationContractNotFoundError as exc:
|
|
raise HTTPException(404, detail={"code": "PRESENTATION_CONTRACT_NOT_FOUND", "template_code": exc.template_code, "client_type": exc.client_type}) from exc
|
|
except InvalidPresentationContractError as exc:
|
|
logger.exception("Invalid persisted presentation contract: template=%s", template_code)
|
|
raise HTTPException(500, detail={"code": "INVALID_PRESENTATION_CONTRACT", "template_code": template_code}) from exc
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
"/modules/{module_code}/search-contract",
|
|
response_model=ModuleSearchContractResponse,
|
|
)
|
|
async def get_module_search_contract(
|
|
module_code: str,
|
|
client_type: str = Query(default="mobile", min_length=1, max_length=32),
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> ModuleSearchContractResponse:
|
|
try:
|
|
return await service.get_module_search_contract(
|
|
module_code=module_code,
|
|
client_type=client_type,
|
|
)
|
|
except ModuleNotFoundError as exc:
|
|
raise HTTPException(
|
|
404,
|
|
detail={"code": "MODULE_NOT_FOUND", "message": str(exc)},
|
|
) from exc
|
|
|
|
|
|
@router.post("/module-search", response_model=ModuleContentSearchResponse)
|
|
async def search_module_contents(
|
|
payload: ModuleContentSearchRequest,
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> ModuleContentSearchResponse:
|
|
try:
|
|
return await service.search_module_contents(payload)
|
|
except ModuleNotFoundError as exc:
|
|
raise HTTPException(
|
|
404,
|
|
detail={"code": "MODULE_NOT_FOUND", "message": str(exc)},
|
|
) from exc
|
|
except (
|
|
InvalidSearchFieldError,
|
|
InvalidSearchOperatorError,
|
|
InvalidSearchValueError,
|
|
InvalidSortFieldError,
|
|
) as exc:
|
|
raise HTTPException(
|
|
422,
|
|
detail={
|
|
"code": type(exc).__name__.replace("Error", "").upper(),
|
|
"message": str(exc),
|
|
},
|
|
) from exc
|
|
|
|
@router.post("/search", response_model=ContentSearchResponse)
|
|
async def search_contents(
|
|
payload: ContentSearchRequest,
|
|
preview: bool = Depends(get_content_preview_mode),
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> ContentSearchResponse:
|
|
try:
|
|
return await service.search_contents(payload, preview=preview)
|
|
except ModuleNotFoundError as exc:
|
|
raise HTTPException(404, detail={"code": "MODULE_NOT_FOUND", "message": str(exc)}) from exc
|
|
except TemplateNotFoundError as exc:
|
|
raise HTTPException(404, detail={"code": "TEMPLATE_NOT_FOUND", "template_code": exc.template_code}) from exc
|
|
except PresentationContractNotFoundError as exc:
|
|
raise HTTPException(409, detail={"code": "PRESENTATION_CONTRACT_NOT_FOUND", "template_code": exc.template_code, "client_type": exc.client_type}) from exc
|
|
except (ModuleIdentityConflictError, TemplateNotInModuleError, TemplateNotCollectionError) as exc:
|
|
raise HTTPException(409, detail={"code": type(exc).__name__.replace("Error", "").upper(), "message": str(exc)}) from exc
|
|
except (InvalidSearchFieldError, InvalidSearchOperatorError, InvalidSearchValueError, InvalidSortFieldError) as exc:
|
|
raise HTTPException(422, detail={"code": type(exc).__name__.replace("Error", "").upper(), "message": str(exc)}) from exc
|
|
except ContentProjectionError as exc:
|
|
logger.exception("Content EAV projection failed")
|
|
raise HTTPException(500, detail={"code": "CONTENT_PROJECTION_ERROR"}) from exc
|
|
|
|
|
|
@router.get("/{content_id}")
|
|
async def get_content(
|
|
content_id: UUID,
|
|
preview: bool = Depends(get_content_preview_mode),
|
|
service: ContentService = Depends(get_content_service),
|
|
):
|
|
return await service.get_content_json(content_id, preview=preview)
|
|
|
|
|
|
@router.post("/module-contents/{content_id}", response_model=ContentDetailResponse)
|
|
async def get_module_content_detail(
|
|
content_id: UUID,
|
|
payload: ModuleRequest,
|
|
preview: bool = Depends(get_content_preview_mode),
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> ContentDetailResponse:
|
|
return await service.get_module_content_detail(
|
|
request=payload,
|
|
content_id=content_id,
|
|
preview=preview,
|
|
)
|
|
|
|
|
|
@router.post("/{content_id}/publish", response_model=PublishContentResponse)
|
|
async def publish_content(
|
|
content_id: UUID,
|
|
actor_id: UUID,
|
|
service: ContentService = Depends(get_content_service),
|
|
) -> PublishContentResponse:
|
|
response = await service.publish_content(content_id=content_id, actor_id=actor_id)
|
|
await service.session.commit()
|
|
await service.invalidate_after_content_commit(content_id=content_id)
|
|
return response
|