Beispiel #1
0
def update(movie_id, movie):
    """
    This function updates an existing movie in the MOVIE structure

    :param movie_id:    Id of movie to update in the MOVIE structure
    :param movie:       movie to update
    :return:            updated movie structure
    """

    # Get the person requested from the db into session
    update_movie = Movie.query.filter(Movie.movie_id == movie_id).one_or_none()

    # Try to find an existing movie with the same name as the update
    name = movie.get("name")

    existing_movie = (Movie.query.filter(Movie.name == name).one_or_none())

    # Are we trying to find a movie that does not exist?
    if update_movie is None:
        abort(
            404,
            "Movie not found for Id: {movie_id}".format(movie_id=movie_id),
        )

    # Would our update create a duplicate of another person already existing?
    elif (existing_movie is not None and existing_movie.movie_id != movie_id):
        abort(
            409,
            "Movie \"{name}\" exists already".format(name=name),
        )

    # Otherwise go ahead and update!
    else:

        # turn the passed in person into a db object
        schema = MovieSchema()
        update = schema.load(movie, session=db.session)  #.data

        # Set the id to the person we want to update
        update.movie_id = update_movie.movie_id

        # merge the new object into the old and commit it to the db
        db.session.merge(update)
        db.session.commit()

        # return updated person in the response
        data = schema.dump(update_movie)  #.data

        return data, 200
Beispiel #2
0
def create(movie):
    """
    This function creates a new movie in the MOVIE structure
    based on the passed in movie data

    :param movie:   movie to create in MOVIE structure
    :return:        201 on success, 406 on movie exists
    """
    popularity = movie.get("popularity", None)
    director = movie.get("director", None)
    genre = movie.get("genre", None)
    imdb_score = movie.get("imdb_score", None)
    name = movie.get("name", None)

    existing_movie = (Movie.query.filter(Movie.name == name).one_or_none())
    if existing_movie is None:
        # Create a person instance using the schema and the passed in person
        schema = MovieSchema()
        new_movie = schema.load(movie, session=db.session)  #.data
        # Add the person to the database
        db.session.add(new_movie)
        db.session.commit()
        # Serialize and return the newly created person in the response
        data = schema.dump(new_movie)  #.data
        return data, 201

    # Does the movie exist already?
    if name not in MOVIE and name is not None:
        MOVIE[name] = {
            "popularity": popularity,
            "director": director,
            "genre": genre,
            "imdb_score": imdb_score,
            "name": name,
        }
        return MOVIE[name], 201

    # Otherwise, they exist, that's an error
    else:
        abort(
            406,
            "Movie with name {name} already exists".format(name=name),
        )