mirror of
https://github.com/sstent/AICyclingCoach.git
synced 2026-01-25 16:41:58 +00:00
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from fastapi import APIRouter, UploadFile, File, Depends, HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from app.database import get_db
|
|
from app.services.gpx import parse_gpx, store_gpx_file
|
|
from app.schemas.gpx import RouteCreate, Route as RouteSchema
|
|
from app.models import Route
|
|
import os
|
|
|
|
router = APIRouter(prefix="/gpx", tags=["GPX Routes"])
|
|
|
|
@router.post("/upload", response_model=RouteSchema)
|
|
async def upload_gpx_route(
|
|
file: UploadFile = File(...),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
# Store GPX file
|
|
gpx_path = await store_gpx_file(file)
|
|
|
|
# Parse GPX file
|
|
gpx_data = await parse_gpx(gpx_path)
|
|
|
|
# Create route in database
|
|
route_data = RouteCreate(
|
|
name=file.filename,
|
|
description=f"Uploaded from {file.filename}",
|
|
total_distance=gpx_data['total_distance'],
|
|
elevation_gain=gpx_data['elevation_gain'],
|
|
gpx_file_path=gpx_path
|
|
)
|
|
db_route = Route(**route_data.dict())
|
|
db.add(db_route)
|
|
await db.commit()
|
|
await db.refresh(db_route)
|
|
|
|
return db_route |