
- Set up project structure with FastAPI and SQLite - Created Todo model with SQLAlchemy ORM - Added CRUD operations for todos - Implemented API endpoints for Todo operations - Added health check endpoint - Added Alembic for database migrations - Updated README with documentation generated with BackendIM... (backend.im)
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
from sqlalchemy.orm import Session
|
|
from app.models.todo import Todo
|
|
from app.models.schemas import TodoCreate, TodoUpdate
|
|
|
|
# Todo CRUD operations
|
|
def get_todos(db: Session, skip: int = 0, limit: int = 100):
|
|
return db.query(Todo).offset(skip).limit(limit).all()
|
|
|
|
def get_todo(db: Session, todo_id: int):
|
|
return db.query(Todo).filter(Todo.id == todo_id).first()
|
|
|
|
def create_todo(db: Session, todo: TodoCreate):
|
|
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):
|
|
db_todo = get_todo(db, todo_id)
|
|
if db_todo:
|
|
update_data = todo.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):
|
|
db_todo = get_todo(db, todo_id)
|
|
if db_todo:
|
|
db.delete(db_todo)
|
|
db.commit()
|
|
return db_todo |