"""Listing endpoints – read-only access to Funda data with ELO ratings.""" from fastapi import APIRouter, Depends, HTTPException from sqlalchemy import text from sqlalchemy.orm import Session from app.database import get_db from app.queries import LISTING_SELECT, row_to_listing from app.schemas import ListingResponse router = APIRouter() @router.get("/listings", response_model=list[ListingResponse]) def get_listings( status: str | None = None, db: Session = Depends(get_db), ): """Return all listings with their current ELO rating.""" query = LISTING_SELECT params: dict = {} if status and status != "all": query += " WHERE l.status = :status" params["status"] = status query += " ORDER BY elo_rating DESC" result = db.execute(text(query), params) return [row_to_listing(row) for row in result] @router.get("/listings/{global_id}", response_model=ListingResponse) def get_listing(global_id: str, db: Session = Depends(get_db)): """Return a single listing by its global_id.""" query = LISTING_SELECT + " WHERE l.global_id = :global_id" row = db.execute(text(query), {"global_id": global_id}).first() if not row: raise HTTPException(status_code=404, detail="Listing not found") return row_to_listing(row)