|
| 1 | +from fastapi import APIRouter, Depends, HTTPException, Query |
| 2 | +from sqlalchemy.orm import Session |
| 3 | +from datetime import date |
| 4 | +from typing import List, Optional |
| 5 | + |
| 6 | +from src.utils.utils import get_db |
| 7 | +from src.models.user_model import User |
| 8 | +from src.models.project_model import Project |
| 9 | +from src.models.task_model import Task |
| 10 | +from src.schemas.projects.projects_schema import ProjectResponse |
| 11 | +from src.schemas.tasks.tasks_schema import TaskResponse |
| 12 | +from src.utils.security import get_current_user |
| 13 | + |
| 14 | +router = APIRouter( |
| 15 | + prefix="/filters", |
| 16 | + tags=["Filters and Relationships"] |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +@router.get("/tasks", response_model=List[TaskResponse]) |
| 21 | +def filter_tasks( |
| 22 | + completed: Optional[bool] = Query(None, description="Filter by completion status (true/false)"), |
| 23 | + reminder: Optional[bool] = Query(None, description="Filter by reminder flag (true/false)"), |
| 24 | + due_before: Optional[date] = Query(None, description="Tasks due before this date"), |
| 25 | + db: Session = Depends(get_db), |
| 26 | + current_user: User = Depends(get_current_user) |
| 27 | +): |
| 28 | + """ |
| 29 | + Return tasks filtered by completion status, reminder flag, and due date, |
| 30 | + only for tasks belonging to projects owned by the authenticated user. |
| 31 | +
|
| 32 | + Args: |
| 33 | + completed (Optional[bool]): Filter by completion status. |
| 34 | + reminder (Optional[bool]): Filter by reminder flag. |
| 35 | + due_before (Optional[date]): Filter tasks due before this date. |
| 36 | + db (Session): Database session. |
| 37 | + current_user (User): Authenticated user. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + List[Task]: List of filtered tasks. |
| 41 | +
|
| 42 | + Raises: |
| 43 | + HTTPException: If no tasks are found. |
| 44 | + """ |
| 45 | + |
| 46 | + query = ( |
| 47 | + db.query(Task) |
| 48 | + .join(Project, Project.id == Task.project_id) |
| 49 | + .filter(Project.user_id == current_user.id) |
| 50 | + ) |
| 51 | + |
| 52 | + if completed is not None: |
| 53 | + query = query.filter(Task.completed == completed) |
| 54 | + if reminder is not None: |
| 55 | + query = query.filter(Task.reminder == reminder) |
| 56 | + if due_before: |
| 57 | + query = query.filter(Task.due_date <= due_before) |
| 58 | + |
| 59 | + return query.all() |
| 60 | + |
| 61 | + |
| 62 | +@router.get("/user/projects", response_model=List[ProjectResponse]) |
| 63 | +def get_projects_by_user( |
| 64 | + db: Session = Depends(get_db), |
| 65 | + current_user: User = Depends(get_current_user) |
| 66 | +): |
| 67 | + """ |
| 68 | + Return all projects belonging to the authenticated user. |
| 69 | +
|
| 70 | + Args: |
| 71 | + db (Session): Database session. |
| 72 | + current_user (User): Authenticated user. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + List[Project]: List of projects owned by the user. |
| 76 | +
|
| 77 | + Raises: |
| 78 | + HTTPException: If no projects are found. |
| 79 | + """ |
| 80 | + |
| 81 | + projects = db.query(Project).filter(Project.user_id == current_user.id).all() |
| 82 | + if not projects: |
| 83 | + raise HTTPException(status_code=404, detail="No projects found.") |
| 84 | + return projects |
| 85 | + |
| 86 | + |
| 87 | +@router.get("/project/{project_id}/tasks", response_model=List[TaskResponse]) |
| 88 | +def get_tasks_by_project( |
| 89 | + project_id: int, |
| 90 | + db: Session = Depends(get_db), |
| 91 | + current_user: User = Depends(get_current_user) |
| 92 | +): |
| 93 | + """ |
| 94 | + Return all tasks for a specific project, |
| 95 | + only if the project belongs to the authenticated user. |
| 96 | +
|
| 97 | + Args: |
| 98 | + project_id (int): ID of the project. |
| 99 | + db (Session): Database session. |
| 100 | + current_user (User): Authenticated user. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + List[Task]: List of tasks for the specified project. |
| 104 | +
|
| 105 | + Raises: |
| 106 | + HTTPException: If the project is not found or not owned by the user. |
| 107 | + """ |
| 108 | + |
| 109 | + project = db.query(Project).filter(Project.id == project_id, Project.user_id == current_user.id).first() |
| 110 | + if not project: |
| 111 | + raise HTTPException(status_code=404, detail="Project not found or not owned by user") |
| 112 | + |
| 113 | + return db.query(Task).filter(Task.project_id == project.id).all() |
| 114 | + |
| 115 | + |
| 116 | +@router.get("/tasks/advanced", response_model=List[TaskResponse]) |
| 117 | +def advanced_filter_tasks( |
| 118 | + completed: Optional[bool] = Query(None, description="Filter by completion status"), |
| 119 | + reminder: Optional[bool] = Query(None, description="Filter by reminder flag"), |
| 120 | + project_id: Optional[int] = Query(None, description="Filter by project ID"), |
| 121 | + due_before: Optional[date] = Query(None, description="Tasks due before this date"), |
| 122 | + db: Session = Depends(get_db), |
| 123 | + current_user: User = Depends(get_current_user) |
| 124 | +): |
| 125 | + """ |
| 126 | + Advanced filtering for tasks related to the authenticated user. |
| 127 | + Allows combining multiple search parameters. |
| 128 | +
|
| 129 | + Args: |
| 130 | + completed (Optional[bool]): Filter by completion status. |
| 131 | + reminder (Optional[bool]): Filter by reminder flag. |
| 132 | + project_id (Optional[int]): Filter by project ID. |
| 133 | + due_before (Optional[date]): Filter tasks due before this date. |
| 134 | + db (Session): Database session. |
| 135 | + current_user (User): Authenticated user. |
| 136 | +
|
| 137 | + Returns: |
| 138 | + List[Task]: List of filtered tasks. |
| 139 | +
|
| 140 | + Raises: |
| 141 | + HTTPException: If no tasks are found. |
| 142 | + """ |
| 143 | + |
| 144 | + query = ( |
| 145 | + db.query(Task) |
| 146 | + .join(Project, Project.id == Task.project_id) |
| 147 | + .filter(Project.user_id == current_user.id) |
| 148 | + ) |
| 149 | + |
| 150 | + if completed is not None: |
| 151 | + query = query.filter(Task.completed == completed) |
| 152 | + if reminder is not None: |
| 153 | + query = query.filter(Task.reminder == reminder) |
| 154 | + if project_id is not None: |
| 155 | + query = query.filter(Task.project_id == project_id) |
| 156 | + if due_before: |
| 157 | + query = query.filter(Task.due_date <= due_before) |
| 158 | + |
| 159 | + return query.all() |
0 commit comments