add api endpoint for geojson data fetching

This commit is contained in:
Viktor Barzin 2025-06-15 12:42:56 +00:00
parent 7602c3762b
commit b995bc2286
No known key found for this signature in database
GPG key ID: 4056458DBDBF8863
2 changed files with 18 additions and 6 deletions

View file

@ -7,6 +7,7 @@ from repositories.listing_repository import ListingRepository
from repositories.listing_repository import ListingRepository
from database import engine
from fastapi.middleware.cors import CORSMiddleware
from ui_exporter import export_immoweb
app = FastAPI()
@ -25,3 +26,10 @@ async def get_listing(user: Annotated[User, Depends(get_current_user)]):
repository = ListingRepository(engine)
listings = await repository.get_listings(limit=5)
return {"listings": listings}
@app.get("/api/listing_geojson")
async def get_listing_geojson(user: Annotated[User, Depends(get_current_user)]):
repository = ListingRepository(engine)
geojson_data = await export_immoweb(repository, limit=500)
return geojson_data

View file

@ -7,13 +7,13 @@ from repositories.listing_repository import ListingRepository
async def export_immoweb(
repository: ListingRepository,
output_file: str,
output_file: str | None = None,
query_parameters: QueryParameters | None = None,
limit: int | None = None,
):
output_file_path = pathlib.Path(output_file)
output_file_path.touch(exist_ok=True)
listings = await repository.get_listings(
query_parameters=query_parameters,
limit=limit,
)
# Convert listings to immoweb format
@ -53,6 +53,10 @@ async def export_immoweb(
prefix = "var data = "
serialized_data = {"type": "FeatureCollection", "features": immoweb_listings}
result = prefix + json.dumps(serialized_data, indent=4)
with open(str(output_file_path), "w") as f:
f.write(result)
# json.dump(serialized_data, f, indent=4)
if output_file:
output_file_path = pathlib.Path(output_file)
output_file_path.touch(exist_ok=True)
with open(str(output_file_path), "w") as f:
f.write(result)
return serialized_data