
- Set up project structure with FastAPI and SQLite - Created Todo model with SQLAlchemy ORM - Added CRUD operations for todos - Implemented API endpoints for Todo operations - Added health check endpoint - Added Alembic for database migrations - Updated README with documentation generated with BackendIM... (backend.im)
58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
|
|
from app.database.base import get_db
|
|
from app.database import crud
|
|
from app.models.schemas import TodoCreate, TodoResponse, TodoUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/todos/", response_model=List[TodoResponse])
|
|
def read_todos(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Retrieve all todos with pagination support.
|
|
"""
|
|
todos = crud.get_todos(db, skip=skip, limit=limit)
|
|
return todos
|
|
|
|
@router.get("/todos/{todo_id}", response_model=TodoResponse)
|
|
def read_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Retrieve a specific todo by its ID.
|
|
"""
|
|
db_todo = crud.get_todo(db, todo_id=todo_id)
|
|
if db_todo is None:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
return db_todo
|
|
|
|
@router.post("/todos/", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(todo: TodoCreate, db: Session = Depends(get_db)):
|
|
"""
|
|
Create a new todo.
|
|
"""
|
|
return crud.create_todo(db=db, todo=todo)
|
|
|
|
@router.put("/todos/{todo_id}", response_model=TodoResponse)
|
|
def update_todo(todo_id: int, todo: TodoUpdate, db: Session = Depends(get_db)):
|
|
"""
|
|
Update a todo by its ID.
|
|
"""
|
|
db_todo = crud.update_todo(db, todo_id=todo_id, todo=todo)
|
|
if db_todo is None:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
return db_todo
|
|
|
|
@router.delete("/todos/{todo_id}", response_model=TodoResponse)
|
|
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Delete a todo by its ID.
|
|
"""
|
|
db_todo = crud.delete_todo(db, todo_id=todo_id)
|
|
if db_todo is None:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
return db_todo |