Automated Action 6d123e4b89 Create simple todo app with FastAPI and SQLite
- Set up project structure
- Create database models with SQLAlchemy
- Add Alembic migrations
- Implement CRUD API endpoints
- Update README with documentation

generated with BackendIM... (backend.im)
2025-05-14 01:42:06 +00:00

74 lines
1.9 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.db.database import get_db
from app.crud import todo as todo_crud
from app.schemas.todo import Todo, TodoCreate, TodoUpdate
from typing import List
router = APIRouter(
prefix="/todos",
tags=["todos"],
responses={404: {"description": "Not found"}}
)
@router.get("/", response_model=List[Todo])
def read_todos(
skip: int = 0,
limit: int = 100,
db: Session = Depends(get_db)
):
"""
Get all todos with pagination.
"""
todos = todo_crud.get_todos(db, skip=skip, limit=limit)
return todos
@router.get("/{todo_id}", response_model=Todo)
def read_todo(
todo_id: int,
db: Session = Depends(get_db)
):
"""
Get a todo by ID.
"""
db_todo = todo_crud.get_todo_by_id(db, todo_id=todo_id)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo
@router.post("/", response_model=Todo, status_code=status.HTTP_201_CREATED)
def create_todo(
todo: TodoCreate,
db: Session = Depends(get_db)
):
"""
Create a new todo.
"""
return todo_crud.create_todo(db=db, todo=todo)
@router.put("/{todo_id}", response_model=Todo)
def update_todo(
todo_id: int,
todo: TodoUpdate,
db: Session = Depends(get_db)
):
"""
Update a todo by ID.
"""
db_todo = todo_crud.update_todo(db=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("/{todo_id}", response_model=Todo)
def delete_todo(
todo_id: int,
db: Session = Depends(get_db)
):
"""
Delete a todo by ID.
"""
db_todo = todo_crud.delete_todo(db=db, todo_id=todo_id)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo