
- Created CRUD operations for todos - Added database models and SQLAlchemy integration - Set up Alembic for database migrations - Added health endpoint - Updated README with installation and usage instructions generated with BackendIM... (backend.im)
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
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(**todo.dict())
|
|
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)
|
|
if db_todo:
|
|
todo_data = todo.dict(exclude_unset=True)
|
|
for key, value in todo_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 True
|
|
return False |