Ejemplo n.º 1
0
def restaurant_update(rest_id):
    if not requires_scope('update:restaurants'):
        raise AuthError({"success": False,
                         "message": "No access to this resource"}, 403)

    rest, ret_val, http_status = retrieve_restaurant(rest_id)
    if ret_val:  # Something went awry
        return jsonify(ret_val), http_status

    json_dict = None
    try:
        json_dict = request.json
    except werkzeug_exc.BadRequest as ex:
        logging.error('Could not decode the json content')
        logging.error(str(ex))
        ret_val = {'success': False, 'id': rest.id, 'restaurant': None, 'message': str(ex),
                   'api_version': API_VERSION
                  }
        return jsonify(ret_val), 400

    rest.name = get_dict_value_trunc(json_dict, 'name', rest.name)
    rest.street = get_dict_value_trunc(json_dict, 'street', rest.street)
    rest.suite = get_dict_value_trunc(json_dict, 'suite', rest.suite)
    rest.city = get_dict_value_trunc(json_dict, 'city', rest.city)
    rest.state = get_dict_value_trunc(json_dict, 'state', rest.state)
    rest.zip_code = get_dict_value_trunc(json_dict, 'zip_code', rest.zip_code)
    rest.phone_num = get_dict_value_trunc(json_dict, 'phone_num', rest.phone_num)
    rest.website = get_dict_value_trunc(json_dict, 'website', rest.website)
    rest.email = get_dict_value_trunc(json_dict, 'email', rest.email)
    rest.date_established = get_dict_value_trunc(json_dict, 'date_established', rest.date_established)

    # Restaurant name may not be blank
    if not rest.name:
        ret_val = {'success': False, 'id': rest.id, 'restaurant': None,
                   'message': f'Name of restaurant may not be blank',
                   'api_version': API_VERSION
                  }
        return jsonify(ret_val), 400

    db.session.add(rest)
    db.session.commit()

    # Retrieve the entire restaurant from the database and return it
    # in the response.
    rest, ret_val, http_status = retrieve_restaurant(rest_id)
    rest_item = restaurant_to_dict(rest)
    ret_val = {'success': True,
               'id': rest.id,
               'message': f"Restaurant updated: {rest.name}",
               'restaurant': rest_item,
               'api_version': API_VERSION
               }
    return jsonify(ret_val), 200
Ejemplo n.º 2
0
def restaurant_create():
    if not requires_scope('create:restaurants'):
        raise AuthError({"success": False,
                         "message": "No access to this resource"}, 403)

    json_dict = None
    try:
        json_dict = request.json
    except werkzeug_exc.BadRequest as ex:
        logging.error('Could not decode the json content')
        logging.error(str(ex))
        ret_val = {'success': False, 'id': None, 'message': str(ex), 'api_version': API_VERSION}
        return jsonify(ret_val), 400

    name = get_dict_value_trunc(json_dict, 'name')  # name is required, validate this
    if not name:
        ret_val = {'success': False, 'id': None, 'message': f'Name of restaurant is required',
                   'api_version': API_VERSION
                  }
        return jsonify(ret_val), 400

    creator = get_dict_value_trunc(json_dict, 'creator')  # creator is required, validate this
    if not creator:
        ret_val = {'success': False, 'id': None, 'message': f'Creator of restaurant is required',
                   'api_version': API_VERSION
                  }
        return jsonify(ret_val), 400

    rest = Restaurant(name=name, creator=creator)

    rest.street = get_dict_value_trunc(json_dict, 'street')
    rest.suite = get_dict_value_trunc(json_dict, 'suite')
    rest.city = get_dict_value_trunc(json_dict, 'city')
    rest.state = get_dict_value_trunc(json_dict, 'state')
    rest.zip_code = get_dict_value_trunc(json_dict, 'zip_code')
    rest.phone_num = get_dict_value_trunc(json_dict, 'phone_num')
    rest.website = get_dict_value_trunc(json_dict, 'website')
    rest.email = get_dict_value_trunc(json_dict, 'email')
    rest.date_established = get_dict_value_trunc(json_dict, 'date_established')

    db.session.add(rest)
    db.session.commit()

    rest_item = restaurant_to_dict(rest)
    ret_val = {'success': True,
               'id': rest.id,
               'message': f'Restaurant created with name: {rest.name}',
               'restaurant': rest_item,
               'api_version': API_VERSION
               }
    return jsonify(ret_val), 200
Ejemplo n.º 3
0
def restaurant_by_id(rest_id):
    if not requires_scope('read:restaurants'):
        raise AuthError({"success": False,
                         "message": "No access to this resource"}, 403)

    rest, ret_val, http_status = retrieve_restaurant(rest_id)
    if ret_val:  # Something went awry
        return jsonify(ret_val), http_status

    rest_item = restaurant_to_dict(rest)
    ret_val = {'success': True, 'id': rest_id, 'restaurant': rest_item, 'message': None,
               'api_version': API_VERSION
              }
    return jsonify(ret_val), 200
Ejemplo n.º 4
0
def restaurant_delete(rest_id):
    if not requires_scope('delete:restaurants'):
        raise AuthError({"success": False,
                         "message": "No access to this resource"}, 403)

    rest, ret_val, http_status = retrieve_restaurant(rest_id)
    if ret_val:  # Something went awry
        return jsonify(ret_val), http_status

    db.session.delete(rest)
    db.session.commit()

    ret_val = {'success': True,
               'id': rest_id,
               'restaurant': None,
               'message': f'Restaurant deleted: {rest.name}',

               'api_version': API_VERSION
               }
    return jsonify(ret_val), 200
Ejemplo n.º 5
0
def reviews():
    if not requires_scope('read:reviews'):
        raise AuthError({"success": False,
                         "message": "No access to this resource"}, 403)

    try:
        reviews = Review.query.filter_by().order_by(Review.id)
        reviews_list = []
        for rev in reviews:
            review_item = review_to_dict(rev)
            reviews_list.append(review_item)
    except Exception as ex:
        logging.error('Failed to retrieve list of reviews for "/reviews" endpoint')
        logging.error(f'Exception was thrown: {str(ex)}')
        ret_val = {'success': False,
            'reviews': None,
            'message': 'Server failure: list of reviews could not be retrieved',
            'api_version': API_VERSION
            }
        return jsonify(ret_val), 500

    ret_val = {'success': True, 'reviews': reviews_list, 'message': None, 'api_version': API_VERSION}
    return jsonify(ret_val), 200