Initial commit
This commit is contained in:
37
src/maps/router.py
Normal file
37
src/maps/router.py
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
Reference in New Issue
Block a user