Beispiel #1
0
def get_reviews_for_movie(movie_rank: int, repo: AbstractRepository):
    movie = repo.get_movie_by_rank(movie_rank)

    if movie is None:
        raise ServicesException('Movie does not exist in the repository')

    return repo.get_reviews_for_movie(movie)
Beispiel #2
0
def get_reviews_for_movie(movie_rank, repo: AbstractRepository):
    movie = repo.get_movie(movie_rank)
    if movie is None:
        raise NonExistentMovieException
    reviews = repo.get_reviews()
    review_list = []
    for review in reviews:
        if review.movie == movie:
            review_list.append(review)
    return reviews_to_dict(review_list)
Beispiel #3
0
def get_random_movies(quantity, repo: AbstractRepository):
    movie_count = repo.get_number_of_movies()
    if quantity >= movie_count:
        # Reduce quantity of ranks to generate if repo has insufficient number of movies.
        quantity = movie_count - 1

    # Pick distinct and random movies.
    random_ranks = random.sample(range(1, movie_count), quantity)
    movies = repo.get_movies_by_rank(random_ranks)
    return movies_to_dict(movies)
Beispiel #4
0
def add_user(username: str, password: str, repo: AbstractRepository):
    # Check that the given username is available.
    user = repo.get_user(username)
    if user is not None:
        raise NameNotUniqueException

    # Encrypt password so that the database doesn't store passwords 'in the clear'.
    password_hash = generate_password_hash(password)

    # Create and store the new User, with password encrypted.
    user = User(username, password_hash)
    repo.add_user(user)
Beispiel #5
0
def add_user(user_name: str, password: str, repo: AbstractRepository):
    # Check that the given username is available and not currently used in repository
    user = repo.get_user(user_name)
    if user is not None:
        raise NameNotUniqueException

    # Encrypt password so that the database doesn't store passwords in plain text format.
    password_hash = generate_password_hash(password)

    # Create and store the new User with an encrypted password
    user = User(user_name, password_hash)
    repo.add_user(user)
Beispiel #6
0
def remove_movie_from_watchlist(username: str, movie_title: str,
                                movie_year: int, repo: AbstractRepository):
    # Check user exists
    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    # Check movie exists
    movie = repo.get_movie_by_title_and_year(movie_title, movie_year)
    if movie is None:
        raise NonExistentMovieException

    repo.remove_movie_from_user_watchlist(user, movie)
Beispiel #7
0
def add_review(movie_rank: int, review_text: str, rating: int, username: str, repo: AbstractRepository):
    # Check that the movie exists.
    movie = repo.get_movie(movie_rank)
    if movie is None:
        raise NonExistentMovieException
    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    # Create review.
    review = Review(movie, review_text, rating)

    # Update the repository.
    repo.add_review(review)
Beispiel #8
0
def add_review(movie_title: str, movie_year: int, username: str, review_text: str, rating: int, repo: AbstractRepository):
    # Check movie and user exists
    movie = repo.get_movie_by_title_and_year(movie_title, movie_year)
    if movie is None:
        raise NonExistentMovieException

    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException
    
    # Create review
    review = make_review(user, movie, review_text, rating)

    # Add review
    repo.add_review(review)
Beispiel #9
0
def get_reviews_for_movie(movie_title, movie_year, repo: AbstractRepository):
    movie = repo.get_movie_by_title_and_year(movie_title, movie_year)

    if movie is None:
        raise NonExistentMovieException

    return reviews_to_dict(movie.reviews)
Beispiel #10
0
def get_movie_by_title_and_year(title: str, year: int, repo: AbstractRepository):
    movie = repo.get_movie_by_title_and_year(title, year)

    if movie is None:
        raise NonExistentMovieException
    
    return movie_to_dict(movie)
Beispiel #11
0
def get_user_watchlist(username: str, repo: AbstractRepository):
    # Check user exists
    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    return watchlist_to_dict(user.watchlist)
Beispiel #12
0
def authenticate_user(username: str, password: str, repo: AbstractRepository):
    authenticated = False
    user = repo.get_user(username)
    if user is not None:
        authenticated = check_password_hash(user.password, password)
    if not authenticated:
        raise AuthenticationException
