From bae2368c11088d983ce89c66a854c38ba84ed471 Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 17 Jan 2025 14:36:59 +0200 Subject: [PATCH 1/2] Solution-py-fastapi-homework-1-task --- src/main.py | 2 ++ src/routes/movies.py | 36 ++++++++++++++++++++++++++++++++++-- src/schemas/movies.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index 35ca2a7..635ceaa 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,7 @@ from fastapi import FastAPI from routes import movie_router +# from routes import movies app = FastAPI( title="Movies homework", @@ -10,3 +11,4 @@ api_version_prefix = "/api/v1" app.include_router(movie_router, prefix=f"{api_version_prefix}/theater", tags=["theater"]) +# app.include_router(movies.router) diff --git a/src/routes/movies.py b/src/routes/movies.py index c62c1a2..cbc6eb8 100644 --- a/src/routes/movies.py +++ b/src/routes/movies.py @@ -2,9 +2,41 @@ from sqlalchemy.orm import Session from database import get_db, MovieModel - +from schemas.movies import MovieListResponseSchema, MovieDetailResponseSchema router = APIRouter() -# Write your code here \ No newline at end of file +@router.get("/movies", response_model=MovieListResponseSchema) +def get_list_movies(page: int = Query(default=1, ge=1), + per_page: int = Query(default=10, ge=1, le=20), + db: Session = Depends(get_db) + ): + offset = (page - 1) * per_page + movies_with_pagination = db.query(MovieModel).offset(offset).limit(per_page).all() + movie_list_schema = [ + MovieDetailResponseSchema.model_validate(movie) for movie in movies_with_pagination + ] + total_items = db.query(MovieModel).count() + total_pages = (total_items + per_page - 1) // per_page + prev_page = f"/theater/movies/?page={page - 1}&per_page={per_page}" + next_page = f"/theater/movies/?page={page + 1}&per_page={per_page}" + + if not movie_list_schema: + raise HTTPException(status_code=404, detail="No movies found.") + + return MovieListResponseSchema( + movies=movies_with_pagination, + total_items=total_items, + total_pages=total_pages, + prev_page=None if page == 1 else prev_page, + next_page=None if page == total_pages else next_page, + ) + + +@router.get("/movies/{movie_id}", response_model=MovieDetailResponseSchema) +def get_movie(movie_id: int, db: Session = Depends(get_db)): + movie = db.query(MovieModel).filter(MovieModel.id == movie_id).first() + if not movie: + raise HTTPException(status_code=404, detail="Movie with the given ID was not found.") + return movie diff --git a/src/schemas/movies.py b/src/schemas/movies.py index fabb9be..982990c 100644 --- a/src/schemas/movies.py +++ b/src/schemas/movies.py @@ -1 +1,30 @@ -# Write your code here +import datetime + +from pydantic import BaseModel + + +class MovieDetailResponseSchema(BaseModel): + id: int + name: str + date: datetime.date + score: float + genre: str + overview: str + crew: str + orig_title: str + status: str + orig_lang: str + budget: float + revenue: float + country: str + + class Config: + from_attributes = True + + +class MovieListResponseSchema(BaseModel): + movies: list[MovieDetailResponseSchema] + prev_page: str | None = None + next_page: str | None = None + total_pages: int + total_items: int From 736471cd65fb3b574aa15f7301ccfcf8237e4457 Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 17 Jan 2025 14:45:54 +0200 Subject: [PATCH 2/2] fix --- src/main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.py b/src/main.py index 635ceaa..35ca2a7 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,6 @@ from fastapi import FastAPI from routes import movie_router -# from routes import movies app = FastAPI( title="Movies homework", @@ -11,4 +10,3 @@ api_version_prefix = "/api/v1" app.include_router(movie_router, prefix=f"{api_version_prefix}/theater", tags=["theater"]) -# app.include_router(movies.router)