Skip to content

Commit

Permalink
remove average rating field
Browse files Browse the repository at this point in the history
  • Loading branch information
kenmoh committed Dec 1, 2023
1 parent 8cf8f54 commit 2efd13b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/models/movie_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from decimal import Decimal
from sqlalchemy import String, Integer, DECIMAL, ForeignKey
from sqlalchemy import String, Integer, DECIMAL, ForeignKey, Float
from sqlalchemy.orm import Mapped, relationship, mapped_column

from app.database import Base
Expand Down Expand Up @@ -31,6 +31,7 @@ class Review(Base):
author: Mapped[str]
comment: Mapped[str]
rating: Mapped[int]
average_rating: Mapped[float] = mapped_column(Float, 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: 1 addition & 1 deletion app/schema/review_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ReviewCreateSchema(BaseModel):
class ReviewResponseSchema(ReviewCreateSchema):
id: int
movie_id: int

average_rating: float | None


class AverageMovieRating(BaseModel):
Expand Down
5 changes: 5 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 @@ -37,6 +38,10 @@ def add_new_review(
db.commit()
db.refresh(new_review)

new_review.average_raring = sum([rating.rating for rating in ratings] if len(ratings) > 0 else 0)
db.commit()
db.refresh(new_review)

return new_review
except Exception:
session.rollback()
Expand Down

0 comments on commit 2efd13b

Please sign in to comment.