Skip to content

Commit

Permalink
fixed reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
kenmoh committed Dec 1, 2023
1 parent 74921cb commit b596531
Show file tree
Hide file tree
Showing 1,635 changed files with 247 additions and 613,458 deletions.
1 change: 1 addition & 0 deletions .idea/ruff.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Settings(BaseSettings):
AWSSecretKey: str

class Config:
env_file = '.env'
env_file = ".env"


settings = Settings()
1 change: 0 additions & 1 deletion app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ def get_db():
yield db
finally:
db.close()

19 changes: 10 additions & 9 deletions app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@


class AddMovieForm:
def __init__(self,
length: float = Form(...),
title: str = Form(...),
descr: str = Form(...),
casts: str = Form(...),
genre: str = Form(...),
thriller: str = Form(...),
image: UploadFile = Depends(add_image)
):
def __init__(
self,
length: float = Form(...),
title: str = Form(...),
descr: str = Form(...),
casts: str = Form(...),
genre: str = Form(...),
thriller: str = Form(...),
image: UploadFile = Depends(add_image),
):
self.title = title
self.length = length
self.descr = descr
Expand Down
14 changes: 9 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

from app.models import movie_model
from app.database import engine
from app.routes import movie_routes
from app.routes import movie_routes, review

movie_model.Base.metadata.create_all(bind=engine)

app = FastAPI(title='MoviesAPI', description='A Simple Movie Review REST API to demonstrate microservices')
app = FastAPI(
title="MoviesAPI",
description="A Simple Movie Review REST API to demonstrate microservices",
)

origins = [
"http://localhost",
Expand All @@ -24,10 +27,11 @@
allow_headers=["*"],
)

@app.get('/')
async def health_check():
return {'health_status': status.HTTP_200_OK}

@app.get("/")
async def health_check():
return {"health_status": status.HTTP_200_OK}


app.include_router(movie_routes.movie_router)
app.include_router(review.review_router)
12 changes: 7 additions & 5 deletions app/models/movie_model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from decimal import Decimal
from sqlalchemy import String, Integer, DECIMAL, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.orm import Mapped, relationship, mapped_column

from app.database import Base


class Movie(Base):
__tablename__ = 'movies'
__tablename__ = "movies"

id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
title: Mapped[str] = mapped_column(String(50))
Expand All @@ -16,19 +16,21 @@ class Movie(Base):
casts: Mapped[str]
genre: Mapped[str]
thriller: Mapped[str]
reviews: Mapped[list['Movie']] = relationship("Review", back_populates="movie", cascade="all, delete-orphan")
reviews: Mapped[list["Movie"]] = relationship(
"Review", back_populates="movie", cascade="all, delete-orphan"
)

def __str__(self):
return self.title


class Review(Base):
__tablename__ = 'reviews'
__tablename__ = "reviews"

id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
author: Mapped[str]
comment: Mapped[str]
rating: Mapped[int]
ip_address: Mapped[str]
ip_address: Mapped[str] = mapped_column(String(255))
movie_id: Mapped[int] = mapped_column(ForeignKey("movies.id"), nullable=False)
movie = relationship(Movie, back_populates="reviews")
155 changes: 82 additions & 73 deletions app/routes/movie_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,130 @@

from app.database import get_db
from app.forms import AddMovieForm
from app.schema.movie_schema import MovieResponseSchema, ReviewResponseSchema, ReviewCreateSchema, AverageMovieReview
from app.schema.movie_schema import (
MovieResponseSchema,
ReviewResponseSchema,
ReviewCreateSchema,
AverageMovieReview,
)
from app.services import movie_services

from app.services import services


movie_router = APIRouter(tags=['Movies'], prefix='/api/movies')
REVIEW_URL = 'https://reviewapi.onrender.com/api/reviews'
movie_router = APIRouter(tags=["Movies"], prefix="/api/movies")
REVIEW_URL = "https://reviewapi.onrender.com/api/reviews"

""" START MOVIE ROUTE"""


@movie_router.get('', status_code=status.HTTP_200_OK)
@movie_router.get("", status_code=status.HTTP_200_OK)
def get_movies(db: Session = Depends(get_db)) -> list[MovieResponseSchema]:
"""
Get all movies from the database
:param db:
:return: All Movies
"""
return services.get_all_movies(db)
return movie_services.get_all_movies(db)


@movie_router.post('', status_code=status.HTTP_201_CREATED)
def add_new_movie(movie: AddMovieForm = Depends(), db: Session = Depends(get_db)) -> (
MovieResponseSchema):
@movie_router.post("", status_code=status.HTTP_201_CREATED)
def add_new_movie(
movie: AddMovieForm = Depends(), db: Session = Depends(get_db)
) -> MovieResponseSchema:
"""
Add new movie to the database
"""

return services.add_movie(movie, db)
return movie_services.add_movie(movie, db)


