feat: separate sql

This commit is contained in:
Stijnvandenbroek
2026-03-06 14:26:20 +00:00
parent 81188a4569
commit c908d96921
16 changed files with 179 additions and 219 deletions

View File

@@ -11,6 +11,11 @@ from app.schemas import RankingResponse
router = APIRouter()
SAMPLE_JOIN = (
f" inner join {settings.ELO_SCHEMA}.sample_listings as s"
f" on l.global_id = s.global_id"
)
@router.get("/rankings", response_model=list[RankingResponse])
def get_rankings(
@@ -19,21 +24,15 @@ def get_rankings(
offset: int = 0,
db: Session = Depends(get_db),
):
"""Return listings ranked by ELO rating (highest first).
Only listings in the stable sample (elo.sample_listings) are shown.
"""
query = LISTING_SELECT + f"""
INNER JOIN {settings.ELO_SCHEMA}.sample_listings s
ON l.global_id = s.global_id
"""
params: dict = {"limit": limit, "offset": offset}
"""Return listings ranked by ELO rating (highest first)."""
query = LISTING_SELECT + SAMPLE_JOIN
params: dict = {"limit": limit, "offset": offset, "default_elo": settings.DEFAULT_ELO}
if status and status != "all":
query += " WHERE l.status = :status"
query += " where l.status = :status"
params["status"] = status
query += " ORDER BY elo_rating DESC, l.current_price DESC LIMIT :limit OFFSET :offset"
query += " order by elo_rating desc, l.current_price desc limit :limit offset :offset"
result = db.execute(text(query), params)
listings = [row_to_listing(row) for row in result]