CoolFace
Apppublic

mydatascraper/competitor_compare

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
stores.py32 linesDownload Raw Back to routers
1from fastapi import APIRouter, Depends, Query
2from sqlalchemy.ext.asyncio import AsyncSession
3from sqlalchemy import select, func
4from app.database import get_db
5from app.auth import verify_api_key
6from app.models import Store
7from app.schemas import StoreResponse, RetailerFilter
8from typing import Optional, List
9
10router = APIRouter(prefix="/stores", tags=["Stores"])
11
12
13@router.get("/", response_model=List[StoreResponse])
14async def list_stores(
15    retailer: Optional[RetailerFilter] = Query(None),
16    city: Optional[str] = Query(None),
17    zip_code: Optional[str] = Query(None),
18    db: AsyncSession = Depends(get_db),
19    _api_key: dict = Depends(verify_api_key),
20):
21    """List all tracked stores, optionally filtered by retailer or location."""
22    stmt = select(Store).where(Store.is_active == True)
23    if retailer:
24        stmt = stmt.where(Store.retailer == retailer.value)
25    if city:
26        stmt = stmt.where(Store.city.ilike(f"%{city}%"))
27    if zip_code:
28        stmt = stmt.where(Store.zip_code == zip_code)
29
30    stmt = stmt.order_by(Store.retailer, Store.city)
31    result = await db.execute(stmt)
32    return result.scalars().all()