def create_customers():

    if request.method == "POST":
        request_body = request.get_json()

        if "name" not in request_body or "postal_code" not in request_body or "phone" not in request_body:
            return ({"details": "Invalid data"}, 400)
        else:
            customer = Customer(name=request_body["name"],
                                phone=request_body["phone"],
                                postal_code=request_body["postal_code"],
                                registered_at=datetime.utcnow())

            db.session.add(customer)
            db.session.commit()

            return make_response(customer.to_json(), 201)
    else:
        customers = Customer.query.all()
        customers_response = []

        for customer in customers:
            customers_response.append(customer.to_json())

        return jsonify(customers_response)
 def save(self, customer):
     """
     Save supplied customer object to database
     """
     try:
         cust = Customer(customer)
         self.customer_table.put_item(
             Item=cust.to_primitive()
         )
     except ClientError as e:
         app.logger.error('Error saving customer record {0}, exception {1}'.format(customer.email_address, str(e)))
         raise CustomerStoreError('Error saving customer record {0}, exception {1}'.format(customer.email_address,
                                                                                           str(e)))
예제 #3
0
    def put(self):
        """
        Create a customer
        ---
        tags:
          - customers
        produces:
          - application/json
        parameters:
          - in: body
            name: body
            required: true
            description: Json request for creating a customer
            schema:
              type: object
              properties:
                firstName:
                  type: string
                  default: Test
                surname:
                  type: string
                  default: McTest
                emailAddress:
                  type: string
                  default: [email protected]
        responses:
          201:
            description: Customer Created
          400:
            description: Invalid Request
          500:
            description: Server Error
        """
        # Validate request, return 400 if invalid
        try:
            customer = Customer(request.get_json())
            customer.validate()
        except DataError as e:
            return {'Error': str(e)}, 400

        try:
            customer_service = CustomerService()
            customer_service.create_or_get_customer(customer)
            return {'Status': 'SUCCESS'}, 201
        except CustomerServiceError:
            return {'Status': 'ERROR'}, 500