def register_business(): if request.method == 'POST': 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') biz = Business.business.items() existing_business = { k: v for k, v in biz if biz_data['business_name'] in v['business_name'] } if business_name == "": return jsonify( {'message': 'Business name should not be an empty string'}), 406 elif category == "": return jsonify( {'message': 'Category should not be an empty string'}), 406 elif location == "": return jsonify( {'message': 'Location should not be an empty string'}), 406 elif description == "": return jsonify( {'message': 'Description should not be an empty string'}), 406 elif existing_business: return jsonify({"message": "Business name already exists"}) else: new_biz = Business(business_name, category, location, description, current_user) new_biz.register_business() response = { 'message': 'Business Successfully Registered', 'Registered by': current_user } return make_response(jsonify(response)), 201 #If GET method businesses = Business.get_all_businesses() return make_response(jsonify(businesses)), 200
def business_manipulation_by_name(name): """get the id from the route""" """use the name to find the business""" business_found = Business.find_business_name(name) if not business_found: """if no business matches the name""" response = jsonify({ "message": "business does not exist", "status": 404 }) response.status_code = 404 return response if request.method == "GET": if business_found: response = jsonify({"Business": business_found}) response.status_code = 200 return response else: response = jsonify({ "message": "business does not exist", "status": 404 }) response.status_code = 404 return response elif request.method == 'PUT': if business_found: name = str(request.data.get('name', '')) description = str(request.data.get('description', '')) location = str(request.data.get('location', '')) contact = str(request.data.get('contact', '')) business_found[0]["name"] = name business_found[0]["description"] = description business_found[0]["location"] = location business_found[0]["contact"] = contact response = jsonify({"business": business_found}) response.status_code = 200 return response else: response = jsonify({ "message": "Cannot update business that does not exist", "status": 404 }) response.status_code = 404 return response else: if business_found: businesses = Business.get_all_businesses() businesses.remove(business_found[0]) response = jsonify({ "business": "business successfully deleted", "status": 200 }) response.status_code = 200 return response else: response = jsonify({ "message": "Cannot delete business that does not exist", "status": 404 }) response.status_code = 404 return response
def business_manipulation(id): """gets the id of the business""" """uses the id to get a single business""" business_found = Business.find_business_id(id) if request.method == "GET": if business_found: response = jsonify({"business": business_found}) response.status_code = 200 return response else: response = jsonify({ "message": "business does not exist", "status": 404 }) response.status_code = 404 return response elif request.method == "PUT": if business_found: name = str(request.data.get('name', '')) description = str(request.data.get('description', '')) location = str(request.data.get('location', '')) contact = str(request.data.get('contact', '')) business_found[0]["name"] = name business_found[0]["description"] = description business_found[0]["location"] = location business_found[0]["contact"] = contact response = jsonify({"business": business_found}) response.status_code = 200 return response else: response = jsonify({ "message": "Cannot update business that does not exist", "status": 404 }) response.status_code = 404 return response else: if business_found: businesses = Business.get_all_businesses() businesses.remove(business_found[0]) response = jsonify({ "business": "business successfully deleted", "status": 200 }) response.status_code = 200 return response else: response = jsonify({ "message": "Cannot delete business that does not exist", "status": 404 }) response.status_code = 404 return response
def business(): if request.method == 'POST': """gets data from request and save business""" name = str(request.data.get('name', '')) description = str(request.data.get('description', '')) location = str(request.data.get('location', '')) contact = str(request.data.get('contact', '')) if name and description and location and contact: """validate that it is not duplicate""" validateName = Business.check_name_exists(name) validateContact = Business.check_contact_exists(contact) validName = Business.validate_business_name(name) validLocation = Business.validate_business_description( location) validContact = Business.validate_business_contact(contact) if validateName: response = jsonify({ "message": "Business name already exists", "status_code": 400 }) response.status_code = 400 return response elif validateContact: response = jsonify({ "message": "Business contact already exists use different contact", "status_code": 400 }) response.status_code = 400 return response elif validName: response = jsonify({ "message": "Enter business name", "status_code": 400 }) response.status_code = 400 return response elif validLocation: response = jsonify({ "message": "Enter business location", "status_code": 400 }) response.status_code = 400 return response elif validContact: response = jsonify({ "message": "Enter business contact", "status_code": 400 }) response.status_code = 400 return response else: """create business object""" business = Business(name=name, description=description, location=location, contact=contact) new_business = business.save_business( name, description, location, contact) response = jsonify(new_business) response.status_code = 201 return response elif not name: response = jsonify({ "message": "name is missing", "status_code": 400 }) response.status_code = 400 return response elif not description: response = jsonify({ "message": "description missing", "status_code": 400 }) response.status_code = 400 return response elif not location: response = jsonify({ "message": "business location is missing", "status_code": 400 }) response.status_code = 400 return response else: response = jsonify({ "message": "business contact is missing", "status_code": 400 }) response.status_code = 400 return response else: """if its a get request""" Businesses = Business.get_all_businesses() print(Businesses) if not Businesses: response = jsonify({ "message": "business does not exist", "status": 200 }) response.status_code = 200 response = jsonify({ "message": "business does not exist", "status": 400 }) response.status_code = 400 return response response = jsonify({"businesses": Businesses}) response.status_code = 200 return response
def test_get_all_businesses(self): # Tests that a list of information is returned self.assertIs(type(Business.get_all_businesses()), list)