
- Set up FastAPI application with SQLite database - Implement User and Item models with relationships - Add CRUD operations for users and items - Configure Alembic for database migrations - Include API documentation at /docs and /redoc - Add health check endpoint at /health - Enable CORS for all origins - Structure code with proper separation of concerns
29 lines
859 B
Python
29 lines
859 B
Python
from typing import List
|
|
from sqlalchemy.orm import Session
|
|
from app.crud.base import CRUDBase
|
|
from app.models.item import Item
|
|
from app.schemas.item import ItemCreate, ItemUpdate
|
|
|
|
class CRUDItem(CRUDBase[Item, ItemCreate, ItemUpdate]):
|
|
def create_with_owner(
|
|
self, db: Session, *, obj_in: ItemCreate, owner_id: int
|
|
) -> Item:
|
|
obj_in_data = obj_in.dict()
|
|
db_obj = self.model(**obj_in_data, owner_id=owner_id)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def get_multi_by_owner(
|
|
self, db: Session, *, owner_id: int, skip: int = 0, limit: int = 100
|
|
) -> List[Item]:
|
|
return (
|
|
db.query(self.model)
|
|
.filter(Item.owner_id == owner_id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
item = CRUDItem(Item) |