
- Set up project structure with FastAPI app - Implement items CRUD API endpoints - Configure SQLite database with SQLAlchemy - Set up Alembic migrations - Add health check endpoint - Enable CORS middleware - Create README with documentation
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from sqlalchemy.orm import Session
|
|
from app.models.item import Item
|
|
from app.schemas.item import ItemCreate, ItemUpdate
|
|
from typing import List, Optional
|
|
|
|
def get_items(db: Session, skip: int = 0, limit: int = 100) -> List[Item]:
|
|
return db.query(Item).offset(skip).limit(limit).all()
|
|
|
|
def get_item(db: Session, item_id: int) -> Optional[Item]:
|
|
return db.query(Item).filter(Item.id == item_id).first()
|
|
|
|
def create_item(db: Session, item: ItemCreate) -> Item:
|
|
db_item = Item(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
def update_item(db: Session, item_id: int, item: ItemUpdate) -> Optional[Item]:
|
|
db_item = get_item(db, item_id)
|
|
if db_item is None:
|
|
return None
|
|
|
|
update_data = item.model_dump(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(db_item, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
def delete_item(db: Session, item_id: int) -> bool:
|
|
db_item = get_item(db, item_id)
|
|
if db_item is None:
|
|
return False
|
|
|
|
db.delete(db_item)
|
|
db.commit()
|
|
return True |