Beispiel #13
0
def create_review(movie_rank: int, review_text: str, rating: int,
                  user_name: str, repo: AbstractRepository):
    # Check that the Movie exists.
    movie = repo.get_movie_by_rank(movie_rank)
    if movie is None:
        raise ServicesException('Movie does not exist in the repository')

    # Check that the user exists.
    user = repo.get_user(user_name)
    if user is None:
        raise ServicesException('User does not exist in the repository')

    # Create review
    review = Review(movie, review_text, rating)

    # Add review to list of user reviews (and consequently to list of Movie reviews)
    user.add_review(review)

    # Update the repository with new Review.
    repo.add_review(review)
Beispiel #14
0
def get_movies_by_genre(genre: str, repo: AbstractRepository):
    movies = repo.get_movies_by_genre(genre)
    return movies_to_dict(movies)
Beispiel #15
0
def get_movies_by_title(title: str, repo: AbstractRepository):
    movies = repo.get_movies_by_title(title)
    return movies_to_dict(movies)
Beispiel #16
0
def get_all_movies(repo: AbstractRepository):
    all_movies = repo.get_all_movies()
    return movies_to_dict(all_movies)
Beispiel #17
0
def get_user(user_name: str, repo: AbstractRepository):
    user = repo.get_user(user_name)
    if user is None:
        raise UnknownUserException

    return user
Beispiel #18
0
def get_movies_by_rank(rank_list: List[int], repo: AbstractRepository):
    movies = repo.get_movies_by_rank(rank_list)
    return movies
Beispiel #19
0
def get_actor(actor_full_name: str, repo: AbstractRepository) -> Actor:
    actor = repo.get_actor(actor_full_name)
    if actor is None:
        raise ServicesException('Actor does not exist in the repository')
    return actor
Beispiel #20
0
def get_review(review_id: int, repo: AbstractRepository) -> Review:
    review = repo.get_review(review_id)
    if review is None:
        raise ServicesException('Review does not exist in the repository')
    return review
Beispiel #21
0
def get_movie_ranks_by_director(director: Director,
                                repo: AbstractRepository) -> List[int]:
    movie_ranks = repo.get_movie_ranks_by_director(director)
    return movie_ranks
Beispiel #22
0
def get_movie_ranks_by_actors(actor_list: List[Actor],
                              repo: AbstractRepository) -> List[int]:
    movie_ranks = repo.get_movie_ranks_by_actors(actor_list)
    return movie_ranks
Beispiel #23
0
def get_movie_ranks_by_genres(genre_list: List[Genre],
                              repo: AbstractRepository) -> List[int]:
    movie_ranks = repo.get_movie_ranks_by_genres(genre_list)
    return movie_ranks
Beispiel #24
0
def get_movies(repo: AbstractRepository):
    return repo.get_movies()
Beispiel #25
0
def get_movies_by_director(director_full_name: str, repo: AbstractRepository):
    movies = repo.get_movies_by_director(director_full_name)
    return movies_to_dict(movies)
Beispiel #26
0
def get_most_common_actor_names(quantity: int,
                                repo: AbstractRepository) -> List[str]:
    actors = repo.get_most_common_actors(quantity)
    actor_names = [actor.actor_full_name for actor in actors]

    return actor_names
Beispiel #27
0
def get_genre_names(repo: AbstractRepository):
    genres = repo.get_genres()
    genre_names = [genre.genre_name for genre in genres]
    return genre_names
Beispiel #28
0
def get_most_common_genre_names(quantity: int,
                                repo: AbstractRepository) -> List[str]:
    genres = repo.get_most_common_genres(quantity)
    genre_names = [genre.genre_name for genre in genres]

    return genre_names
Beispiel #29
0
def get_movie_ranks_by_release_year(release_year: int,
                                    repo: AbstractRepository) -> List[int]:
    movie_ranks = repo.get_movie_ranks_by_release_year(release_year)
    return movie_ranks
Beispiel #30
0
def get_most_common_director_names(quantity: int,
                                   repo: AbstractRepository) -> List[str]:
    directors = repo.get_most_common_directors(quantity)
    director_names = [director.director_full_name for director in directors]

    return director_names