Skip to content

Commit

Permalink
feat: add pagination on GET /prices (#28)
Browse files Browse the repository at this point in the history
* Add fastapi-pagination package

* Add pagination on GET /posts

* Refactor get_prices query

---------

Co-authored-by: Raphaël Bournhonesque <raphael0202@users.noreply.github.com>
  • Loading branch information
raphodn and raphael0202 authored Nov 15, 2023
1 parent c4b6a7a commit 4e39b71
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 15 deletions.
8 changes: 5 additions & 3 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from fastapi.responses import HTMLResponse, PlainTextResponse
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.templating import Jinja2Templates
from fastapi_pagination import Page, add_pagination
from fastapi_pagination.ext.sqlalchemy import paginate
from openfoodfacts.utils import get_logger

from app import crud, schemas
Expand All @@ -43,6 +45,7 @@
"url": "https://www.gnu.org/licenses/agpl-3.0.en.html",
},
)
add_pagination(app)
templates = Jinja2Templates(directory=Path(__file__).parent / "templates")
init_sentry(settings.sentry_dns)

Expand Down Expand Up @@ -129,7 +132,7 @@ async def authentication(
)


@app.get("/prices", response_model=list[schemas.PriceBase])
@app.get("/prices", response_model=Page[schemas.PriceBase])
async def get_price(
product_code: str | None = None,
location_osm_id: int | None = None,
Expand All @@ -140,8 +143,7 @@ async def get_price(
"location_osm_id": location_osm_id,
"date": date,
}
db_prices = crud.get_prices(db, filters=filters) # type: ignore
return db_prices
return paginate(db, crud.get_prices_query(filters=filters))


@app.post("/prices", response_model=schemas.PriceBase)
Expand Down
25 changes: 16 additions & 9 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from mimetypes import guess_extension

from fastapi import UploadFile
from sqlalchemy import select
from sqlalchemy.orm import Session
from sqlalchemy.sql import func

Expand Down Expand Up @@ -55,15 +56,21 @@ 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 get_prices_query(filters: dict | None = None):
"""Useful for pagination."""
query = select(Price)
if filters:
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


def get_prices(db: Session, filters: dict | None = None):
return db.execute(get_prices_query(filters=filters)).all()


def create_price(db: Session, price: PriceCreate, user: UserBase):
Expand Down
84 changes: 81 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sentry-sdk = {extras = ["fastapi"], version = "~1.35.0"}
sqlalchemy = "~2.0.23"
sqlalchemy-utils = "~0.41.1"
uvicorn = "~0.23.2"
fastapi-pagination = "^0.12.12"


[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 4e39b71

Please sign in to comment.