fix: resolve architecture validation errors in media and customers APIs
- Add proper media exceptions (MediaNotFoundException, MediaUploadException, etc.) - Update media service to use exceptions from app/exceptions/media - Remove direct HTTPException raises from vendor/media.py and vendor/customers.py - Move db.query from customers API to service layer (get_customer_orders) - Fix pagination macro call in vendor/media.html template - Update media.js: add parent init call, PlatformSettings, apiClient.postFormData - Add try/catch error handling to media.js init method 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
183
app/api/v1/vendor/media.py
vendored
183
app/api/v1/vendor/media.py
vendored
@@ -8,12 +8,13 @@ The get_current_vendor_api dependency guarantees token_vendor_id is present.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile
|
||||
from fastapi import APIRouter, Depends, File, Query, UploadFile
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_vendor_api
|
||||
from app.core.database import get_db
|
||||
from app.services.media_service import MediaNotFoundException, media_service
|
||||
from app.exceptions.media import MediaOptimizationException
|
||||
from app.services.media_service import media_service
|
||||
from models.database.user import User
|
||||
from models.schema.media import (
|
||||
MediaDetailResponse,
|
||||
@@ -86,35 +87,29 @@ async def upload_media(
|
||||
- Save metadata to database
|
||||
- Return file URL
|
||||
"""
|
||||
try:
|
||||
# Read file content
|
||||
file_content = await file.read()
|
||||
# Read file content
|
||||
file_content = await file.read()
|
||||
|
||||
# Upload using service
|
||||
media_file = await media_service.upload_file(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
file_content=file_content,
|
||||
filename=file.filename or "unnamed",
|
||||
folder=folder or "general",
|
||||
)
|
||||
# Upload using service (exceptions will propagate to handler)
|
||||
media_file = await media_service.upload_file(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
file_content=file_content,
|
||||
filename=file.filename or "unnamed",
|
||||
folder=folder or "general",
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
return MediaUploadResponse(
|
||||
id=media_file.id,
|
||||
file_url=media_file.file_url,
|
||||
thumbnail_url=media_file.thumbnail_url,
|
||||
filename=media_file.original_filename,
|
||||
file_size=media_file.file_size,
|
||||
media_type=media_file.media_type,
|
||||
message="File uploaded successfully",
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to upload media: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return MediaUploadResponse(
|
||||
id=media_file.id,
|
||||
file_url=media_file.file_url,
|
||||
thumbnail_url=media_file.thumbnail_url,
|
||||
filename=media_file.original_filename,
|
||||
file_size=media_file.file_size,
|
||||
media_type=media_file.media_type,
|
||||
message="File uploaded successfully",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/upload/multiple", response_model=MultipleUploadResponse)
|
||||
@@ -185,17 +180,14 @@ def get_media_details(
|
||||
- Return file URL
|
||||
- Return basic info
|
||||
"""
|
||||
try:
|
||||
media = media_service.get_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
media = media_service.get_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
|
||||
return MediaDetailResponse.model_validate(media)
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
return MediaDetailResponse.model_validate(media)
|
||||
|
||||
|
||||
@router.put("/{media_id}", response_model=MediaDetailResponse)
|
||||
@@ -213,27 +205,21 @@ def update_media_metadata(
|
||||
- Update description
|
||||
- Move to different folder
|
||||
"""
|
||||
try:
|
||||
media = media_service.update_media_metadata(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
filename=metadata.filename,
|
||||
alt_text=metadata.alt_text,
|
||||
description=metadata.description,
|
||||
folder=metadata.folder,
|
||||
metadata=metadata.metadata,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
media = media_service.update_media_metadata(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
filename=metadata.filename,
|
||||
alt_text=metadata.alt_text,
|
||||
description=metadata.description,
|
||||
folder=metadata.folder,
|
||||
metadata=metadata.metadata,
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
return MediaDetailResponse.model_validate(media)
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return MediaDetailResponse.model_validate(media)
|
||||
|
||||
|
||||
@router.delete("/{media_id}", response_model=MediaDetailResponse)
|
||||
@@ -250,22 +236,16 @@ def delete_media(
|
||||
- Delete database record
|
||||
- Return success/error
|
||||
"""
|
||||
try:
|
||||
media_service.delete_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
media_service.delete_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
return MediaDetailResponse(message="Media file deleted successfully")
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return MediaDetailResponse(message="Media file deleted successfully")
|
||||
|
||||
|
||||
@router.get("/{media_id}/usage", response_model=MediaUsageResponse)
|
||||
@@ -280,17 +260,14 @@ def get_media_usage(
|
||||
- Check products using this media
|
||||
- Return list of usage
|
||||
"""
|
||||
try:
|
||||
usage = media_service.get_media_usage(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
usage = media_service.get_media_usage(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
|
||||
return MediaUsageResponse(**usage)
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
return MediaUsageResponse(**usage)
|
||||
|
||||
|
||||
@router.post("/optimize/{media_id}", response_model=OptimizationResultResponse)
|
||||
@@ -304,30 +281,24 @@ def optimize_media(
|
||||
|
||||
Note: Image optimization requires PIL/Pillow to be installed.
|
||||
"""
|
||||
try:
|
||||
media = media_service.get_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
# Service will raise MediaNotFoundException if not found
|
||||
media = media_service.get_media(
|
||||
db=db,
|
||||
vendor_id=current_user.token_vendor_id,
|
||||
media_id=media_id,
|
||||
)
|
||||
|
||||
if media.media_type != "image":
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Only images can be optimized"
|
||||
)
|
||||
if media.media_type != "image":
|
||||
raise MediaOptimizationException("Only images can be optimized")
|
||||
|
||||
# For now, return current state - optimization is done on upload
|
||||
return OptimizationResultResponse(
|
||||
media_id=media_id,
|
||||
original_size=media.file_size,
|
||||
optimized_size=media.optimized_size or media.file_size,
|
||||
savings_percent=0.0 if not media.optimized_size else
|
||||
round((1 - media.optimized_size / media.file_size) * 100, 1),
|
||||
optimized_url=media.file_url,
|
||||
message="Image optimization applied on upload" if media.is_optimized
|
||||
else "Image not yet optimized",
|
||||
)
|
||||
|
||||
except MediaNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Media file not found")
|
||||
# For now, return current state - optimization is done on upload
|
||||
return OptimizationResultResponse(
|
||||
media_id=media_id,
|
||||
original_size=media.file_size,
|
||||
optimized_size=media.optimized_size or media.file_size,
|
||||
savings_percent=0.0 if not media.optimized_size else
|
||||
round((1 - media.optimized_size / media.file_size) * 100, 1),
|
||||
optimized_url=media.file_url,
|
||||
message="Image optimization applied on upload" if media.is_optimized
|
||||
else "Image not yet optimized",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user