@movie_router.put('/{movie_id}', status_code=status.HTTP_202_ACCEPTED)
def update_movie(movie_id, movie: AddMovieForm = Depends(), db: Session = Depends(get_db)) -> MovieResponseSchema:
@movie_router.put("/{movie_id}", status_code=status.HTTP_202_ACCEPTED)
def update_movie(
movie_id, movie: AddMovieForm = Depends(), db: Session = Depends(get_db)
) -> MovieResponseSchema:
"""
Update a movie by it ID
"""

return services.update_movie(movie_id, movie, db)
return movie_services.update_movie(movie_id, movie, db)


@movie_router.get('/{movie_id}', status_code=status.HTTP_200_OK)
@movie_router.get("/{movie_id}", status_code=status.HTTP_200_OK)
def get_movie(movie_id, db: Session = Depends(get_db)) -> MovieResponseSchema:
"""
Get a single movie from the database
"""
return services.get_movie(movie_id, db)

return movie_services.get_movie(movie_id, db)


@movie_router.delete('/{movie_id}', status_code=status.HTTP_204_NO_CONTENT)
@movie_router.delete("/{movie_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_movie(movie_id: int, db: Session = Depends(get_db)):
"""
Delete a movie from database
"""
requests.delete(f'{REVIEW_URL}/delete-reviews/{movie_id}')
return services.delete_movie(movie_id, db)
requests.delete(f"{REVIEW_URL}/delete-reviews/{movie_id}")
return movie_services.delete_movie(movie_id, db)


""" END MOVIE ROUTE """

""" START REVIEW ROUTE """


@movie_router.get('/reviews/{movie_id}', status_code=status.HTTP_200_OK)
def get_movie_reviews(movie_id: int) -> list[ReviewResponseSchema]:
"""
Get all reviews by a movie
"""
response = requests.get(f'{REVIEW_URL}/{movie_id}')
data = response.json()
return data


@movie_router.post('/reviews/{movie_id}', status_code=status.HTTP_201_CREATED)
def add_review(movie_id: int, review: ReviewCreateSchema, db: Session = Depends(get_db)) -> ReviewResponseSchema:
"""
Add review to a movie
:param movie_id:
:param review:
:param db:
:return: New Review
"""
movie = services.get_movie(movie_id, db)
data = {
"comment": review.comment, "author": review.author, 'rating': review.rating
}
if not movie:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f'Movie with ID: {movie_id} not found!')
response = requests.post(f'{REVIEW_URL}/{movie_id}', json=data)
data = response.json()
return data


@movie_router.delete('/reviews/{review_id}', status_code=status.HTTP_204_NO_CONTENT)
def delete_review(review_id: int):
"""
Delete movie review
:param review_id:
:return: None
"""
response = requests.delete(f'{REVIEW_URL}/delete-review/{review_id}')
return response


@movie_router.get('/average-rating/{movie_id}', status_code=status.HTTP_200_OK)
def get_avg_movie_rating(movie_id: int) -> AverageMovieReview:
"""
Get the average rating of a movie
:param movie_id:
:return: average movie rating (float)
"""
response = requests.get(f'{REVIEW_URL}/average-rating/{movie_id}')
avg_rating = response.json()
return avg_rating
#
# @movie_router.get("/reviews/{movie_id}", status_code=status.HTTP_200_OK)
# def get_movie_reviews(movie_id: int) -> list[ReviewResponseSchema]:
# """
# Get all reviews by a movie
# """
# response = requests.get(f"{REVIEW_URL}/{movie_id}")
# data = response.json()
# return data
#
#
# @movie_router.post("/reviews/{movie_id}", status_code=status.HTTP_201_CREATED)
# def add_review(
# movie_id: int, review: ReviewCreateSchema, db: Session = Depends(get_db)
# ) -> ReviewResponseSchema:
# """
# Add review to a movie
# :param movie_id:
# :param review:
# :param db:
# :return: New Review
# """
# movie = movie_services.get_movie(movie_id, db)
# data = {"comment": review.comment, "author": review.author, "rating": review.rating}
# if not movie:
# raise HTTPException(
# status_code=status.HTTP_404_NOT_FOUND,
# detail=f"Movie with ID: {movie_id} not found!",
# )
# response = requests.post(f"{REVIEW_URL}/{movie_id}", json=data)
# data = response.json()
# return data
#
#
# @movie_router.delete("/reviews/{review_id}", status_code=status.HTTP_204_NO_CONTENT)
# def delete_review(review_id: int):
# """
# Delete movie review
# :param review_id:
# :return: None
# """
# response = requests.delete(f"{REVIEW_URL}/delete-review/{review_id}")
# return response
#
#
# @movie_router.get("/average-rating/{movie_id}", status_code=status.HTTP_200_OK)
# def get_avg_movie_rating(movie_id: int) -> AverageMovieReview:
# """
# Get the average rating of a movie
# :param movie_id:
# :return: average movie rating (float)
# """
# response = requests.get(f"{REVIEW_URL}/average-rating/{movie_id}")
# avg_rating = response.json()
# return avg_rating


""" END REVIEW ROUTE """


Loading

0 comments on commit b596531

Please sign in to comment.