Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution-py-fastapi-homework-1-task #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/routes/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,41 @@
from sqlalchemy.orm import Session

from database import get_db, MovieModel

from schemas.movies import MovieListResponseSchema, MovieDetailResponseSchema

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that MovieListResponseSchema and MovieDetailResponseSchema are correctly defined in schemas.movies and match the expected response structure.


router = APIRouter()


# Write your code here
@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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider checking if page - 1 is valid (greater than 0) before constructing prev_page to avoid generating invalid URLs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if page - 1 is valid (greater than 0) before constructing prev_page to prevent invalid URLs.

prev_page = f"/theater/movies/?page={page - 1}&per_page={per_page}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that next_page does not exceed total_pages to avoid generating invalid URLs.

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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The movies field in MovieListResponseSchema should likely be populated with movie_list_schema instead of movies_with_pagination to ensure the response matches the schema.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The movies field in MovieListResponseSchema should be populated with movie_list_schema instead of movies_with_pagination to ensure consistency with the schema definition.

movies=movies_with_pagination,
total_items=total_items,
total_pages=total_pages,
prev_page=None if page == 1 else prev_page,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that next_page does not exceed total_pages to avoid generating invalid URLs.

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
31 changes: 30 additions & 1 deletion src/schemas/movies.py
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the movies field in MovieListResponseSchema is populated with instances of MovieDetailResponseSchema in the route handler to maintain consistency with the schema definition.

next_page: str | None = None
total_pages: int
total_items: int
Loading