endpoint setup
This commit is contained in:
40
app/api/v1/endpoints/map.py
Normal file
40
app/api/v1/endpoints/map.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.db.session import get_async_session
|
||||
from app.services.location_service import LocationService
|
||||
from app.schemas.map_schema import MapQueryResponse
|
||||
|
||||
router = APIRouter(prefix="/map", tags=["Map"])
|
||||
|
||||
|
||||
@router.get("/query", response_model=MapQueryResponse)
|
||||
async def query_nearby_locations(
|
||||
lat: float = Query(..., ge=-90, le=90),
|
||||
lon: float = Query(..., ge=-180, le=180),
|
||||
radius: int = Query(2000, gt=0, le=50000),
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
):
|
||||
"""
|
||||
Returns all locations within a given radius from a lat/lon point.
|
||||
Uses PostGIS ST_DWithin (geography).
|
||||
"""
|
||||
|
||||
try:
|
||||
locations = await LocationService.get_nearby_locations(
|
||||
session=session,
|
||||
lat=lat,
|
||||
lon=lon,
|
||||
radius= radius
|
||||
)
|
||||
|
||||
return MapQueryResponse(
|
||||
total=len(locations),
|
||||
locations=locations
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="Failed to fetch nearby locations"
|
||||
)
|
||||
93
app/api/v1/endpoints/property.py
Normal file
93
app/api/v1/endpoints/property.py
Normal file
@@ -0,0 +1,93 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
|
||||
from app.db.session import get_async_session
|
||||
from app.services.property_service import PropertyService
|
||||
from app.schemas.property_schema import PropertyResponse
|
||||
from app.schemas.full_entry_schema import FullEntryCreateRequest
|
||||
from app.schemas.property_update_request import PropertyOwnerUpdateRequest
|
||||
|
||||
router = APIRouter(prefix="/property", tags=["Property"])
|
||||
|
||||
|
||||
@router.get("/by-location/{location_id}", response_model=PropertyResponse)
|
||||
async def get_property_by_location(
|
||||
location_id: int,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
):
|
||||
"""
|
||||
Fetch property + owner using location_id.
|
||||
Called when agent clicks map box.
|
||||
"""
|
||||
|
||||
property_obj = await PropertyService.get_property_by_location_id(
|
||||
session=session,
|
||||
location_id=location_id
|
||||
)
|
||||
|
||||
if not property_obj:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Property not found for given location"
|
||||
)
|
||||
|
||||
return property_obj
|
||||
|
||||
@router.post("/create-full", response_model=PropertyResponse)
|
||||
async def create_full_entry(
|
||||
payload: FullEntryCreateRequest,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
):
|
||||
"""
|
||||
Creates Location + Owner + Property in a single transaction.
|
||||
Frontend cannot send IDs.
|
||||
"""
|
||||
|
||||
try:
|
||||
property_obj = await PropertyService.create_full_entry(
|
||||
session=session,
|
||||
location_data=payload.location,
|
||||
owner_data=payload.owner,
|
||||
property_data=payload.property,
|
||||
)
|
||||
|
||||
return property_obj
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="Failed to create property entry"
|
||||
)
|
||||
|
||||
@router.put("/{property_id}", response_model=PropertyResponse)
|
||||
async def update_property_and_owner(
|
||||
property_id: int = Path(..., gt=0),
|
||||
payload: PropertyOwnerUpdateRequest = ...,
|
||||
session: AsyncSession = Depends(get_async_session),
|
||||
):
|
||||
"""
|
||||
Update Property + Owner details.
|
||||
Partial updates supported.
|
||||
"""
|
||||
|
||||
if not payload.owner and not payload.property:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="No update data provided"
|
||||
)
|
||||
|
||||
property_obj = await PropertyService.update_property_and_owner(
|
||||
session=session,
|
||||
property_id=property_id,
|
||||
owner_data=payload.owner.dict(exclude_unset=True) if payload.owner else None,
|
||||
property_data=payload.property.dict(exclude_unset=True) if payload.property else None,
|
||||
)
|
||||
|
||||
if not property_obj:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Property not found"
|
||||
)
|
||||
|
||||
return property_obj
|
||||
Reference in New Issue
Block a user