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 get(self, _id): """ Finds an customer by its full name and returns it. :param id :type id: int :return: customer data. :rtype: application/json. """ customer = CustomerModel.find_by_id(_id) if customer: return customer.json() else: return ({'message': 'Customer not found'}, 404)
def delete(self, _id): """ Finds an customer by its name and deletes it. :param id: the id of the customer. :type int :return: success or failure message. :rtype: application/json response. """ # claims = get_jwt_claims() # # if not claims['is_admin']: # return {'message': 'Admin privilege required.'}, 401 customer = CustomerModel.find_by_id(_id) if customer: try: customer.delete_from_db() return {'message': 'Customer deleted'} except: return ({ 'message': 'An error occurred deleting the customer.' }, 500) else: return {'message': 'Customer Not Found'}