diff --git a/app/services/movie_services.py b/app/services/movie_services.py index 26b52ba..a6d71a8 100644 --- a/app/services/movie_services.py +++ b/app/services/movie_services.py @@ -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 diff --git a/app/services/reviews_service.py b/app/services/reviews_service.py index 13abdcf..c23f462 100644 --- a/app/services/reviews_service.py +++ b/app/services/reviews_service.py @@ -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)