first
This commit is contained in:
0
sop-back/app/api/routes/__init__.py
Normal file
0
sop-back/app/api/routes/__init__.py
Normal file
109
sop-back/app/api/routes/admin.py
Normal file
109
sop-back/app/api/routes/admin.py
Normal file
@@ -0,0 +1,109 @@
|
||||
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)
|
||||
31
sop-back/app/api/routes/collections.py
Normal file
31
sop-back/app/api/routes/collections.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from app.db.database import get_db
|
||||
from app.models.models import Collection, Character
|
||||
from app.schemas.schemas import CollectionSummary, CollectionOut
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("", response_model=List[CollectionSummary])
|
||||
def list_collections(db: Session = Depends(get_db)):
|
||||
rows = db.query(Collection).order_by(Collection.created_at.desc()).all()
|
||||
return [
|
||||
CollectionSummary(
|
||||
id=c.id,
|
||||
name=c.name,
|
||||
created_at=c.created_at,
|
||||
character_count=len(c.characters),
|
||||
)
|
||||
for c in rows
|
||||
]
|
||||
|
||||
|
||||
@router.get("/{collection_id}", response_model=CollectionOut)
|
||||
def get_collection(collection_id: int, db: Session = Depends(get_db)):
|
||||
c = db.get(Collection, collection_id)
|
||||
if not c:
|
||||
raise HTTPException(status_code=404, detail="Collection not found")
|
||||
return c
|
||||
15
sop-back/app/api/routes/health.py
Normal file
15
sop-back/app/api/routes/health.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from fastapi import APIRouter
|
||||
from app.core.config import settings
|
||||
from app.schemas.schemas import AdminStatus
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/admin/status", response_model=AdminStatus)
|
||||
def admin_status():
|
||||
return AdminStatus(admin_enabled=settings.ADMIN_ENABLED)
|
||||
Reference in New Issue
Block a user