Skip to content

Commit

Permalink
add order_by filter to reviewa
Browse files Browse the repository at this point in the history
  • Loading branch information
kenmoh committed Dec 7, 2023
1 parent dd0133f commit 947aaaa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions app/services/movie_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ def get_all_movies(db: session, item_type: TypeEnum):
return movies


def get_highest_rated(db: session):
"""
This function
gets all movies from the database
:param db:
:return: All movies in the database
"""
movies = db.query(Movie).order_by(desc(Movie.created_at)).filter(Movie.average_rating >= 4).all()
for movie in movies:
reviews = get_all_reviews_by_movie(movie.id, db)

if reviews:
average_rating = sum(review.rating for review in reviews) / len(reviews)
movie.average_rating = average_rating
else:
movie.average_rating = 0.0

return movies


def add_movie(movie: AddMovieForm, db: session):
"""
Add new movie to the database service
Expand Down
2 changes: 1 addition & 1 deletion app/services/reviews_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def get_all_reviews(db: session):
try:
return db.query(Review).all()
return db.query(Review).order_by(desc(Review.created_at)).all()
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
Expand Down

0 comments on commit 947aaaa

Please sign in to comment.