
- Set up FastAPI application with CORS support - Created SQLite database configuration with absolute paths - Implemented Todo model with SQLAlchemy - Added full CRUD operations for todos - Created API endpoints with proper error handling - Set up Alembic for database migrations - Added health check and base URL endpoints - Updated README with comprehensive documentation - Configured project structure following best practices Features: - Complete Todo CRUD API - SQLite database with proper path configuration - Database migrations with Alembic - API documentation at /docs and /redoc - Health check endpoint at /health - CORS enabled for all origins - Proper error handling and validation
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
from app.models.todo import Todo
|
|
from app.models.schemas import TodoCreate, TodoUpdate
|
|
|
|
|
|
def get_todo(db: Session, todo_id: int) -> Optional[Todo]:
|
|
"""Get a single todo by ID"""
|
|
return db.query(Todo).filter(Todo.id == todo_id).first()
|
|
|
|
|
|
def get_todos(db: Session, skip: int = 0, limit: int = 100) -> List[Todo]:
|
|
"""Get all todos with pagination"""
|
|
return db.query(Todo).offset(skip).limit(limit).all()
|
|
|
|
|
|
def create_todo(db: Session, todo: TodoCreate) -> Todo:
|
|
"""Create a new todo"""
|
|
db_todo = 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_update: TodoUpdate) -> Optional[Todo]:
|
|
"""Update an existing todo"""
|
|
db_todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
|
if db_todo:
|
|
update_data = todo_update.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(db_todo, field, value)
|
|
db.commit()
|
|
db.refresh(db_todo)
|
|
return db_todo
|
|
|
|
|
|
def delete_todo(db: Session, todo_id: int) -> bool:
|
|
"""Delete a todo"""
|
|
db_todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
|
if db_todo:
|
|
db.delete(db_todo)
|
|
db.commit()
|
|
return True
|
|
return False |