新增管理员接口 GET /profiles/{user_id}/stats,返回用户全部/公开上传数和收藏数

This commit is contained in:
2026-07-26 01:37:15 +08:00
parent 9c7ff9c489
commit 53548d2788
3 changed files with 115 additions and 3 deletions
+41 -3
View File
@@ -6,9 +6,9 @@ from sqlalchemy import func, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import joinedload
from app.deps import CurrentUser, DbSession, OptionalUser
from app.models import Cloud, Profile, User
from app.schemas import CloudOut, PageOut, ProfileOut, ProfileUpdateIn, PublicProfileOut
from app.deps import AdminUser, CurrentUser, DbSession, OptionalUser
from app.models import Cloud, Profile, User, UserCollection
from app.schemas import CloudOut, PageOut, ProfileOut, ProfileStatsOut, ProfileUpdateIn, PublicProfileOut
from app.serializers import cloud_out, profile_out, public_profile_out
@@ -38,6 +38,44 @@ async def update_my_profile(
return profile_out(user.profile)
@router.get("/{user_id}/stats", response_model=ProfileStatsOut)
async def get_user_stats(
user_id: uuid.UUID,
_: AdminUser,
db: DbSession,
) -> ProfileStatsOut:
result = await db.execute(
select(User).options(joinedload(User.profile)).where(User.id == user_id)
)
user = result.scalar_one_or_none()
if not user:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")
cloud_count = await db.scalar(
select(func.count()).select_from(Cloud).where(Cloud.user_id == user_id)
) or 0
public_cloud_count = await db.scalar(
select(func.count()).select_from(Cloud).where(
Cloud.user_id == user_id,
Cloud.status == "approved",
Cloud.is_hidden.is_(False),
)
) or 0
collection_count = await db.scalar(
select(func.count()).select_from(UserCollection).where(UserCollection.user_id == user_id)
) or 0
return ProfileStatsOut(
user_id=user.id,
username=user.profile.username,
email=user.email,
created_at=user.created_at,
cloud_count=cloud_count,
public_cloud_count=public_cloud_count,
collection_count=collection_count,
)
@router.get("/{user_id}/clouds", response_model=PageOut[CloudOut])
async def get_profile_clouds(
user_id: uuid.UUID,