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

implement schemas and routes #8

Open
wants to merge 4 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
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
inline-quotes = "
ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818, VNE001, F401
ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818, VNE001, F401, E121
max-line-length = 119
max-complexity = 18
select = B,C,E,F,W,T4,B9,ANN,Q0,N8,VNE
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,4 @@ cython_debug/

# PyPI configuration file
.pypirc
.vscode
Mxbely marked this conversation as resolved.
Show resolved Hide resolved
48 changes: 46 additions & 2 deletions src/routes/movies.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session

from database import get_db, MovieModel
from database import get_db
from schemas.movies import MovieListResponseSchema, MovieDetailResponseSchema
from database.models import MovieModel


router = APIRouter()


# Write your code here
@router.get("/movies", response_model=MovieListResponseSchema)
def get_movies(
page: int = Query(1, ge=1),
per_page: int = Query(10, ge=1),
db: Session = Depends(get_db)
):
movies = db.query(MovieModel).offset((page - 1) * per_page).limit(per_page).all()
total_movies = db.query(MovieModel).count()
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 movies:
raise HTTPException(status_code=404, detail="No movies found.")

if page and page < 1:
raise HTTPException(
status_code=422,
detail=[
{
"loc": [
"query",
"page"
],
"msg": "ensure this value is greater than or equal to 1",
"type": "value_error.number.not_ge"
}
]
)

return {
"movies": movies,
"prev_page": prev_page if page > 1 else None,
"next_page": next_page if total_movies > (page * per_page) else None,
"total_pages": (total_movies // per_page) + (1 if total_movies % per_page > 0 else 0),
"total_items": total_movies,
}


@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
28 changes: 27 additions & 1 deletion src/schemas/movies.py
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# Write your code here
from pydantic import BaseModel
import datetime
from typing import Optional, List


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 MovieListResponseSchema(BaseModel):
movies: List[MovieDetailResponseSchema]
prev_page: Optional[str] | None
next_page: Optional[str] | None
Mxbely marked this conversation as resolved.
Show resolved Hide resolved
total_pages: int
Mxbely marked this conversation as resolved.
Show resolved Hide resolved
total_items: int
Loading