Exemplo n.º 1
0
def delete_business(business_id):
    current_user = get_jwt_identity()
    business = Businesses.get_one(business_id)
    if business:
        if current_user == business.owner_id:
            Businesses.delete_business(business_id)
            return jsonify({'message': 'Successfully Deleted'}), 201
        else:
            return jsonify(
                {'message':
                 'You cannot delete a business that is not yours'}), 401

    else:
        return jsonify(
            {'message': 'Cannot Delete. Resource(Business) Not Found'}), 404
Exemplo n.º 2
0
def update_business(business_id):
    '''Route to update and delete a business'''
    current_user = get_jwt_identity()
    business = Businesses.get_one(business_id)
    if business:
        if current_user == business.owner_id:
            data = request.get_json()
            Businesses.update_business(business_id, data)
            return jsonify({'message': 'Successfully Updated'}), 201
        else:
            return jsonify(
                {'message':
                 'You cannot update a business that is not yours'}), 401

    else:
        return jsonify(
            {'message': 'Cannot Update. Resource(Business) Not Found'}), 404
Exemplo n.º 3
0
def get_reviews(business_id):
    business = Businesses.get_one(business_id)
    if business:
        allreviews = Reviews.get_reviews(business_id)
        obj = [allreview.serialize() for allreview in allreviews]
        if len(obj) > 0:
            return make_response(jsonify(obj)), 200
        else:
            return jsonify(
                {'message': 'No reviews yet.Please review business'}), 404
    else:
        return jsonify({'message': 'Resource(Business) Not Found'}), 404
Exemplo n.º 4
0
def get_business():
    '''Route to get all the businesses'''
    page = request.args.get('page', 1, type=int)
    limit = request.args.get('limit', 2, type=int)
    businesses = Businesses.get_all(page, limit)
    if len(businesses) > 0:
        obj = [business.serialize() for business in businesses]
        return make_response(jsonify(obj)), 200
    elif len(businesses) == 0 and page == 1:
        return jsonify({'message': 'No businesses yet'}), 404
    else:
        return jsonify({'message': 'Nothing on this page'}), 200
Exemplo n.º 5
0
def register_business():
    '''Route to register a business'''
    current_user = get_jwt_identity()
    biz_data = request.get_json()

    business_name = biz_data.get('business_name')
    category = biz_data.get('category')
    location = biz_data.get('location')
    description = biz_data.get('description')

    data = {
        "business_name": business_name,
        "category": category,
        "location": location,
        "description": description
    }

    if validate.inputs(data):
        return jsonify(validate.inputs(data)), 406

    owner = Users.query.filter_by(id=current_user).first()
    owner_id = owner.id
    existing_business = Businesses.query.filter_by(
        business_name=business_name).first()
    if existing_business:
        response = {'message': "Business Name Taken!"}
        return make_response(jsonify(response['message'])), 409

    else:
        new_biz = Businesses(business_name, category, location, description,
                             owner_id)
        new_biz.register_business()
        response = {
            'message':
            new_biz.business_name + '. Business successfully registered by ' +
            new_biz.owner.username
        }
        return make_response(jsonify(response['message'])), 201
Exemplo n.º 6
0
def get_a_business(business_id):
    business = Businesses.get_one(business_id)
    if business:
        results = {
            business.id: {
                'Business name': business.business_name,
                'Category': business.category,
                'Location': business.location,
                'Created By': business.owner.username,
                'Description': business.description
            }
        }
        return make_response(jsonify(results)), 200
    else:
        return jsonify({'message': 'Resource Not Found'}), 404
Exemplo n.º 7
0
def search():
    '''Route to search and filter businesses'''
    search = request.args.get('q', type=str)
    category = request.args.get('category', type=str)
    location = request.args.get('location', type=str)
    page = request.args.get('page', 1, type=int)
    limit = request.args.get('limit', 2, type=int)

    businesses = Businesses.search(search, category, location, page, limit)
    if len(businesses) > 0:
        obj = [business.serialize() for business in businesses]
        ctx = {'Businesses': obj, 'Current Page': page}
        return make_response(jsonify(ctx)), 200
    elif len(businesses) == 0 and page == 1:
        return jsonify({'message': 'No Match found'}), 404
    else:
        return jsonify({'message': 'Nothing on this page'}), 200
Exemplo n.º 8
0
def reviews(business_id):
    current_user = get_jwt_identity()
    business = Businesses.get_one(business_id)
    if business:
        if current_user != business.owner_id:
            review_data = request.get_json()
            title = review_data.get('title')
            description = review_data.get('description')
            data = {"title": title, "description": description}
            if validate.inputs(data):
                return jsonify(validate.inputs(data)), 406
            user_id = business.owner_id
            business_id = business.id
            new_review = Reviews(title, description, user_id, business_id)
            new_review.add_review()
            response = {
                'message': 'Review Posted',
            }
            return make_response(jsonify(response)), 201
        else:
            return jsonify({'message': 'Cannot Review your own Business'}), 401
    else:
        return jsonify(
            {'message': 'Cannot Review. Resource(Business) Not Found'}), 404