
- Set up project structure with FastAPI - Create SQLite database with SQLAlchemy - Implement Alembic for database migrations - Add CRUD operations for items resource - Create health endpoint - Update documentation generated with BackendIM... (backend.im)
53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
from typing import List, Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.crud import item as crud
|
|
from app.schemas.item import Item, ItemCreate, ItemUpdate
|
|
from app.db.session import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Item])
|
|
def read_items(
|
|
skip: int = 0, limit: int = 100, db: Session = Depends(get_db)
|
|
):
|
|
items = 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 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 = 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 = 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}", response_model=Item)
|
|
def delete_item(
|
|
item_id: int, db: Session = Depends(get_db)
|
|
):
|
|
db_item = crud.delete_item(db, item_id=item_id)
|
|
if db_item is None:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return db_item |