Skip to content

Commit

Permalink
add average_rating to Movie model
Browse files Browse the repository at this point in the history
  • Loading branch information
kenmoh committed Dec 2, 2023
1 parent 67a6779 commit 7d86d79
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/models/movie_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Movie(Base):
casts: Mapped[str]
genre: Mapped[str]
thriller: Mapped[str]
average_rating: Mapped[float] = mapped_column(Float, nullable=True)
reviews: Mapped[list["Movie"]] = relationship(
"Review", back_populates="movie", cascade="all, delete-orphan"
)
Expand All @@ -31,7 +32,6 @@ class Review(Base):
author: Mapped[str]
comment: Mapped[str]
rating: Mapped[int]
average_rating: Mapped[float] = mapped_column(Float)
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")
1 change: 1 addition & 0 deletions app/schema/movie_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MovieCreateSchema(BaseModel):
class MovieResponseSchema(MovieCreateSchema):
id: int
cover_image_url: str
average_rating: float | None
reviews: list[ReviewResponseSchema]


Expand Down
1 change: 0 additions & 1 deletion app/schema/review_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ReviewCreateSchema(BaseModel):
class ReviewResponseSchema(ReviewCreateSchema):
id: int
movie_id: int
average_rating: float


class AverageMovieRating(BaseModel):
Expand Down
27 changes: 8 additions & 19 deletions app/services/reviews_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,36 @@ def add_new_review(
.filter(Review.movie_id == movie_id, Review.ip_address == ip_address)
.first()
)
ratings = db.query(Review).filter(Review.movie_id == movie_id).all()

if existing_review:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="You already reviewed this movie!",
)
# try:
# with session.begin():

new_review = Review(
movie_id=movie_id,
author=review.author,
comment=review.comment,
rating=review.rating,
ip_address=ip_address,
average_rating=sum([rating.rating for rating in ratings]) / len(ratings)
if len(ratings) > 0
else review.rating,
)
db.add(new_review)
db.commit()
db.refresh(new_review)

if len(ratings) > 0:
new_review.average_rating = sum([rating.rating for rating in ratings]) / len(
ratings
)
db.commit()
db.refresh(new_review)

return new_review
# except Exception:
# session.rollback()
# raise HTTPException(
# status_code=status.HTTP_400_BAD_REQUEST, detail="You already reviewed this movie...."
# )


def get_all_reviews_by_movie(movie_id, db: session):
try:
return db.query(Review).filter(Review.movie_id == movie_id).all()
reviews = db.query(Review).filter(Review.movie_id == movie_id).all()
average_reviews = (
sum([review.review for review in reviews]) / len(reviews)
if len(reviews) > 0
else sum([review.review for review in reviews])
)
return {"reviews": reviews, "average_reviews": average_reviews}
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
Expand Down

0 comments on commit 7d86d79

Please sign in to comment.