
- Set up project structure and dependencies - Create task model and schema - Implement Alembic migrations - Add CRUD API endpoints for task management - Add health endpoint with database connectivity check - Add comprehensive error handling - Add tests for API endpoints - Update README with API documentation
108 lines
2.6 KiB
Python
108 lines
2.6 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import schemas, crud
|
|
from app.db.session import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[schemas.Task])
|
|
def read_tasks(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
completed: Optional[bool] = None,
|
|
priority: Optional[int] = None,
|
|
search: Optional[str] = None,
|
|
) -> Any:
|
|
"""
|
|
Retrieve tasks with optional filtering.
|
|
"""
|
|
if completed is not None:
|
|
tasks = crud.task.get_multi_by_completed(
|
|
db, completed=completed, skip=skip, limit=limit
|
|
)
|
|
elif priority is not None:
|
|
tasks = crud.task.get_multi_by_priority(
|
|
db, priority=priority, skip=skip, limit=limit
|
|
)
|
|
elif search is not None:
|
|
tasks = crud.task.search_by_title(
|
|
db, title=search, skip=skip, limit=limit
|
|
)
|
|
else:
|
|
tasks = crud.task.get_multi(db, skip=skip, limit=limit)
|
|
return tasks
|
|
|
|
|
|
@router.post("/", response_model=schemas.Task, status_code=status.HTTP_201_CREATED)
|
|
def create_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_in: schemas.TaskCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new task.
|
|
"""
|
|
task = crud.task.create(db=db, obj_in=task_in)
|
|
return task
|
|
|
|
|
|
@router.get("/{task_id}", response_model=schemas.Task)
|
|
def read_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
) -> Any:
|
|
"""
|
|
Get task by ID.
|
|
"""
|
|
task = crud.task.get(db=db, id=task_id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
return task
|
|
|
|
|
|
@router.put("/{task_id}", response_model=schemas.Task)
|
|
def update_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
task_in: schemas.TaskUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update a task.
|
|
"""
|
|
task = crud.task.get(db=db, id=task_id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
task = crud.task.update(db=db, db_obj=task, obj_in=task_in)
|
|
return task
|
|
|
|
|
|
@router.delete("/{task_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete a task.
|
|
"""
|
|
task = crud.task.get(db=db, id=task_id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
crud.task.remove(db=db, id=task_id)
|
|
return None |