Automated Action e1ed70e281 Create FastAPI Todo API application
- Set up project structure and requirements
- Create database models and connection
- Set up Alembic for migrations
- Implement CRUD operations for todos
- Build RESTful API endpoints
- Update README with project documentation

generated with BackendIM... (backend.im)
2025-05-11 19:49:50 +00:00

55 lines
1.3 KiB
Python

from sqlalchemy.orm import Session
from typing import List, Optional
from models.todo import Todo
from schemas.todo import TodoCreate, TodoUpdate
def get_todos(db: Session, skip: int = 0, limit: int = 100) -> List[Todo]:
"""
Get all todos with pagination
"""
return db.query(Todo).offset(skip).limit(limit).all()
def get_todo(db: Session, todo_id: int) -> Optional[Todo]:
"""
Get a specific todo by ID
"""
return db.query(Todo).filter(Todo.id == todo_id).first()
def create_todo(db: Session, todo: TodoCreate) -> Todo:
"""
Create a new todo
"""
db_todo = Todo(**todo.dict())
db.add(db_todo)
db.commit()
db.refresh(db_todo)
return db_todo
def update_todo(db: Session, todo_id: int, todo: TodoUpdate) -> Optional[Todo]:
"""
Update an existing todo
"""
db_todo = get_todo(db, todo_id)
if db_todo:
update_data = todo.dict(exclude_unset=True)
for key, value in update_data.items():
setattr(db_todo, key, value)
db.commit()
db.refresh(db_todo)
return db_todo
def delete_todo(db: Session, todo_id: int) -> bool:
"""
Delete a todo
"""
db_todo = get_todo(db, todo_id)
if db_todo:
db.delete(db_todo)
db.commit()
return True
return False