
- Created FastAPI application with CRUD endpoints for todos - Set up SQLAlchemy with SQLite database - Created database models and Pydantic schemas - Added Alembic for database migrations - Added health check endpoint - Updated README with project information generated with BackendIM... (backend.im)
40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from sqlalchemy.orm import Session
|
|
from . import models, schemas
|
|
|
|
def get_todo(db: Session, todo_id: int):
|
|
return db.query(models.Todo).filter(models.Todo.id == todo_id).first()
|
|
|
|
def get_todos(db: Session, skip: int = 0, limit: int = 100):
|
|
return db.query(models.Todo).offset(skip).limit(limit).all()
|
|
|
|
def create_todo(db: Session, todo: schemas.TodoCreate):
|
|
db_todo = models.Todo(
|
|
title=todo.title,
|
|
description=todo.description,
|
|
completed=todo.completed
|
|
)
|
|
db.add(db_todo)
|
|
db.commit()
|
|
db.refresh(db_todo)
|
|
return db_todo
|
|
|
|
def update_todo(db: Session, todo_id: int, todo: schemas.TodoUpdate):
|
|
db_todo = get_todo(db, todo_id)
|
|
|
|
# Update fields if they are provided
|
|
if todo.title is not None:
|
|
db_todo.title = todo.title
|
|
if todo.description is not None:
|
|
db_todo.description = todo.description
|
|
if todo.completed is not None:
|
|
db_todo.completed = todo.completed
|
|
|
|
db.commit()
|
|
db.refresh(db_todo)
|
|
return db_todo
|
|
|
|
def delete_todo(db: Session, todo_id: int):
|
|
db_todo = get_todo(db, todo_id)
|
|
db.delete(db_todo)
|
|
db.commit()
|
|
return db_todo |