-
Notifications
You must be signed in to change notification settings - Fork 43
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
@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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider checking if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if |
||
prev_page = f"/theater/movies/?page={page - 1}&per_page={per_page}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
movies=movies_with_pagination, | ||
total_items=total_items, | ||
total_pages=total_pages, | ||
prev_page=None if page == 1 else prev_page, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
next_page: str | None = None | ||
total_pages: int | ||
total_items: int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure that
MovieListResponseSchema
andMovieDetailResponseSchema
are correctly defined inschemas.movies
and match the expected response structure.