73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, Path, Query, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
from app.models.task import TaskPriority, TaskStatus
|
|
from app.schemas.responses import PaginatedResponse
|
|
from app.schemas.task import Task, TaskCreate, TaskUpdate
|
|
from app.services.task import TaskService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=PaginatedResponse[Task])
|
|
def get_tasks(
|
|
skip: int = Query(0, ge=0, description="Number of tasks to skip"),
|
|
limit: int = Query(100, gt=0, le=1000, description="Maximum number of tasks to return"),
|
|
status: Optional[TaskStatus] = Query(None, description="Filter by task status"),
|
|
priority: Optional[TaskPriority] = Query(None, description="Filter by task priority"),
|
|
completed: Optional[bool] = Query(None, description="Filter by completion status"),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Get a list of tasks with optional filtering and pagination.
|
|
"""
|
|
tasks = TaskService.get_tasks(
|
|
db, skip=skip, limit=limit, status=status, priority=priority, completed=completed
|
|
)
|
|
|
|
total = TaskService.count_tasks(
|
|
db, status=status, priority=priority, completed=completed
|
|
)
|
|
|
|
return PaginatedResponse.create(tasks, total, skip, limit)
|
|
|
|
|
|
@router.post("/", response_model=Task, status_code=status.HTTP_201_CREATED)
|
|
def create_task(task_data: TaskCreate, db: Session = Depends(get_db)):
|
|
"""Create a new task.
|
|
"""
|
|
return TaskService.create_task(db, task_data)
|
|
|
|
|
|
@router.get("/{task_id}", response_model=Task)
|
|
def get_task(
|
|
task_id: int = Path(..., gt=0, description="The ID of the task"),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Get a specific task by ID.
|
|
"""
|
|
return TaskService.get_task(db, task_id)
|
|
|
|
|
|
@router.put("/{task_id}", response_model=Task)
|
|
def update_task(
|
|
task_data: TaskUpdate,
|
|
task_id: int = Path(..., gt=0, description="The ID of the task"),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Update a specific task by ID.
|
|
"""
|
|
return TaskService.update_task(db, task_id, task_data)
|
|
|
|
|
|
@router.delete("/{task_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_task(
|
|
task_id: int = Path(..., gt=0, description="The ID of the task"),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Delete a specific task by ID.
|
|
"""
|
|
TaskService.delete_task(db, task_id)
|
|
return None |