Skip to content

Commit

Permalink
Solution-py-fastapi-homework-1-task
Browse files Browse the repository at this point in the history
  • Loading branch information
skyfoxwork committed Jan 17, 2025
1 parent 9aa1b96 commit bae2368
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fastapi import FastAPI

from routes import movie_router
# from routes import movies

app = FastAPI(
title="Movies homework",
Expand All @@ -10,3 +11,4 @@
api_version_prefix = "/api/v1"

app.include_router(movie_router, prefix=f"{api_version_prefix}/theater", tags=["theater"])
# app.include_router(movies.router)
36 changes: 34 additions & 2 deletions src/routes/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 movie_list_schema:
raise HTTPException(status_code=404, detail="No movies found.")

return MovieListResponseSchema(
movies=movies_with_pagination,
total_items=total_items,
total_pages=total_pages,
prev_page=None if page == 1 else prev_page,
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
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 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
next_page: str | None = None
total_pages: int
total_items: int

0 comments on commit bae2368

Please sign in to comment.