Skip to content

Commit

Permalink
feat: prices GET endpoint (#26)
Browse files Browse the repository at this point in the history
* Basic /prices GET path

* Add basic filtering
  • Loading branch information
raphodn authored Nov 15, 2023
1 parent 5cc63f4 commit a769910
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import uuid
from datetime import date
from pathlib import Path
from typing import Annotated

Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a769910

Please sign in to comment.