Spaces:
Sleeping
Sleeping
File size: 7,689 Bytes
57a6662 e6cc6c2 57a6662 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | from fastapi import APIRouter, HTTPException, status, Depends
from sqlmodel import Session, select, and_, func
from typing import List
from uuid import UUID
from datetime import datetime
from ..models.user import User
from ..models.project import Project, ProjectCreate, ProjectUpdate, ProjectRead
from ..models.task import Task
from ..database import get_session_dep
from ..utils.deps import get_current_user
router = APIRouter(prefix="/api/{user_id}/projects", tags=["projects"])
@router.get("/", response_model=List[ProjectRead])
def list_projects(
user_id: UUID,
current_user: User = Depends(get_current_user),
session: Session = Depends(get_session_dep)
):
"""List all projects for the authenticated user."""
# Verify that the user_id in the URL matches the authenticated user
if current_user.id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Build the query with user_id filter
query = select(Project).where(Project.user_id == user_id)
# Apply ordering (newest first)
query = query.order_by(Project.created_at.desc())
projects = session.exec(query).all()
return projects
@router.post("", response_model=ProjectRead, status_code=status.HTTP_201_CREATED)
def create_project(
*,
user_id: UUID,
project_data: ProjectCreate,
current_user: User = Depends(get_current_user),
session: Session = Depends(get_session_dep)
):
"""Create a new project for the authenticated user."""
# Verify that the user_id in the URL matches the authenticated user
if current_user.id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
# Create the project
project = Project(
name=project_data.name,
description=project_data.description,
color=project_data.color,
user_id=user_id
)
session.add(project)
session.commit()
session.refresh(project)
return project
@router.get("/{project_id}", response_model=ProjectRead)
def get_project(
*,
user_id: UUID,
project_id: UUID,
current_user: User = Depends(get_current_user),
session: Session = Depends(get_session_dep)
):
"""Get a specific project by ID for the authenticated user."""
# Verify that the user_id in the URL matches the authenticated user
if current_user.id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Fetch the project
project = session.get(Project, project_id)
# Check if project exists and belongs to the user
if not project or project.user_id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
return project
@router.put("/{project_id}", response_model=ProjectRead)
def update_project(
*,
user_id: UUID,
project_id: UUID,
project_data: ProjectUpdate,
current_user: User = Depends(get_current_user),
session: Session = Depends(get_session_dep)
):
"""Update an existing project for the authenticated user."""
# Verify that the user_id in the URL matches the authenticated user
if current_user.id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Fetch the project
project = session.get(Project, project_id)
# Check if project exists and belongs to the user
if not project or project.user_id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Update the project
project_data_dict = project_data.dict(exclude_unset=True)
for key, value in project_data_dict.items():
setattr(project, key, value)
session.add(project)
session.commit()
session.refresh(project)
return project
@router.delete("/{project_id}")
def delete_project(
*,
user_id: UUID,
project_id: UUID,
current_user: User = Depends(get_current_user),
session: Session = Depends(get_session_dep)
):
"""Delete a project for the authenticated user."""
# Verify that the user_id in the URL matches the authenticated user
if current_user.id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Fetch the project
project = session.get(Project, project_id)
# Check if project exists and belongs to the user
if not project or project.user_id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Delete the project
session.delete(project)
session.commit()
return {"message": "Project deleted successfully"}
@router.get("/{project_id}/tasks", response_model=List[Task])
def list_project_tasks(
*,
user_id: UUID,
project_id: UUID,
current_user: User = Depends(get_current_user),
session: Session = Depends(get_session_dep)
):
"""List all tasks for a specific project."""
# Verify that the user_id in the URL matches the authenticated user
if current_user.id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Fetch the project
project = session.get(Project, project_id)
# Check if project exists and belongs to the user
if not project or project.user_id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Build the query with project_id filter
query = select(Task).where(Task.project_id == project_id)
# Apply ordering (newest first)
query = query.order_by(Task.created_at.desc())
tasks = session.exec(query).all()
return tasks
@router.get("/{project_id}/progress")
def get_project_progress(
*,
user_id: UUID,
project_id: UUID,
current_user: User = Depends(get_current_user),
session: Session = Depends(get_session_dep)
):
"""Get progress statistics for a specific project."""
# Verify that the user_id in the URL matches the authenticated user
if current_user.id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Fetch the project
project = session.get(Project, project_id)
# Check if project exists and belongs to the user
if not project or project.user_id != user_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Get task counts
total_tasks_query = select(func.count()).where(Task.project_id == project_id)
completed_tasks_query = select(func.count()).where(and_(Task.project_id == project_id, Task.completed == True))
total_tasks = session.exec(total_tasks_query).first()
completed_tasks = session.exec(completed_tasks_query).first()
# Calculate progress
progress = 0
if total_tasks > 0:
progress = round((completed_tasks / total_tasks) * 100, 2)
return {
"total_tasks": total_tasks,
"completed_tasks": completed_tasks,
"pending_tasks": total_tasks - completed_tasks,
"progress": progress
} |