78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlmodel import Session, select
|
|
|
|
from app.database import get_session
|
|
from app.models.user import User
|
|
from app.models.activity import Activity
|
|
from app.models.zone import Zone
|
|
from app.schemas.activity import ActivityCreate, ActivityRead, ActivityDetail
|
|
from app.auth.dependencies import get_current_user
|
|
from app.services.geo_pipeline import process_activity
|
|
|
|
router = APIRouter(prefix="/activities", tags=["activities"])
|
|
|
|
|
|
@router.post("", response_model=ActivityDetail, status_code=status.HTTP_201_CREATED)
|
|
def create_activity(
|
|
body: ActivityCreate,
|
|
current_user: User = Depends(get_current_user),
|
|
session: Session = Depends(get_session),
|
|
):
|
|
"""Upload a GPS track and trigger the zone computation pipeline."""
|
|
result = process_activity(
|
|
user_id=current_user.id,
|
|
activity_type=body.type,
|
|
started_at=body.started_at,
|
|
ended_at=body.ended_at,
|
|
gps_track=body.gps_track,
|
|
session=session,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.get("", response_model=list[ActivityRead])
|
|
def list_activities(
|
|
current_user: User = Depends(get_current_user),
|
|
session: Session = Depends(get_session),
|
|
limit: int = 50,
|
|
offset: int = 0,
|
|
):
|
|
"""List current user's activities."""
|
|
activities = session.exec(
|
|
select(Activity)
|
|
.where(Activity.user_id == current_user.id)
|
|
.order_by(Activity.created_at.desc()) # type: ignore[union-attr]
|
|
.offset(offset)
|
|
.limit(limit)
|
|
).all()
|
|
return activities
|
|
|
|
|
|
@router.get("/{activity_id}", response_model=ActivityDetail)
|
|
def get_activity(
|
|
activity_id: uuid.UUID,
|
|
current_user: User = Depends(get_current_user),
|
|
session: Session = Depends(get_session),
|
|
):
|
|
activity = session.get(Activity, activity_id)
|
|
if not activity or activity.user_id != current_user.id:
|
|
raise HTTPException(status_code=404, detail="Activity not found")
|
|
|
|
# Find associated zone
|
|
zone = session.exec(select(Zone).where(Zone.activity_id == activity_id)).first()
|
|
|
|
return ActivityDetail(
|
|
id=activity.id,
|
|
user_id=activity.user_id,
|
|
type=activity.type,
|
|
started_at=activity.started_at,
|
|
ended_at=activity.ended_at,
|
|
distance_m=activity.distance_m,
|
|
status=activity.status,
|
|
created_at=activity.created_at,
|
|
zone_id=zone.id if zone else None,
|
|
area_m2=zone.area_m2 if zone else None,
|
|
)
|