update places router: gmaps api searchText
This commit is contained in:
parent
d5fec58ad5
commit
04f64cf059
@ -6,11 +6,12 @@ from sqlmodel import select
|
|||||||
|
|
||||||
from ..config import settings
|
from ..config import settings
|
||||||
from ..deps import SessionDep, get_current_username
|
from ..deps import SessionDep, get_current_username
|
||||||
from ..models.models import (Category, Image, Place, PlaceCreate, PlaceRead,
|
from ..models.models import (Category, Image, GooglePlaceResult, Place, PlaceCreate,
|
||||||
PlacesCreate, PlaceUpdate)
|
PlaceRead, PlacesCreate, PlaceUpdate)
|
||||||
from ..security import verify_exists_and_owns
|
from ..security import verify_exists_and_owns
|
||||||
from ..utils.utils import (b64img_decode, download_file, patch_image,
|
from ..utils.utils import (b64img_decode, download_file, patch_image,
|
||||||
save_image_to_file)
|
save_image_to_file)
|
||||||
|
from ..utils.gmaps import compute_avg_price, gmaps_textsearch
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/places", tags=["places"])
|
router = APIRouter(prefix="/api/places", tags=["places"])
|
||||||
|
|
||||||
@ -110,6 +111,31 @@ async def create_places(
|
|||||||
return [PlaceRead.serialize(p) for p in new_places]
|
return [PlaceRead.serialize(p) for p in new_places]
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/google-search", response_model=list[GooglePlaceResult])
|
||||||
|
async def google_search_text(q: str, session: SessionDep, current_user: Annotated[str, Depends(get_current_username)]) -> list[GooglePlaceResult]:
|
||||||
|
db_user = session.get(User, current_user)
|
||||||
|
if not db_user or not db_user.google_apikey:
|
||||||
|
raise HTTPException(status_code=400, detail="Google Maps API key not configured")
|
||||||
|
|
||||||
|
data = await gmaps_textsearch(q, db_user.google_apikey)
|
||||||
|
|
||||||
|
results = []
|
||||||
|
for place in data:
|
||||||
|
loc = place.get("location", {})
|
||||||
|
result = GooglePlaceResult(
|
||||||
|
id=place.get("id"),
|
||||||
|
name=place.get("displayName", {}).get('text'),
|
||||||
|
lat=loc.get("latitude"),
|
||||||
|
lng=loc.get("longitude"),
|
||||||
|
price=compute_avg_price(place.get("priceRange")),
|
||||||
|
types=place.get("types", []),
|
||||||
|
allow_dogs=place.get("allowDogs")
|
||||||
|
)
|
||||||
|
results.append(result)
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{place_id}", response_model=PlaceRead)
|
@router.put("/{place_id}", response_model=PlaceRead)
|
||||||
def update_place(
|
def update_place(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user