feat: initial project setup
This commit is contained in:
36
backend/app/routers/rankings.py
Normal file
36
backend/app/routers/rankings.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""Ranking endpoints – listings sorted by ELO."""
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
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 RankingResponse
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/rankings", response_model=list[RankingResponse])
|
||||
def get_rankings(
|
||||
status: str | None = None,
|
||||
limit: int = 100,
|
||||
offset: int = 0,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Return listings ranked by ELO rating (highest first)."""
|
||||
query = LISTING_SELECT
|
||||
params: dict = {"limit": limit, "offset": offset}
|
||||
|
||||
if status and status != "all":
|
||||
query += " WHERE l.status = :status"
|
||||
params["status"] = status
|
||||
|
||||
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]
|
||||
|
||||
return [
|
||||
RankingResponse(rank=offset + i + 1, listing=listing)
|
||||
for i, listing in enumerate(listings)
|
||||
]
|
||||
Reference in New Issue
Block a user