Automated Action 5e499461d7 Create simple FastAPI Todo application
- Set up project structure with FastAPI
- Create Todo model with SQLAlchemy
- Set up Alembic for database migrations
- Implement CRUD operations for todos
- Add health endpoint
- Update README with setup and usage instructions

generated with BackendIM... (backend.im)
2025-05-13 05:00:15 +00:00

43 lines
1.1 KiB
Python

from typing import List, Optional, Dict, Any, Union
from sqlalchemy.orm import Session
from app.models.todo import Todo
from app.schemas.todo import TodoCreate, TodoUpdate
def get_todo(db: Session, todo_id: int) -> Optional[Todo]:
return db.query(Todo).filter(Todo.id == todo_id).first()
def get_todos(db: Session, skip: int = 0, limit: int = 100) -> List[Todo]:
return db.query(Todo).offset(skip).limit(limit).all()
def create_todo(db: Session, todo: TodoCreate) -> Todo:
db_todo = Todo(**todo.model_dump())
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]:
db_todo = get_todo(db, todo_id)
if db_todo:
update_data = todo.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(db_todo, field, value)
db.add(db_todo)
db.commit()
db.refresh(db_todo)
return db_todo
def delete_todo(db: Session, todo_id: int) -> bool:
db_todo = get_todo(db, todo_id)
if db_todo:
db.delete(db_todo)
db.commit()
return True
return False