2025-05-30 17:39:08 +00:00

141 lines
4.2 KiB
Python

"""
Task management endpoints
"""
from datetime import datetime
from typing import Any, List, Optional
from app import crud, models, schemas
from app.api import deps
from app.models.task import TaskPriority, TaskStatus
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
router = APIRouter()
@router.post("", response_model=schemas.Task)
def create_task(
*,
db: Session = Depends(deps.get_db),
task_in: schemas.TaskCreate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Create a new task
"""
task = crud.task.create_with_owner(db=db, obj_in=task_in, owner_id=current_user.id)
return task
@router.get("", response_model=List[schemas.Task])
def read_tasks(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
status: Optional[TaskStatus] = None,
priority: Optional[TaskPriority] = None,
search: Optional[str] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Retrieve tasks with filtering options
"""
if status:
tasks = crud.task.get_multi_by_owner_and_status(
db=db, owner_id=current_user.id, status=status, skip=skip, limit=limit
)
elif priority:
tasks = crud.task.get_multi_by_owner_and_priority(
db=db, owner_id=current_user.id, priority=priority, skip=skip, limit=limit
)
elif search:
tasks = crud.task.get_multi_by_owner_and_search(
db=db, owner_id=current_user.id, search=search, skip=skip, limit=limit
)
elif start_date and end_date:
tasks = crud.task.get_multi_by_owner_and_due_date_range(
db=db, owner_id=current_user.id, start_date=start_date, end_date=end_date,
skip=skip, limit=limit
)
else:
tasks = crud.task.get_multi_by_owner(
db=db, owner_id=current_user.id, skip=skip, limit=limit
)
return tasks
@router.get("/{id}", response_model=schemas.Task)
def read_task(
*,
db: Session = Depends(deps.get_db),
id: int,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get task by ID
"""
task = crud.task.get_by_id_and_owner(db=db, id=id, owner_id=current_user.id)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
return task
@router.put("/{id}", response_model=schemas.Task)
def update_task(
*,
db: Session = Depends(deps.get_db),
id: int,
task_in: schemas.TaskUpdate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update a task
"""
task = crud.task.get_by_id_and_owner(db=db, id=id, owner_id=current_user.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.patch("/{id}/status", response_model=schemas.Task)
def update_task_status(
*,
db: Session = Depends(deps.get_db),
id: int,
status: TaskStatus,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update a task status
"""
task = crud.task.get_by_id_and_owner(db=db, id=id, owner_id=current_user.id)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
task = crud.task.update_status(db=db, db_obj=task, status=status)
return task
@router.delete("/{id}", response_model=schemas.Task)
def delete_task(
*,
db: Session = Depends(deps.get_db),
id: int,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Delete a task (soft delete)
"""
task = crud.task.get_by_id_and_owner(db=db, id=id, owner_id=current_user.id)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
task = crud.task.soft_delete(db=db, db_obj=task)
return task