def get_movie_reviews(args): movie = Movie.query.get(args['movie_id']) if not movie: return error_response(400, 'Movie does not exist') query = db.session.query(Review, User) \ .join(User) \ .filter(Review.movie_id == args['movie_id']) \ .order_by(Review.timestamp) pagination = paginate(query, page=args['page'], per_page=10) response = { 'items': [{ 'author': user.username, 'body': review.body, 'timestamp': review.timestamp } for review, user in pagination.items], 'page_size': 10, 'current_page': pagination.page, 'total_pages': pagination.pages, 'total_results': pagination.total } return success_response(**response)
def get_movie_recommendations(args): movie = Movie.query.get(args['movie_id']) if not movie: return error_response(400, 'Movie does not exist') query = db.session.query(Recommendation, User, Movie) \ .join(User, Recommendation.author_id == User.id) \ .join(Movie, Recommendation.recommendation_to == Movie.id) \ .filter(Recommendation.recommendation_from == movie.id) \ .order_by(Recommendation.timestamp) pagination = paginate(query, page=args['page'], per_page=10) response = { 'items': [{ 'author': user.username, 'body': recommendation.body, 'movie_title': movie.title, 'poster_path': movie.poster_path, 'movie_id': movie.id } for recommendation, user, movie in pagination.items], 'page_size': 10, 'current_page': pagination.page, 'total_pages': pagination.pages, 'total_results': pagination.total } return success_response(**response)
def get_user_recommendations(args): user = User.query.get(args['user_id']) if not user: return error_response(400, 'User does not exist') movie_from = aliased(Movie) movie_to = aliased(Movie) query = db.session.query(Recommendation, movie_from, movie_to) \ .join(movie_from, Recommendation.recommendation_from == movie_from.id) \ .join(movie_to, Recommendation.recommendation_to == movie_to.id) \ .filter(Recommendation.author_id == user.id) \ .order_by(Recommendation.timestamp) pagination = paginate(query, page=args['page'], per_page=10) response = { 'items': [{'body': recommendation.body, 'movie_from_title': movie_from.title, 'movie_from_poster_path': movie_from.poster_path, 'movie_from_id': movie_from.id, 'movie_to_title': movie_to.title, 'movie_to_poster_path': movie_to.poster_path, 'movie_to_id': movie_to.id} for recommendation, movie_from, movie_to in pagination.items], 'page_size': 10, 'current_page': pagination.page, 'total_pages': pagination.pages, 'total_results': pagination.total } return success_response(**response)
def get_user_comments(args): user = User.query.get(args['user_id']) if not user: return error_response(400, 'User does not exist') query = db.session.query(Comment, User) \ .join(User, Comment.host_id == User.id) \ .filter(Comment.host_id == user.id) \ .order_by(Comment.timestamp) pagination = paginate(query, page=args['page'], per_page=10) response = { 'items': [{'author': user.username, 'body': comment.body, 'timestamp': comment.timestamp, 'author_id': user.id} for comment, user in pagination.items], 'page_size': 10, 'current_page': pagination.page, 'total_pages': pagination.pages, 'total_results': pagination.total } return success_response(**response)
def get_user_reviews(args): user = User.query.get(args['user_id']) if not user: return error_response(400, 'User does not exist') query = db.session.query(Review, Movie) \ .join(Movie) \ .filter(Review.author_id == user.id) \ .order_by(Review.timestamp) pagination = paginate(query, page=args['page'], per_page=10) response = { 'items': [{'body': review.body, 'timestamp': review.timestamp, 'movie_title': movie.title, 'movie_id': movie.id} for review, movie in pagination.items], 'page_size': 10, 'current_page': pagination.page, 'total_pages': pagination.pages, 'total_results': pagination.total } return success_response(**response)