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

Create routes for movies #37

Open
wants to merge 3 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 src/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from config.settings import get_settings
from .settings import get_settings
4 changes: 2 additions & 2 deletions src/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from database.models import (
from .models import (
Base,
MovieModel
)
from database.session import (
from .session import (
get_db_contextmanager,
get_db,
reset_sqlite_database
Expand Down
5 changes: 3 additions & 2 deletions src/database/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from sqlalchemy.exc import SQLAlchemyError
from tqdm import tqdm

from config import get_settings
from database import MovieModel, get_db_contextmanager
from src.config.settings import get_settings
from .models import MovieModel
from .session import get_db_contextmanager


class CSVDatabaseSeeder:
Expand Down
4 changes: 2 additions & 2 deletions src/database/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session

from config import get_settings
from database import Base
from src.config.settings import get_settings
from src.database.models import Base

settings = get_settings()

Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from fastapi import FastAPI

from routes import movie_router
from src.routes import movie_router

app = FastAPI(
title="Movies homework",
Expand Down
2 changes: 1 addition & 1 deletion src/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from routes.movies import router as movie_router
from .movies import router as movie_router
42 changes: 39 additions & 3 deletions src/routes/movies.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
from fastapi import APIRouter, Depends, HTTPException, Query
import math

from fastapi import APIRouter, Depends, HTTPException, Query, Request
from sqlalchemy.orm import Session

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


router = APIRouter()


# Write your code here
@router.get("/movies/", response_model=MovieListResponseSchema)
def get_movies(
page: int = Query(ge=1, default=1),
per_page: int = Query(ge=1, le=20, default=10),
db: Session = Depends(get_db)
):
movie_query = db.query(MovieModel)
total_items = movie_query.count()
movies = movie_query.offset(per_page * (page - 1)).limit(per_page).all()
if not movies:
raise HTTPException(status_code=404, detail="No movies found.")

total_pages = math.ceil(total_items / per_page)

prev_page = f"/theater/movies/?page={page - 1}&per_page={per_page}" if page > 1 else None
next_page = f"/theater/movies/?page={page + 1}&per_page={per_page}" if page < total_pages else None

response = {
"movies": movies,
"prev_page": prev_page,
"next_page": next_page,
"total_pages": total_pages,
"total_items": total_items,
}
return response


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

from pydantic import BaseModel


class MovieBase(BaseModel):
id: int
name: str
date: date
score: float
genre: str
overview: str
crew: str
orig_title: str
status: str
orig_lang: str
budget: float
revenue: float
country: str


class MovieDetailResponseSchema(MovieBase):
pass


class MovieListResponseSchema(BaseModel):
movies: list[MovieDetailResponseSchema]
prev_page: Optional[str]
next_page: Optional[str]
total_pages: int
total_items: int
8 changes: 4 additions & 4 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import pytest
from fastapi.testclient import TestClient

from config import get_settings
from database import (
from src.config import get_settings
from src.database import (
reset_sqlite_database,
get_db_contextmanager,
)
from database.populate import CSVDatabaseSeeder
from main import app
from src.database.populate import CSVDatabaseSeeder
from src.main import app


@pytest.fixture(scope="function", autouse=True)
Expand Down
Loading