41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
"""Listing endpoints – read-only access to listing data with ELO ratings."""
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException
|
||
from sqlalchemy import text
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app.config import settings
|
||
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 = {"default_elo": settings.DEFAULT_ELO}
|
||
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, "default_elo": settings.DEFAULT_ELO}
|
||
).first()
|
||
if not row:
|
||
raise HTTPException(status_code=404, detail="Listing not found")
|
||
return row_to_listing(row)
|