def put(self, _id): """ Creates or updates an customer using the provided name, price and customer_id. :param id: the id of the customer. :type int: :param first_name: the first_name of the customer. :type str :param last_name: the last_name of the customer. :type str :return: success or failure message. :rtype: application/json response. """ request_data = Customer.parser.parse_args() customer = CustomerModel.find_by_id(_id) if customer is None: customer = CustomerModel(**request_data) else: customer.first_name = request_data['first_name'] customer.last_name = request_data['last_name'] customer.phone = request_data['phone'] customer.email = request_data['email'] customer.city = request_data['city'] customer.address = request_data['address'] customer.birth_date = request_data['birth_date'] try: customer.save_to_db() except: return ({ 'message': 'An error occurred updating the customer.' }, 500) return customer.json()
def test_customer_json(self): customer = CustomerModel('test_first_name', 'test_last_name', '050-0000000', '*****@*****.**', 'test_city', 'test_address', '01-01-2019') expected = { 'first_name': 'test_first_name', 'last_name': 'test_last_name', 'phone': '050-0000000', 'email': '*****@*****.**', 'city': 'test_city', 'address': 'test_address', 'birth_date': '01-01-2019' } self.assertEqual( customer.json(), expected, "The JSON export of the customer is incorrect. Received {}, expected {}." .format(customer.json(), expected))
def post(self): """ Creates a new customer using the provided first_name, last_name . """ request_data = Customer.parser.parse_args() if CustomerModel.find_by_name(request_data['first_name'], request_data['last_name']): return ({ 'message': ("An customer with name '{}' '{}' already exists.").format( request_data['first_name'], request_data['last_name']) }, 400) customer = CustomerModel(**request_data) try: customer.save_to_db() except: return ({ 'message': 'An error occurred inserting the customer.' }, 500) return (customer.json(), 201)