First shot of pagination, try not to brake anything

This commit is contained in:
Colin Goutte 2023-08-27 18:47:57 +02:00
parent 1565863b56
commit 99045993fe
2 changed files with 19 additions and 4 deletions

View File

@ -39,7 +39,7 @@ def get_movie_by_name(db: Session, name: str = ""):
def get_all_movies(db: Session):
db_movie = db.query(models.Movie)
return db_movie.all()
return db_movie
def get_movie_by_id(db: Session, id_: str = ""):

21
dev.py
View File

@ -114,9 +114,24 @@ async def delete_movie(id_: str, db: Session = Depends(get_db)) -> None:
@app.get("/movies/")
async def list_movie(db: Session = Depends(get_db)) -> schemas.MovieObjectsOut:
movies = crud.get_all_movies(db)
count = len(movies)
async def list_movie(
db: Session = Depends(get_db),
pagenum: int | None = None,
pagesize: int | None = None,
) -> schemas.MovieObjectsOut:
paginate_params = {}
pagination_params = {"pagenum": pagenum, "pagesize": pagesize}
if any(v for v in pagination_params.values() if v is not None):
missing = [name for (name, value) in pagination_params.items() if not value]
if missing:
raise HTTPException(status_code=404, detail=f"No movie found with id {id_}")
paginate_params = dict(offset=(pagenum - 1) * pagesize, limit=pagesize)
movies = crud.get_all_movies(db, **paginate_params)
count = movies.count()
return {"movies": movies, "count": count}