diff --git a/.flake8 b/.flake8 index cbd72d8..193a3d8 100644 --- a/.flake8 +++ b/.flake8 @@ -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 diff --git a/.gitignore b/.gitignore index c720747..d760850 100644 --- a/.gitignore +++ b/.gitignore @@ -170,3 +170,4 @@ cython_debug/ # PyPI configuration file .pypirc +.vscode diff --git a/src/routes/movies.py b/src/routes/movies.py index c62c1a2..0dde3cb 100644 --- a/src/routes/movies.py +++ b/src/routes/movies.py @@ -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 \ No newline at end of file +@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 diff --git a/src/schemas/movies.py b/src/schemas/movies.py index fabb9be..700ccb1 100644 --- a/src/schemas/movies.py +++ b/src/schemas/movies.py @@ -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 + total_pages: int + total_items: int