Exemplo n.º 1
0
def create_evaluation(movie_id):
    """
    First calls the Movies service to check if the movie exists.  
    Then add an evaluation to a movie.
    :param movie_id:
    :param request.json: a dictionnary containing a field
    'description.

    :return: if success the created Evaluation object
    """

    requested_fields = {'description'}
    included_fields = set(request.json.keys())
    if not request.json or not ut.test_intersection(requested_fields,
                                                    included_fields):
        abort(400)
    description = request.json['description']
    try:
        response = requests.get(
            "http://127.0.0.1:5000/movies/{}".format(movie_id))
        if response.status_code != 200:
            return make_response(jsonify({"error": "Movie not found"}), 404)
        evaluation = ut.add_evaluation(description, movie_id)
        return make_response(jsonify({"evaluation": evaluation.serialize()}),
                             201)

    except requests.exceptions.ConnectionError:
        # raise ServiceUnavailable("The Movies service is unavailable.")
        return make_response(
            jsonify({"error": "The Movies service is unavailable."}), 503)
Exemplo n.º 2
0
def update_evaluation(evaluation_id):
    """
    Updates an evaluation based on its id.
    :param evaluation_id:
    :param request.json: a dictionnary containing a field
    'description.
    :return: if success the updated Evaluation object
    """

    requested_fields = {'description'}
    included_fields = set(request.json.keys())

    evaluation = ut.get_evaluation(evaluation_id)
    if evaluation is None:
        abort(404)

    if not request.json:
        abort(400)

    if not ut.test_intersection(requested_fields, included_fields):
        abort(400)

    description = request.json['description']

    included_fields = set(request.json.keys())
    evaluation = ut.update_evaluation_by_id(evaluation_id, description)
    return make_response(jsonify(evaluation.serialize()))
Exemplo n.º 3
0
def update_book(book_id):
    """
    Updates a book based on its id.
    :param book_id:
    :param request.json: a dictionnary containing fields
    'name' and 'year.
    :return: if success the updated Book object
    """
    requested_fields = {'name', 'year'}
    included_fields = set(request.json.keys())

    book = ut.get_book(book_id)
    if book is None:
        abort(404)

    if not request.json:
        abort(400)

    if not ut.test_intersection(requested_fields, included_fields):
        abort(400)

    book_name = request.json['name']
    book_year = request.json['year']

    included_fields = set(request.json.keys())
    book = ut.update_book_by_id(book_id, book_name, book_year)
    return make_response(jsonify(book.serialize()))
Exemplo n.º 4
0
def update_movie(movie_id):
    """
    Updates a movie based on its id.
    :param movie_id:
    :param request.json: a dictionnary containing fields
    'name' and 'year.
    :return: if success the updated Movie object
    """
    requested_fields = {'name', 'year'}
    included_fields = set(request.json.keys())

    movie = ut.get_movie(movie_id)
    if movie is None:
        abort(404)

    if not request.json:
        abort(400)

    if not ut.test_intersection(requested_fields,included_fields):
        abort(400)

    movie_name = request.json['name']
    movie_year = request.json['year']

    included_fields = set(request.json.keys())
    movie = ut.update_movie_by_id(movie_id,movie_name,movie_year)
    return make_response(jsonify(movie.serialize()))
Exemplo n.º 5
0
def create_movie():
    """ 
    Create a movie object.
    :param movie_id:
    :param request.json: a dictionnary containing fields
    'name' and 'year.

    :return: if success the created Movie object
    """
    requested_fields = {'name', 'year'}
    included_fields = set(request.json.keys())
    if not request.json or not ut.test_intersection(requested_fields,included_fields):
        abort(400)
    movie_name = request.json['name']
    movie_year = request.json['year']

    movie = ut.add_movie(movie_name, movie_year)
    return make_response(jsonify({"movie": movie.serialize()}),201)
Exemplo n.º 6
0
def create_book():
    """ 
    Create a book object.
    :param book_id:
    :param request.json: a dictionnary containing fields
    'name' and 'year.

    :return: if success the created Book object
    """
    requested_fields = {'name', 'year'}
    included_fields = set(request.json.keys())
    if not request.json or not ut.test_intersection(requested_fields,
                                                    included_fields):
        abort(400)
    book_name = request.json['name']
    book_year = request.json['year']

    book = ut.add_book(book_name, book_year)
    return make_response(jsonify({"book": book.serialize()}), 201)