from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form from sqlalchemy.orm import Session from sqlalchemy.exc import IntegrityError from typing import List from app.db.database import get_db from app.core.deps import require_admin from app.models.models import Collection, Character from app.schemas.schemas import CollectionOut from app.services.storage import upload_image, delete_object_by_url router = APIRouter(dependencies=[Depends(require_admin)]) ALLOWED_TYPES = {"image/jpeg", "image/png", "image/webp", "image/gif"} @router.post("/collections", response_model=CollectionOut, status_code=201) async def create_collection( name: str = Form(...), files: List[UploadFile] = File(...), db: Session = Depends(get_db), ): name = name.strip() if not name: raise HTTPException(status_code=400, detail="Collection name required") if not files: raise HTTPException(status_code=400, detail="At least one image is required") collection = Collection(name=name) db.add(collection) try: db.flush() except IntegrityError: db.rollback() raise HTTPException(status_code=409, detail="A collection with this name already exists") for f in files: if f.content_type not in ALLOWED_TYPES: db.rollback() raise HTTPException( status_code=415, detail=f"Unsupported file type: {f.content_type}", ) data = await f.read() _, url = upload_image(data, f.filename or "image", f.content_type or "") char_name = (f.filename or "image").rsplit(".", 1)[0] db.add( Character( name=char_name, filename=f.filename or "image", s3_url=url, collection_id=collection.id, ) ) db.commit() db.refresh(collection) return collection @router.post("/collections/{collection_id}/characters", response_model=CollectionOut) async def add_characters( collection_id: int, files: List[UploadFile] = File(...), db: Session = Depends(get_db), ): collection = db.get(Collection, collection_id) if not collection: raise HTTPException(status_code=404, detail="Collection not found") for f in files: if f.content_type not in ALLOWED_TYPES: raise HTTPException(status_code=415, detail=f"Unsupported file type: {f.content_type}") data = await f.read() _, url = upload_image(data, f.filename or "image", f.content_type or "") char_name = (f.filename or "image").rsplit(".", 1)[0] db.add( Character( name=char_name, filename=f.filename or "image", s3_url=url, collection_id=collection.id, ) ) db.commit() db.refresh(collection) return collection @router.delete("/collections/{collection_id}", status_code=204) def delete_collection(collection_id: int, db: Session = Depends(get_db)): collection = db.get(Collection, collection_id) if not collection: raise HTTPException(status_code=404, detail="Collection not found") urls = [c.s3_url for c in collection.characters] db.delete(collection) db.commit() for u in urls: delete_object_by_url(u) @router.delete("/characters/{character_id}", status_code=204) def delete_character(character_id: int, db: Session = Depends(get_db)): ch = db.get(Character, character_id) if not ch: raise HTTPException(status_code=404, detail="Character not found") url = ch.s3_url db.delete(ch) db.commit() delete_object_by_url(url)