
- 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.5 KiB
Python
39 lines
1.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from app.db.session import get_db
|
|
from app.schemas.item import Item, ItemCreate, ItemUpdate
|
|
from app.crud import item as item_crud
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/", response_model=List[Item])
|
|
def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
items = item_crud.get_items(db, skip=skip, limit=limit)
|
|
return items
|
|
|
|
@router.post("/", response_model=Item, status_code=status.HTTP_201_CREATED)
|
|
def create_item(item: ItemCreate, db: Session = Depends(get_db)):
|
|
return item_crud.create_item(db=db, item=item)
|
|
|
|
@router.get("/{item_id}", response_model=Item)
|
|
def read_item(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = item_crud.get_item(db, item_id=item_id)
|
|
if db_item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return db_item
|
|
|
|
@router.put("/{item_id}", response_model=Item)
|
|
def update_item(item_id: int, item: ItemUpdate, db: Session = Depends(get_db)):
|
|
db_item = item_crud.update_item(db, item_id=item_id, item=item)
|
|
if db_item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return db_item
|
|
|
|
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_item(item_id: int, db: Session = Depends(get_db)):
|
|
success = item_crud.delete_item(db, item_id=item_id)
|
|
if not success:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return None |