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

@@ -1,9 +1,10 @@
"""Listing endpoints read-only access to Funda data with ELO ratings."""
"""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
@@ -18,11 +19,11 @@ def get_listings(
):
"""Return all listings with their current ELO rating."""
query = LISTING_SELECT
params: dict = {}
params: dict = {"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"
query += " order by elo_rating desc"
result = db.execute(text(query), params)
return [row_to_listing(row) for row in result]
@@ -30,8 +31,10 @@ def get_listings(
@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()
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)