first
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user