def create_a_new_review(place_id):
    """ create new resource by my list of place """

    place = storage.get(Place, place_id)
    if place is None:
        return jsonify({'Error': 'Not found'}), 404

    review_object_json = request.get_json()
    if review_object_json is None:
        return jsonify({'Error': 'Not a JSON'}), 400

    if 'user_id' not in review_object_json:
        return jsonify({'Error': 'Missing user_id'}), 400

    user_id = review_object_json.get('user_id')

    user = storage.get(User, user_id)
    if user is None:
        return jsonify({'Error': 'Not found'}), 404

    if 'text' not in review_object_json:
        return jsonify({'Error': 'Missing text'}), 400

    review_object_json['place_id'] = place_id
    instance = Review(**review_object_json)
    instance.save()

    return jsonify(instance.to_dict()), 201
Beispiel #2
0
def create_new_review(place_id=None):
    """ create new resource by my list of reviews """

    place_object = storage.get(Place, place_id)
    if place_object is None:
        abort(404, "Not found")

    place_object_json = request.get_json()
    if place_object_json is None:
        abort(400, "Not a JSON")

    # looking for a "user_id" value for creating a object place
    user_id = place_object_json.get("user_id")
    if user_id is None:
        abort(400, 'Missing user_id')

    # creating a new user object as attribute of place object
    user_object = storage.get(User, user_id)
    if user_object is None:
        abort(404, "Not found")

    # verify if exist "name" value into POST request
    if place_object_json.get("text") is None:
        abort(400, 'Missing text')

    # If the place_id is not linked to any City object, raise a 404 error
    # this will be never raise a 404 error
    place_object_json["place_id"] = place_id

    review_object = Review(**place_object_json)
    review_object.save()

    return jsonify(review_object.to_dict()), 201
def post_review(place_id=None):
    """ Post a review in a place with an id """
    body = request.get_json(silent=True)
    if not body:
        return make_response(jsonify({'error': 'Not a JSON'}), 400)
    if 'user_id' not in body:
        return make_response(jsonify({'error': 'Missing user_id'}), 400)
    if 'text' not in body:
        return make_response(jsonify({'error': 'Missing text'}), 400)
    place = storage.get(Place, place_id)
    user = storage.get(User, body['user_id'])
    if place and user:
        new_review = Review(**body)
        new_review.place_id = place.id
        storage.new(new_review)
        storage.save()
        return make_response(jsonify(new_review.to_dict()), 201)
    return abort(404)
def create_review(place_id):
    '''This is the 'create_review' method.

    Creates a Review object of a given place.
    '''
    try:
        r = request.get_json()
    except:
        r = None

    if r is None:
        return "Not a JSON", 400
    if "user_id" not in r.keys():
        return "Missing user_id", 400
    if "text" not in r.keys():
        return "Missing text", 400

    place = storage.get("Place", place_id)
    if place is None:
        abort(404)

    user = storage.get("User", r["user_id"])
    if user is None:
        abort(404)

    review = Review(**r)
    review.place_id = place_id
    review.save()
    return jsonify(review.to_json()), 201
def create_review(places_id):
    """Creates a new place"""
    place = storage.get(Place, places_id)
    if not bool(place):
        return abort(404)

    body = request.get_json(silent=True)
    if body is None:
        return jsonify({'error': 'Not a JSON'}), 400

    if 'user_id' not in body:
        return jsonify({'error': 'Missing user_id'}), 400
    user = storage.get(User, body.get('user_id'))
    if not bool(user):
        return abort(404)

    if 'text' in body:
        new_review = Review(**body)
        setattr(new_review, 'place_id', place.id)
        storage.new(new_review)
        storage.save()
        return jsonify(new_review.to_dict()), 201
    else:
        return jsonify({'error': 'Missing text'}), 400
Beispiel #6
0
def create_review(place_id):
    """
    creates one review
    """
    try:
        r = request.get_json()
    except:
        r = None
    if r is None:
        return "Not a JSON", 400
    if "user_id" not in r.keys():
        return "Missing user_id", 400
    if "text" not in r.keys():
        return "Missing text", 400
    place = storage.get("Place", place_id)
    if place is None:
        abort(404)
    user = storage.get("User", r["user_id"])
    if user is None:
        abort(404)
    review = Review(**r)
    review.place_id = place_id
    review.save()
    return jsonify(review.to_json()), 201
