38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from sqlmodel import select
|
|
|
|
from src.db import get_db
|
|
from src.maps.models import Waypoint
|
|
from src.maps.schemas import WaypointCreate, WaypointResponse
|
|
from src.utils.decorators import auth_required
|
|
|
|
maps_router = APIRouter()
|
|
|
|
@auth_required()
|
|
@maps_router.post("/waypoints", response_model=WaypointResponse)
|
|
def create_waypoint(waypoint: WaypointCreate, db: Session = Depends(get_db)):
|
|
db_wp = Waypoint.model_validate(waypoint)
|
|
db.add(db_wp)
|
|
db.commit()
|
|
db.refresh(db_wp)
|
|
return db_wp
|
|
|
|
|
|
@auth_required()
|
|
@maps_router.get("/waypoints", response_model=List[WaypointResponse])
|
|
def get_waypoints(db: Session = Depends(get_db)):
|
|
waypoints = db.execute(select(Waypoint)).scalars().all()
|
|
return waypoints
|
|
|
|
|
|
@auth_required()
|
|
@maps_router.get("/waypoints/{waypoint_id}", response_model=WaypointResponse)
|
|
def get_waypoint(waypoint_id: int, db: Session = Depends(get_db)):
|
|
wp = db.get(Waypoint, waypoint_id)
|
|
if not wp:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Waypoint not found")
|
|
return wp
|