def delete_customers(id): """ Delete a Customer This endpoint will delete a Customer based the id specified in the path --- tags: - Customers description: Deletes a Customer from the database parameters: - name: id in: path description: ID of customer to delete type: integer required: true responses: 204: description: Customer deleted """ customer = Customer.find(redis, id) if customer: customer.delete(redis) return make_response('', HTTP_204_NO_CONTENT)
def create_customers(): """ Creates a Customer This endpoint will create a Customer based the data in the body that is posted --- tags: - Customers consumes: - application/json produces: - application/json parameters: - in: body name: body schema: id: data required: - address_line1 - address_line2 - age - email - first_name - last_name - gender - phonenumber properties: address_line1: type: string description: address line 1 of the customer address_line2: type: string description: address line 2 of the customer age: type: integer description: age of the customer email: type: string description: email address of the customer first_name: type: string description: first name of the customer last_name: type: string description: last name of the customer gender: type: string description: gender of the customer phonenumber: type: string description: phone number of the customer responses: 201: description: Customer information schema: id: Customer properties: id: type: integer description: unique id assigned internally by service active: type: boolean description: the status of customer whether it is currently active (false in this case) address_line1: type: string description: address line 1 of the customer address_line2: type: string description: address line 2 of the customer age: type: integer description: age of the customer email: type: string description: email address of the customer first_name: type: string description: first name of the customer last_name: type: string description: last name of the customer gender: type: string description: gender of the customer phonenumber: type: string description: phone number of the customer 400: description: Bad Request (the posted data was not valid) """ id = 0 payload = request.get_json() if Customer.validate(payload): customer = Customer(id, payload['first_name'], payload['last_name'], payload['gender'], payload['age'], payload['email'], payload['address_line1'], payload['address_line2'], payload['phonenumber'], True) customer.save(redis) id = customer.id cust = Customer.find( redis, id ) # added so that the response body of POST matches that of the GET and we compare the results in the TDD in the same format as the json returned by Redis #message = customer.serialize() message = cust.serialize() rc = HTTP_201_CREATED else: message = {'error': 'Data is not valid'} rc = HTTP_400_BAD_REQUEST response = make_response(jsonify(message), rc) if rc == HTTP_201_CREATED: response.headers['Location'] = url_for('get_customers', id=id) return response
def update_customers(id): """ Update customer information This endpoint will return update customer information --- tags: - Customers produces: - application/json parameters: - name: id in: path description: the id to match for finding a customer type: integer required: true - in: body name: body schema: id: data required: - address_line1 - address_line2 - age - email - first_name - last_name - gender - phonenumber properties: address_line1: type: string description: address line 1 of the customer address_line2: type: string description: address line 2 of the customer age: type: integer description: age of the customer email: type: string description: email address of the customer first_name: type: string description: first name of the customer last_name: type: string description: last name of the customer gender: type: string description: gender of the customer phonenumber: type: string description: phone number of the customer responses: 200: description: Customer information schema: id: Customer properties: id: type: integer description: unique id assigned internally by service active: type: boolean description: updated status of customer whether it is currently active (false in this case) address_line1: type: string description: updated address line 1 of the customer address_line2: type: string description: updated address line 2 of the customer age: type: integer description: updated age of the customer email: type: string description: updated email address of the customer first_name: type: string description: updated first name of the customer last_name: type: string description: updated last name of the customer gender: type: string description: updated gender of the customer phonenumber: type: string description: updated phone number of the customer 404: description: error, Customer was not found 400: description: bad request, data was invalid """ customer = Customer.find(redis, id) if customer: active = customer.active payload = request.get_json() payload['active'] = True # make payload complete if Customer.validate(payload): customer = Customer.from_dict(payload) customer.id = id # so that the id in the URI is utilized customer.active = active # restore the activity status customer.save(redis) message = customer.serialize() rc = HTTP_200_OK else: message = {'error': 'Customer data was not valid'} rc = HTTP_400_BAD_REQUEST else: message = {'error': 'Customer %s was not found' % id} rc = HTTP_404_NOT_FOUND return make_response(jsonify(message), rc)
def get_customers(id): """ Get customer with given ID This endpoint will return customer customer information with given ID --- tags: - Customers produces: - application/json parameters: - name: id in: path description: the id to match for finding a customer type: integer required: true responses: 200: description: Customer information schema: id: Customer properties: id: type: integer description: unique id assigned internally by service active: type: boolean description: the status of customer whether it is currently active (false in this case) address_line1: type: string description: address line 1 of the customer address_line2: type: string description: address line 2 of the customer age: type: integer description: age of the customer email: type: string description: email address of the customer first_name: type: string description: first name of the customer last_name: type: string description: last name of the customer gender: type: string description: gender of the customer phonenumber: type: string description: phone number of the customer 404: description: error, Customer was not found """ customer = Customer.find(redis, id) if customer: message = customer.serialize() rc = HTTP_200_OK else: message = {'error': 'Customer with id: %s was not found' % str(id)} rc = HTTP_404_NOT_FOUND return make_response(jsonify(message), rc)