def create_review(place_id):
    """Example endpoint creates one review
    Creates one review associated with a place_id based on the JSON body
    ---
    parameters:
      - name: place_id
        in: path
        type: string
        enum: ["3f54d114-582d-4dab-8559-f0682dbf1fa6"]
        required: true
        default: "3f54d114-582d-4dab-8559-f0682dbf1fa6"
    definitions:
      State:
        type: object
        properties:
          __class__:
            type: string
            description: The string of class object
          created_at:
            type: string
            description: The date the object created
          id:
            type: string
            description: the id of the review
          place_id:
            type: string
            description: the id of the place
          text:
            type: string
            description: written review
          updated_at:
            type: string
            description: The date the object was updated
          user_id:
            type: string
            description: The user id
            items:
              $ref: '#/definitions/Color'
      Color:
        type: string
    responses:
      201:
        description: A list of a dictionary of a Review objects
        schema:
          $ref: '#/definitions/State'
        examples:
            [{"__class__": "Review",
              "created_at": "2017-03-25T02:17:07",
              "id": "3f54d114-582d-4dab-8559-f0682dbf1fa6",
              "place_id": "279b355e-ff9a-4b85-8114-6db7ad2a4cd2",
              "text": "Really nice place and really nice people. Secluded.",
              "updated_at": "2017-03-25T02:17:07",
              "user_id": "887dcd8d-d5ee-48de-9626-73ff4ea732fa"}]
    """
    try:
        r = request.get_json()
    except:
        r = None
    if r is None:
        return "Not a JSON", 400
    if "user_id" not in r.keys():
        return "Missing user_id", 400
    if "text" not in r.keys():
        return "Missing text", 400
    place = storage.get("Place", place_id)
    if place is None:
        abort(404)
    user = storage.get("User", r["user_id"])
    if user is None:
        abort(404)
    review = Review(**r)
    review.place_id = place_id
    review.save()
    return jsonify(review.to_json()), 201
def create_review(place_id):
    """Example endpoint creates one review
    Creates one review associated with a place_id based on the JSON body
    ---
    parameters:
      - name: place_id
        in: path
        type: string
        enum: ["3f54d114-582d-4dab-8559-f0682dbf1fa6"]
        required: true
        default: "3f54d114-582d-4dab-8559-f0682dbf1fa6"
    definitions:
      State:
        type: object
        properties:
          __class__:
            type: string
            description: The string of class object
          created_at:
            type: string
            description: The date the object created
          id:
            type: string
            description: the id of the review
          place_id:
            type: string
            description: the id of the place
          text:
            type: string
            description: written review
          updated_at:
            type: string
            description: The date the object was updated
          user_id:
            type: string
            description: The user id
            items:
              $ref: '#/definitions/Color'
      Color:
        type: string
    responses:
      201:
        description: A list of a dictionary of a Review objects
        schema:
          $ref: '#/definitions/State'
        examples:
            [{"__class__": "Review",
              "created_at": "2017-03-25T02:17:07",
              "id": "3f54d114-582d-4dab-8559-f0682dbf1fa6",
              "place_id": "279b355e-ff9a-4b85-8114-6db7ad2a4cd2",
              "text": "Really nice place and really nice people. Secluded.",
              "updated_at": "2017-03-25T02:17:07",
              "user_id": "887dcd8d-d5ee-48de-9626-73ff4ea732fa"}]
    """
    try:
        r = request.get_json()
    except:
        r = None
    if r is None:
        return "Not a JSON", 400
    if "user_id" not in r.keys():
        return "Missing user_id", 400
    if "text" not in r.keys():
        return "Missing text", 400
    place = storage.get("Place", place_id)
    if place is None:
        abort(404)
    user = storage.get("User", r["user_id"])
    if user is None:
        abort(404)
    review = Review(**r)
    review.place_id = place_id
    review.save()
    return jsonify(review.to_json()), 201