diff --git a/app/api.py b/app/api.py index 62115d93..7d34ea40 100644 --- a/app/api.py +++ b/app/api.py @@ -1,5 +1,6 @@ import asyncio import uuid +from datetime import date from pathlib import Path from typing import Annotated @@ -119,6 +120,21 @@ async def authentication( ) +@app.get("/prices", response_model=list[schemas.PriceBase]) +async def get_price( + product_code: str | None = None, + location_osm_id: int | None = None, + date: date | None = None, +): + filters = { + "product_code": product_code, + "location_osm_id": location_osm_id, + "date": date, + } + db_prices = crud.get_prices(db, filters=filters) # type: ignore + return db_prices + + @app.post("/prices", response_model=schemas.PriceBase) async def create_price( price: schemas.PriceCreate, diff --git a/app/crud.py b/app/crud.py index caae13c0..c2d00d3e 100644 --- a/app/crud.py +++ b/app/crud.py @@ -49,6 +49,17 @@ def delete_user(db: Session, user_id: UserBase): return False +def get_prices(db: Session, filters={}): + query = db.query(Price) + if filters.get("product_code", None): + query = query.filter(Price.product_code == filters["product_code"]) + if filters.get("location_osm_id", None): + query = query.filter(Price.location_osm_id == filters["location_osm_id"]) + if filters.get("date", None): + query = query.filter(Price.date == filters["date"]) + return query.all() + + def create_price(db: Session, price: PriceCreate, user: UserBase): db_price = Price(**price.dict(), owner=user.user_id) db.add(db_price)