All tweaks added
This commit is contained in:
@@ -3,7 +3,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, desc
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import get_current_user
|
||||
@@ -23,7 +23,7 @@ class RouteCreate(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
sport_type: Optional[str] = None
|
||||
activity_id: int # use this activity as the reference route
|
||||
activity_id: int
|
||||
|
||||
|
||||
class RouteOut(BaseModel):
|
||||
@@ -34,6 +34,7 @@ class RouteOut(BaseModel):
|
||||
reference_polyline: Optional[str]
|
||||
bounding_box: Optional[dict]
|
||||
distance_m: Optional[float]
|
||||
auto_detected: Optional[bool]
|
||||
created_at: datetime
|
||||
|
||||
class Config:
|
||||
@@ -64,13 +65,44 @@ async def list_routes(
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
@router.get("/recent-activities")
|
||||
async def recent_activities_for_route(
|
||||
days: int = Query(14, ge=1, le=90),
|
||||
sport_type: Optional[str] = None,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Return recent activities for the route creation dropdown."""
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(days=days)
|
||||
q = select(Activity).where(
|
||||
Activity.user_id == current_user.id,
|
||||
Activity.start_time >= cutoff,
|
||||
Activity.sport_type != "swimming",
|
||||
)
|
||||
if sport_type:
|
||||
q = q.where(Activity.sport_type == sport_type)
|
||||
q = q.order_by(desc(Activity.start_time)).limit(50)
|
||||
result = await db.execute(q)
|
||||
activities = result.scalars().all()
|
||||
return [
|
||||
{
|
||||
"id": a.id,
|
||||
"name": a.name,
|
||||
"sport_type": a.sport_type,
|
||||
"start_time": a.start_time,
|
||||
"distance_m": a.distance_m,
|
||||
"duration_s": a.duration_s,
|
||||
}
|
||||
for a in activities
|
||||
]
|
||||
|
||||
|
||||
@router.post("/", response_model=RouteOut)
|
||||
async def create_route(
|
||||
body: RouteCreate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
# Load the reference activity
|
||||
act_result = await db.execute(
|
||||
select(Activity).where(
|
||||
Activity.id == body.activity_id,
|
||||
@@ -89,11 +121,10 @@ async def create_route(
|
||||
reference_polyline=activity.polyline,
|
||||
bounding_box=activity.bounding_box,
|
||||
distance_m=activity.distance_m,
|
||||
auto_detected=False,
|
||||
)
|
||||
db.add(route)
|
||||
await db.flush()
|
||||
|
||||
# Link this activity to the route
|
||||
activity.named_route_id = route.id
|
||||
await db.commit()
|
||||
await db.refresh(route)
|
||||
@@ -124,7 +155,6 @@ async def route_activities(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""All activities on this named route, ordered fastest first."""
|
||||
result = await db.execute(
|
||||
select(Activity).where(
|
||||
Activity.named_route_id == route_id,
|
||||
@@ -153,7 +183,6 @@ async def assign_activity_to_route(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Manually assign an activity to a named route."""
|
||||
activity_id = body.get("activity_id")
|
||||
act_result = await db.execute(
|
||||
select(Activity).where(
|
||||
@@ -164,7 +193,6 @@ async def assign_activity_to_route(
|
||||
activity = act_result.scalar_one_or_none()
|
||||
if not activity:
|
||||
raise HTTPException(status_code=404, detail="Activity not found")
|
||||
|
||||
activity.named_route_id = route_id
|
||||
await db.commit()
|
||||
return {"status": "ok"}
|
||||
|
||||
Reference in New Issue
Block a user