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 #16

Open
wants to merge 1 commit 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
48 changes: 46 additions & 2 deletions src/routes/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,53 @@
from sqlalchemy.orm import Session

from database import get_db, MovieModel

from schemas.movies import MovieDetailResponseSchema, MovieListResponseSchema

router = APIRouter()


# Write your code here
@router.get("/movies/", response_model=MovieListResponseSchema)
def get_movies_list(
page: int = Query(1, ge=1, description="Page number, must be >= 1"),
per_page: int = Query(
10, ge=1, le=20,
description="Items per page, must be >= 1 and <= 20"
),
db: Session = Depends(get_db)
):
total_items = db.query(MovieModel).count()
total_pages = (total_items + per_page - 1) // per_page

movies = (
db.query(MovieModel)
.offset((page - 1) * per_page)
.limit(per_page)
.all()
)

if not movies:
raise HTTPException(status_code=404, detail="No movies found.")

prev_page = (f"/theater/movies/?page={page - 1}"
f"&per_page={per_page}") if page > 1 else None
next_page = (f"/theater/movies/?page={page + 1}"
Comment on lines +32 to +35

Choose a reason for hiding this comment

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

The pagination links are using an incorrect base path /theater/movies/. It should be /movies/ to match the endpoint defined in the router.

f"&per_page={per_page}") if page < total_pages else None

return MovieListResponseSchema(
movies=movies,
prev_page=prev_page,
next_page=next_page,
total_pages=total_pages,
total_items=total_items
)


@router.get("/movies/{movie_id}/", response_model=MovieDetailResponseSchema)
def get_movie_details(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 typing import Optional, List

from pydantic import BaseModel, ConfigDict


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

model_config = ConfigDict(from_attributes=True)


class MovieListResponseSchema(BaseModel):
movies: List[MovieDetailResponseSchema]
prev_page: Optional[str] = None
next_page: Optional[str] = None
total_pages: int
total_items: int
Loading