
- Created comprehensive task management system with CRUD operations - Implemented SQLAlchemy models and database configuration - Added Alembic migrations for database schema management - Created FastAPI endpoints for task management with proper validation - Added health check endpoint and base URL information - Configured CORS middleware and OpenAPI documentation - Updated README with comprehensive API documentation - Code formatted and linted with Ruff
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
from app.models.task import Task
|
|
from app.schemas.task import TaskCreate, TaskUpdate
|
|
|
|
|
|
class TaskCRUD:
|
|
def create_task(self, db: Session, task: TaskCreate) -> Task:
|
|
db_task = Task(**task.model_dump())
|
|
db.add(db_task)
|
|
db.commit()
|
|
db.refresh(db_task)
|
|
return db_task
|
|
|
|
def get_task(self, db: Session, task_id: int) -> Optional[Task]:
|
|
return db.query(Task).filter(Task.id == task_id).first()
|
|
|
|
def get_tasks(
|
|
self,
|
|
db: Session,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
completed: Optional[bool] = None,
|
|
) -> List[Task]:
|
|
query = db.query(Task)
|
|
if completed is not None:
|
|
query = query.filter(Task.completed == completed)
|
|
return query.offset(skip).limit(limit).all()
|
|
|
|
def update_task(
|
|
self, db: Session, task_id: int, task_update: TaskUpdate
|
|
) -> Optional[Task]:
|
|
db_task = self.get_task(db, task_id)
|
|
if not db_task:
|
|
return None
|
|
|
|
update_data = task_update.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(db_task, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_task)
|
|
return db_task
|
|
|
|
def delete_task(self, db: Session, task_id: int) -> bool:
|
|
db_task = self.get_task(db, task_id)
|
|
if not db_task:
|
|
return False
|
|
|
|
db.delete(db_task)
|
|
db.commit()
|
|
return True
|
|
|
|
def get_tasks_by_priority(self, db: Session, priority: str) -> List[Task]:
|
|
return db.query(Task).filter(Task.priority == priority).all()
|
|
|
|
|
|
task_crud = TaskCRUD()
|