
- Set up FastAPI application with CORS and authentication - Implement user registration and login with JWT tokens - Create SQLAlchemy models for users and items - Add CRUD endpoints for item management - Configure Alembic for database migrations - Add health check endpoint - Include comprehensive API documentation - Set up proper project structure with routers and schemas
76 lines
3.0 KiB
Python
76 lines
3.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from app.db.session import get_db
|
|
from app.models.item import Item
|
|
from app.models.user import User
|
|
from app.schemas.item import ItemCreate, ItemUpdate, Item as ItemSchema
|
|
from app.routers.auth import get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
def get_items(db: Session, user_id: int, skip: int = 0, limit: int = 100):
|
|
return db.query(Item).filter(Item.owner_id == user_id).offset(skip).limit(limit).all()
|
|
|
|
def get_item(db: Session, item_id: int, user_id: int):
|
|
return db.query(Item).filter(Item.id == item_id, Item.owner_id == user_id).first()
|
|
|
|
def create_item(db: Session, item: ItemCreate, user_id: int):
|
|
db_item = Item(**item.dict(), owner_id=user_id)
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
def update_item(db: Session, item_id: int, item_update: ItemUpdate, user_id: int):
|
|
db_item = db.query(Item).filter(Item.id == item_id, Item.owner_id == user_id).first()
|
|
if not db_item:
|
|
return None
|
|
|
|
update_data = item_update.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(db_item, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
def delete_item(db: Session, item_id: int, user_id: int):
|
|
db_item = db.query(Item).filter(Item.id == item_id, Item.owner_id == user_id).first()
|
|
if not db_item:
|
|
return None
|
|
|
|
db.delete(db_item)
|
|
db.commit()
|
|
return db_item
|
|
|
|
@router.get("/", response_model=List[ItemSchema])
|
|
def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
|
items = get_items(db, current_user.id, skip=skip, limit=limit)
|
|
return items
|
|
|
|
@router.post("/", response_model=ItemSchema)
|
|
def create_item_endpoint(item: ItemCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
|
return create_item(db, item, current_user.id)
|
|
|
|
@router.get("/{item_id}", response_model=ItemSchema)
|
|
def read_item(item_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
|
db_item = get_item(db, item_id, current_user.id)
|
|
if db_item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return db_item
|
|
|
|
@router.put("/{item_id}", response_model=ItemSchema)
|
|
def update_item_endpoint(item_id: int, item_update: ItemUpdate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
|
db_item = update_item(db, item_id, item_update, current_user.id)
|
|
if db_item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return db_item
|
|
|
|
@router.delete("/{item_id}", response_model=ItemSchema)
|
|
def delete_item_endpoint(item_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
|
db_item = delete_item(db, item_id, current_user.id)
|
|
if db_item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return db_item |