Automated Action af063c97fc Create Todo app with FastAPI and SQLite
- Set up project structure with FastAPI and SQLAlchemy
- Create Todo model with CRUD operations
- Implement database migrations with Alembic
- Add health endpoint and API documentation
- Create comprehensive README

generated with BackendIM... (backend.im)
2025-05-13 17:36:33 +00:00

47 lines
1.2 KiB
Python

from sqlalchemy.orm import Session
from fastapi import HTTPException, status
from typing import List, Optional
from app.db.models import Todo
from app.schemas.todo import TodoCreate, TodoUpdate
def get_todos(db: Session, skip: int = 0, limit: int = 100) -> List[Todo]:
return db.query(Todo).offset(skip).limit(limit).all()
def get_todo(db: Session, todo_id: int) -> Todo:
todo = db.query(Todo).filter(Todo.id == todo_id).first()
if todo is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Todo with ID {todo_id} not found"
)
return todo
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_update: TodoUpdate) -> Todo:
db_todo = get_todo(db, todo_id)
update_data = todo_update.model_dump(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) -> None:
db_todo = get_todo(db, todo_id)
db.delete(db_todo)
db.commit()
return None