
- Set up SQLite database configuration and directory structure - Configure Alembic for proper SQLite migrations - Add initial model schemas and API endpoints - Fix OAuth2 authentication - Implement proper code formatting with Ruff
123 lines
3.7 KiB
Python
123 lines
3.7 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, Path
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.api import deps
|
|
from app.core.exceptions import NotFoundException, ForbiddenException
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.schemas.location import Location, LocationCreate, LocationUpdate
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Location])
|
|
def get_user_locations(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get all locations for the current user.
|
|
"""
|
|
locations = crud.get_user_locations(db, user_id=current_user.id)
|
|
return locations
|
|
|
|
|
|
@router.get("/default", response_model=Location)
|
|
def get_default_location(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get the default location for the current user.
|
|
"""
|
|
location = crud.get_user_default_location(db, user_id=current_user.id)
|
|
|
|
if not location:
|
|
raise NotFoundException(detail="No default location set for this user")
|
|
|
|
return location
|
|
|
|
|
|
@router.post("/", response_model=Location)
|
|
def create_location(
|
|
location_in: LocationCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create a new location for the current user.
|
|
"""
|
|
location = crud.create_location(db, obj_in=location_in, user_id=current_user.id)
|
|
return location
|
|
|
|
|
|
@router.get("/{location_id}", response_model=Location)
|
|
def get_location(
|
|
location_id: int = Path(..., description="ID of the location to get"),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get a specific location by ID.
|
|
"""
|
|
location = crud.get_location(db, location_id=location_id)
|
|
|
|
if not location:
|
|
raise NotFoundException(detail="Location not found")
|
|
|
|
# Check if location belongs to current user
|
|
if location.user_id != current_user.id:
|
|
raise ForbiddenException(detail="Not enough permissions to access this location")
|
|
|
|
return location
|
|
|
|
|
|
@router.put("/{location_id}", response_model=Location)
|
|
def update_location(
|
|
location_in: LocationUpdate,
|
|
location_id: int = Path(..., description="ID of the location to update"),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a specific location by ID.
|
|
"""
|
|
location = crud.get_location(db, location_id=location_id)
|
|
|
|
if not location:
|
|
raise NotFoundException(detail="Location not found")
|
|
|
|
# Check if location belongs to current user
|
|
if location.user_id != current_user.id:
|
|
raise ForbiddenException(detail="Not enough permissions to update this location")
|
|
|
|
location = crud.update_location(db, db_obj=location, obj_in=location_in)
|
|
|
|
return location
|
|
|
|
|
|
@router.delete("/{location_id}", response_model=Location)
|
|
def delete_location(
|
|
location_id: int = Path(..., description="ID of the location to delete"),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a specific location by ID.
|
|
"""
|
|
location = crud.get_location(db, location_id=location_id)
|
|
|
|
if not location:
|
|
raise NotFoundException(detail="Location not found")
|
|
|
|
# Check if location belongs to current user
|
|
if location.user_id != current_user.id:
|
|
raise ForbiddenException(detail="Not enough permissions to delete this location")
|
|
|
|
location = crud.delete_location(db, location_id=location_id)
|
|
|
|
return location |