Spaces:
Sleeping
Sleeping
'code'
Browse files- src/routers/stats.py +12 -10
src/routers/stats.py
CHANGED
|
@@ -2,22 +2,24 @@
|
|
| 2 |
Task statistics router
|
| 3 |
Provides endpoints for calculating and retrieving task statistics
|
| 4 |
"""
|
| 5 |
-
from fastapi import APIRouter, Depends, HTTPException
|
| 6 |
-
from
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
from
|
| 11 |
-
from
|
|
|
|
|
|
|
| 12 |
|
| 13 |
router = APIRouter(prefix="/api", tags=["stats"])
|
| 14 |
|
| 15 |
|
| 16 |
@router.get("/{user_id}/tasks/stats")
|
| 17 |
async def get_task_stats(
|
| 18 |
-
user_id:
|
| 19 |
current_user: User = Depends(get_current_user),
|
| 20 |
-
|
| 21 |
):
|
| 22 |
"""
|
| 23 |
Get task statistics for a user.
|
|
@@ -25,10 +27,10 @@ async def get_task_stats(
|
|
| 25 |
"""
|
| 26 |
# Verify user is requesting their own stats
|
| 27 |
if current_user.id != user_id:
|
| 28 |
-
raise HTTPException(status_code=
|
| 29 |
|
| 30 |
# Get all tasks for user
|
| 31 |
-
tasks =
|
| 32 |
|
| 33 |
total = len(tasks)
|
| 34 |
completed = len([t for t in tasks if t.completed])
|
|
|
|
| 2 |
Task statistics router
|
| 3 |
Provides endpoints for calculating and retrieving task statistics
|
| 4 |
"""
|
| 5 |
+
from fastapi import APIRouter, Depends, HTTPException, status
|
| 6 |
+
from sqlmodel import Session, select, func
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
+
from uuid import UUID
|
| 9 |
+
|
| 10 |
+
from ..database import get_session_dep
|
| 11 |
+
from ..models.task import Task
|
| 12 |
+
from ..models.user import User
|
| 13 |
+
from ..utils.deps import get_current_user
|
| 14 |
|
| 15 |
router = APIRouter(prefix="/api", tags=["stats"])
|
| 16 |
|
| 17 |
|
| 18 |
@router.get("/{user_id}/tasks/stats")
|
| 19 |
async def get_task_stats(
|
| 20 |
+
user_id: UUID,
|
| 21 |
current_user: User = Depends(get_current_user),
|
| 22 |
+
session: Session = Depends(get_session_dep)
|
| 23 |
):
|
| 24 |
"""
|
| 25 |
Get task statistics for a user.
|
|
|
|
| 27 |
"""
|
| 28 |
# Verify user is requesting their own stats
|
| 29 |
if current_user.id != user_id:
|
| 30 |
+
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Unauthorized")
|
| 31 |
|
| 32 |
# Get all tasks for user
|
| 33 |
+
tasks = session.exec(select(Task).where(Task.user_id == user_id)).all()
|
| 34 |
|
| 35 |
total = len(tasks)
|
| 36 |
completed = len([t for t in tasks if t.completed])
|