Refactoring code for modular approach
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, BackgroundTasks
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import get_db
|
||||
from app.api.deps import get_current_user, get_current_admin_user
|
||||
from app.tasks.background_tasks import process_marketplace_import
|
||||
from app.api.deps import get_current_admin_user
|
||||
from app.services.admin_service import admin_service
|
||||
from middleware.decorators import rate_limit
|
||||
from models.api_models import MarketplaceImportJobResponse, MarketplaceImportRequest, UserResponse, ShopListResponse
|
||||
from models.database_models import User, MarketplaceImportJob, Shop
|
||||
from datetime import datetime
|
||||
from models.api_models import MarketplaceImportJobResponse, UserResponse, ShopListResponse
|
||||
from models.database_models import User
|
||||
import logging
|
||||
|
||||
router = APIRouter()
|
||||
@@ -24,8 +23,12 @@ def get_all_users(
|
||||
current_admin: User = Depends(get_current_admin_user)
|
||||
):
|
||||
"""Get all users (Admin only)"""
|
||||
users = db.query(User).offset(skip).limit(limit).all()
|
||||
return [UserResponse.model_validate(user) for user in users]
|
||||
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")
|
||||
|
||||
|
||||
@router.put("/admin/users/{user_id}/status")
|
||||
@@ -35,20 +38,14 @@ def toggle_user_status(
|
||||
current_admin: User = Depends(get_current_admin_user)
|
||||
):
|
||||
"""Toggle user active status (Admin only)"""
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
if user.id == current_admin.id:
|
||||
raise HTTPException(status_code=400, detail="Cannot deactivate your own account")
|
||||
|
||||
user.is_active = not user.is_active
|
||||
user.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
|
||||
status = "activated" if user.is_active else "deactivated"
|
||||
return {"message": f"User {user.username} has been {status}"}
|
||||
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")
|
||||
|
||||
|
||||
@router.get("/admin/shops", response_model=ShopListResponse)
|
||||
@@ -59,15 +56,18 @@ def get_all_shops_admin(
|
||||
current_admin: User = Depends(get_current_admin_user)
|
||||
):
|
||||
"""Get all shops with admin view (Admin only)"""
|
||||
total = db.query(Shop).count()
|
||||
shops = db.query(Shop).offset(skip).limit(limit).all()
|
||||
try:
|
||||
shops, total = admin_service.get_all_shops(db=db, skip=skip, limit=limit)
|
||||
|
||||
return ShopListResponse(
|
||||
shops=shops,
|
||||
total=total,
|
||||
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")
|
||||
|
||||
|
||||
@router.put("/admin/shops/{shop_id}/verify")
|
||||
@@ -77,17 +77,14 @@ def verify_shop(
|
||||
current_admin: User = Depends(get_current_admin_user)
|
||||
):
|
||||
"""Verify/unverify shop (Admin only)"""
|
||||
shop = db.query(Shop).filter(Shop.id == shop_id).first()
|
||||
if not shop:
|
||||
raise HTTPException(status_code=404, detail="Shop not found")
|
||||
|
||||
shop.is_verified = not shop.is_verified
|
||||
shop.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(shop)
|
||||
|
||||
status = "verified" if shop.is_verified else "unverified"
|
||||
return {"message": f"Shop {shop.shop_code} has been {status}"}
|
||||
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")
|
||||
|
||||
|
||||
@router.put("/admin/shops/{shop_id}/status")
|
||||
@@ -97,17 +94,14 @@ def toggle_shop_status(
|
||||
current_admin: User = Depends(get_current_admin_user)
|
||||
):
|
||||
"""Toggle shop active status (Admin only)"""
|
||||
shop = db.query(Shop).filter(Shop.id == shop_id).first()
|
||||
if not shop:
|
||||
raise HTTPException(status_code=404, detail="Shop not found")
|
||||
|
||||
shop.is_active = not shop.is_active
|
||||
shop.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(shop)
|
||||
|
||||
status = "activated" if shop.is_active else "deactivated"
|
||||
return {"message": f"Shop {shop.shop_code} has been {status}"}
|
||||
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")
|
||||
|
||||
|
||||
@router.get("/admin/marketplace-import-jobs", response_model=List[MarketplaceImportJobResponse])
|
||||
@@ -121,33 +115,15 @@ def get_all_marketplace_import_jobs(
|
||||
current_admin: User = Depends(get_current_admin_user)
|
||||
):
|
||||
"""Get all marketplace import jobs (Admin only)"""
|
||||
|
||||
query = db.query(MarketplaceImportJob)
|
||||
|
||||
# Apply filters
|
||||
if marketplace:
|
||||
query = query.filter(MarketplaceImportJob.marketplace.ilike(f"%{marketplace}%"))
|
||||
if shop_name:
|
||||
query = query.filter(MarketplaceImportJob.shop_name.ilike(f"%{shop_name}%"))
|
||||
if status:
|
||||
query = query.filter(MarketplaceImportJob.status == status)
|
||||
|
||||
# Order by creation date and apply pagination
|
||||
jobs = query.order_by(MarketplaceImportJob.created_at.desc()).offset(skip).limit(limit).all()
|
||||
|
||||
return [
|
||||
MarketplaceImportJobResponse(
|
||||
job_id=job.id,
|
||||
status=job.status,
|
||||
marketplace=job.marketplace,
|
||||
shop_name=job.shop_name,
|
||||
imported=job.imported_count or 0,
|
||||
updated=job.updated_count or 0,
|
||||
total_processed=job.total_processed or 0,
|
||||
error_count=job.error_count or 0,
|
||||
error_message=job.error_message,
|
||||
created_at=job.created_at,
|
||||
started_at=job.started_at,
|
||||
completed_at=job.completed_at
|
||||
) for job in jobs
|
||||
]
|
||||
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")
|
||||
|
||||
@@ -19,7 +19,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Enhanced Product Routes with Marketplace Support
|
||||
@router.get("/products", response_model=ProductListResponse)
|
||||
@router.get("/product", response_model=ProductListResponse)
|
||||
def get_products(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
@@ -58,7 +58,7 @@ def get_products(
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.post("/products", response_model=ProductResponse)
|
||||
@router.post("/product", response_model=ProductResponse)
|
||||
def create_product(
|
||||
product: ProductCreate,
|
||||
db: Session = Depends(get_db),
|
||||
@@ -86,7 +86,7 @@ def create_product(
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.get("/products/{product_id}", response_model=ProductDetailResponse)
|
||||
@router.get("/product/{product_id}", response_model=ProductDetailResponse)
|
||||
def get_product(
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
@@ -116,7 +116,7 @@ def get_product(
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.put("/products/{product_id}", response_model=ProductResponse)
|
||||
@router.put("/product/{product_id}", response_model=ProductResponse)
|
||||
def update_product(
|
||||
product_id: str,
|
||||
product_update: ProductUpdate,
|
||||
@@ -142,7 +142,7 @@ def update_product(
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.delete("/products/{product_id}")
|
||||
@router.delete("/product/{product_id}")
|
||||
def delete_product(
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
Reference in New Issue
Block a user