
- Add SQLAlchemy models for Todo with timestamps - Create Pydantic schemas for request/response validation - Implement CRUD operations for Todo management - Add REST API endpoints for todo operations (GET, POST, PUT, DELETE) - Configure SQLite database with proper connection settings - Set up Alembic migrations for database schema management - Add comprehensive API documentation and health check endpoint - Enable CORS for all origins - Include proper error handling and HTTP status codes - Update README with complete setup and usage instructions
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
from typing import List, Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.todo import Todo
|
|
from app.schemas.todo import TodoCreate, TodoUpdate
|
|
|
|
|
|
class CRUDTodo:
|
|
"""CRUD operations for Todo model."""
|
|
|
|
def get(self, 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_multi(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[Todo]:
|
|
"""Get multiple todos with pagination."""
|
|
return db.query(Todo).offset(skip).limit(limit).all()
|
|
|
|
def create(self, db: Session, *, obj_in: TodoCreate) -> Todo:
|
|
"""Create a new todo."""
|
|
db_obj = Todo(
|
|
title=obj_in.title,
|
|
description=obj_in.description,
|
|
completed=obj_in.completed,
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def update(self, db: Session, *, db_obj: Todo, obj_in: TodoUpdate) -> Todo:
|
|
"""Update a todo."""
|
|
update_data = obj_in.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(db_obj, field, value)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def remove(self, db: Session, *, todo_id: int) -> Optional[Todo]:
|
|
"""Delete a todo."""
|
|
obj = db.query(Todo).get(todo_id)
|
|
if obj:
|
|
db.delete(obj)
|
|
db.commit()
|
|
return obj
|
|
|
|
|
|
todo = CRUDTodo()
|