
- Add SQLAlchemy models for Todo with timestamps - Create Pydantic schemas for request/response validation - Implement CRUD operations for Todo management - Add REST API endpoints for todo operations (GET, POST, PUT, DELETE) - Configure SQLite database with proper connection settings - Set up Alembic migrations for database schema management - Add comprehensive API documentation and health check endpoint - Enable CORS for all origins - Include proper error handling and HTTP status codes - Update README with complete setup and usage instructions
88 lines
2.0 KiB
Python
88 lines
2.0 KiB
Python
from typing import List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud import todo
|
|
from app.db.session import get_db
|
|
from app.schemas.todo import TodoCreate, TodoResponse, TodoUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[TodoResponse])
|
|
def read_todos(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Retrieve todos with pagination.
|
|
"""
|
|
todos = todo.get_multi(db, skip=skip, limit=limit)
|
|
return todos
|
|
|
|
|
|
@router.post("/", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_in: TodoCreate,
|
|
):
|
|
"""
|
|
Create new todo.
|
|
"""
|
|
return todo.create(db=db, obj_in=todo_in)
|
|
|
|
|
|
@router.get("/{todo_id}", response_model=TodoResponse)
|
|
def read_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_id: int,
|
|
):
|
|
"""
|
|
Get todo by ID.
|
|
"""
|
|
todo_obj = todo.get(db=db, todo_id=todo_id)
|
|
if not todo_obj:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found"
|
|
)
|
|
return todo_obj
|
|
|
|
|
|
@router.put("/{todo_id}", response_model=TodoResponse)
|
|
def update_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_id: int,
|
|
todo_in: TodoUpdate,
|
|
):
|
|
"""
|
|
Update a todo.
|
|
"""
|
|
todo_obj = todo.get(db=db, todo_id=todo_id)
|
|
if not todo_obj:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found"
|
|
)
|
|
return todo.update(db=db, db_obj=todo_obj, obj_in=todo_in)
|
|
|
|
|
|
@router.delete("/{todo_id}", response_model=TodoResponse)
|
|
def delete_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_id: int,
|
|
):
|
|
"""
|
|
Delete a todo.
|
|
"""
|
|
todo_obj = todo.remove(db=db, todo_id=todo_id)
|
|
if not todo_obj:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found"
|
|
)
|
|
return todo_obj
|