Exception handling enhancement

This commit is contained in:
2025-09-23 22:42:26 +02:00
parent b1a76cdb57
commit 98285aa8aa
35 changed files with 3283 additions and 1743 deletions

View File

@@ -1,16 +1,18 @@
# app/api/v1/admin.py
"""Summary description ....
"""
Admin endpoints - simplified with service-level exception handling.
This module provides classes and functions for:
- ....
- ....
- ....
- User management (view, toggle status)
- Shop management (view, verify, toggle status)
- Marketplace import job monitoring
- Admin dashboard statistics
"""
import logging
from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from app.api.deps import get_current_admin_user
@@ -25,7 +27,6 @@ router = APIRouter()
logger = logging.getLogger(__name__)
# Admin-only routes
@router.get("/admin/users", response_model=List[UserResponse])
def get_all_users(
skip: int = Query(0, ge=0),
@@ -34,12 +35,8 @@ def get_all_users(
current_admin: User = Depends(get_current_admin_user),
):
"""Get all users (Admin only)."""
try:
users = admin_service.get_all_users(db=db, skip=skip, limit=limit)
return [UserResponse.model_validate(user) for user in users]
except Exception as e:
logger.error(f"Error getting users: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
users = admin_service.get_all_users(db=db, skip=skip, limit=limit)
return [UserResponse.model_validate(user) for user in users]
@router.put("/admin/users/{user_id}/status")
@@ -49,14 +46,8 @@ def toggle_user_status(
current_admin: User = Depends(get_current_admin_user),
):
"""Toggle user active status (Admin only)."""
try:
user, message = admin_service.toggle_user_status(db, user_id, current_admin.id)
return {"message": message}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error toggling user {user_id} status: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
user, message = admin_service.toggle_user_status(db, user_id, current_admin.id)
return {"message": message}
@router.get("/admin/shops", response_model=ShopListResponse)
@@ -67,13 +58,8 @@ def get_all_shops_admin(
current_admin: User = Depends(get_current_admin_user),
):
"""Get all shops with admin view (Admin only)."""
try:
shops, total = admin_service.get_all_shops(db=db, skip=skip, limit=limit)
return ShopListResponse(shops=shops, total=total, skip=skip, limit=limit)
except Exception as e:
logger.error(f"Error getting shops: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
shops, total = admin_service.get_all_shops(db=db, skip=skip, limit=limit)
return ShopListResponse(shops=shops, total=total, skip=skip, limit=limit)
@router.put("/admin/shops/{shop_id}/verify")
@@ -83,14 +69,8 @@ def verify_shop(
current_admin: User = Depends(get_current_admin_user),
):
"""Verify/unverify shop (Admin only)."""
try:
shop, message = admin_service.verify_shop(db, shop_id)
return {"message": message}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error verifying shop {shop_id}: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
shop, message = admin_service.verify_shop(db, shop_id)
return {"message": message}
@router.put("/admin/shops/{shop_id}/status")
@@ -100,14 +80,8 @@ def toggle_shop_status(
current_admin: User = Depends(get_current_admin_user),
):
"""Toggle shop active status (Admin only)."""
try:
shop, message = admin_service.toggle_shop_status(db, shop_id)
return {"message": message}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error toggling shop {shop_id} status: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
shop, message = admin_service.toggle_shop_status(db, shop_id)
return {"message": message}
@router.get(
@@ -123,15 +97,29 @@ def get_all_marketplace_import_jobs(
current_admin: User = Depends(get_current_admin_user),
):
"""Get all marketplace import jobs (Admin only)."""
try:
return admin_service.get_marketplace_import_jobs(
db=db,
marketplace=marketplace,
shop_name=shop_name,
status=status,
skip=skip,
limit=limit,
)
except Exception as e:
logger.error(f"Error getting marketplace import jobs: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
return admin_service.get_marketplace_import_jobs(
db=db,
marketplace=marketplace,
shop_name=shop_name,
status=status,
skip=skip,
limit=limit,
)
@router.get("/admin/stats/users")
def get_user_statistics(
db: Session = Depends(get_db),
current_admin: User = Depends(get_current_admin_user),
):
"""Get user statistics for admin dashboard (Admin only)."""
return admin_service.get_user_statistics(db)
@router.get("/admin/stats/shops")
def get_shop_statistics(
db: Session = Depends(get_db),
current_admin: User = Depends(get_current_admin_user),
):
"""Get shop statistics for admin dashboard (Admin only)."""
return admin_service.get_shop_statistics(db)