52ff2f80ad
CI/CD Pipeline / Validate Code Quality And Tests (push) Has been cancelled
CI/CD Pipeline / Frontend Lint (push) Has been cancelled
Deploy / Deploy Staging (push) Has been cancelled
Deploy / Build Production Runtime Images (push) Has been cancelled
Deploy / Deploy Production (push) Has been cancelled
Deploy / Production Browser E2E (push) Has been cancelled
93 lines
3.6 KiB
Python
93 lines
3.6 KiB
Python
from typing import Any
|
|
|
|
from app.auth import AuthenticatedUser, get_current_user
|
|
from app.dependencies import get_asset_library_repository, get_project_repository
|
|
from app.schemas.asset_library import (
|
|
AssetLibraryResponse,
|
|
CreateAssetLibraryRequest,
|
|
ListAssetLibrariesResponse,
|
|
)
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
|
|
from packages.application import (
|
|
CreateAssetLibraryCommand,
|
|
CreateAssetLibraryUseCase,
|
|
GetProjectUseCase,
|
|
ListAssetLibrariesUseCase,
|
|
)
|
|
from packages.domain import AssetLibraryKind
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _check_project_access(project_id: str, user_id: str, project_repository) -> None:
|
|
"""检查用户是否有项目访问权限"""
|
|
project = project_repository.find_by_id(project_id)
|
|
if project is None:
|
|
raise HTTPException(status_code=404, detail=f"Project {project_id} not found")
|
|
if not project.can_access(user_id):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Access denied to project")
|
|
|
|
|
|
def _to_asset_library_response(item) -> AssetLibraryResponse:
|
|
return AssetLibraryResponse(
|
|
id=item.id,
|
|
project_id=item.project_id,
|
|
name=item.name,
|
|
kind=item.kind.value,
|
|
asset_count=item.asset_count,
|
|
total_size=item.total_size,
|
|
)
|
|
|
|
|
|
@router.get("", response_model=ListAssetLibrariesResponse)
|
|
def list_asset_libraries(
|
|
project_id: str | None = Query(None),
|
|
authenticated_user: AuthenticatedUser = Depends(get_current_user),
|
|
asset_library_repository: Any = Depends(get_asset_library_repository),
|
|
project_repository: Any = Depends(get_project_repository),
|
|
) -> ListAssetLibrariesResponse:
|
|
user_id = authenticated_user.user.id
|
|
use_case = ListAssetLibrariesUseCase(asset_library_repository)
|
|
|
|
if project_id:
|
|
# If project_id provided, check access and filter by project
|
|
project = GetProjectUseCase(project_repository).execute(project_id)
|
|
if project is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
|
|
if not project.can_access(user_id):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Access denied to project")
|
|
items = use_case.execute(project_id)
|
|
else:
|
|
# If no project_id, list all libraries from accessible projects
|
|
accessible_projects = project_repository.find_accessible_projects(user_id)
|
|
all_items = []
|
|
for proj in accessible_projects:
|
|
all_items.extend(use_case.execute(proj.id))
|
|
items = all_items
|
|
|
|
return ListAssetLibrariesResponse(items=[_to_asset_library_response(item) for item in items])
|
|
|
|
|
|
@router.post("", response_model=AssetLibraryResponse)
|
|
def create_asset_library(
|
|
request: CreateAssetLibraryRequest,
|
|
authenticated_user: AuthenticatedUser = Depends(get_current_user),
|
|
asset_library_repository: Any = Depends(get_asset_library_repository),
|
|
project_repository: Any = Depends(get_project_repository),
|
|
) -> AssetLibraryResponse:
|
|
project = project_repository.find_by_id(request.project_id)
|
|
if project is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
|
|
if not project.can_access(authenticated_user.user.id):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Access denied to project")
|
|
use_case = CreateAssetLibraryUseCase(asset_library_repository)
|
|
item = use_case.execute(
|
|
CreateAssetLibraryCommand(
|
|
project_id=request.project_id,
|
|
name=request.name,
|
|
kind=AssetLibraryKind(request.kind),
|
|
)
|
|
)
|
|
return _to_asset_library_response(item)
|