Skip to content

Commit

Permalink
add average_rating field
Browse files Browse the repository at this point in the history
  • Loading branch information
kenmoh committed Dec 1, 2023
1 parent b596531 commit af8dc65
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/movie_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Review(Base):
author: Mapped[str]
comment: Mapped[str]
rating: Mapped[int]
average_raring: Mapped[float] = mapped_column(Integer, nullable=True)
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")
2 changes: 2 additions & 0 deletions app/services/reviews_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def add_new_review(
movie_id: int, ip_address: str, review: ReviewCreateSchema, db: session
):
existing_review = db.query(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(
Expand All @@ -31,6 +32,7 @@ def add_new_review(
comment=review.comment,
rating=review.rating,
ip_address=ip_address,
average_raring=sum([[rating.rating for rating in ratings] if len(ratings) > 0 else 0])
)
db.add(new_review)
db.commit()
Expand Down

0 comments on commit af8dc65

Please sign in to comment.