Exemplo n.º 1
0
def search_by_keyword(keyword):
    """
    Retrieve all customers for the given keyword
    This endpoint will return customers which match given keyword
    ---
    tags:
      - Customers
    produces:
      - application/json
    parameters:
      - name: keyword
        in: path
        description: the keyword to match for returning customers information
        type: string
        required: true
    responses:
      200:
        description: An array of customers
        schema:
          type: array
          items:
            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 (true) or not (false)
                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
    """
    results = []
    results.extend(Customer.search_in_age(redis, keyword))
    results.extend(Customer.search_in_first_name(redis, keyword))
    results.extend(Customer.search_in_last_name(redis, keyword))
    results.extend(Customer.search_in_email(redis, keyword))
    results.extend(Customer.search_in_address_line1(redis, keyword))
    results.extend(Customer.search_in_address_line2(redis, keyword))
    results.extend(Customer.search_in_phonenumber(redis, keyword))
    final_results = []
    list_ids = []
    for res in results:
        if res.id not in list_ids:
            final_results.append(res)
            list_ids.append(res.id)
    answer = [Customer.serialize(customer) for customer in final_results]
    return make_response(jsonify(answer), HTTP_200_OK)
Exemplo n.º 2
0
def list_customers():
    """
    Retrieve all customers or the customers which match given parameter(s)
    This endpoint will return customers which match given parameter(s)
    ---
    tags:
      - Customers
    description: The customers endpoint allows you to query customers
    produces:
      - application/json
    parameters:
      - name: email
        in: query
        description: the email to match for returning customers information
        type: string
        required: false
      - name: last-name
        in: query
        description: the last name to match for returning customers information
        type: string
        required: false
      - name: first-name
        in: query
        description: the first name to match for returning customers information
        type: string
        required: false
      - name: age
        in: query
        description: the age to match for returning customers information
        type: integer
        required: false
      - name: gender
        in: query
        description: the gender to match for returning customers information
        type: string
        required: false
      - name: address-line1
        in: query
        description: the address line 1 to match for returning customers information
        type: string
        required: false
      - name: address-line2
        in: query
        description: the address line 2 to match for returning customers information
        type: string
        required: false
      - name: phonenumber
        in: query
        description: the phone number to match for returning customers information
        type: string
        required: false
      - name: active
        in: query
        description: the status of customer to match for returning customers information
        type: boolean
        required: false
    responses:
      200:
        description: An array of customers
        schema:
          type: array
          items:
            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 (true) or not (false)
                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
    """
    customers = []
    email = request.args.get('email')
    last_name = request.args.get('last-name')
    first_name = request.args.get('first-name')
    age = request.args.get('age')
    gender = request.args.get('gender')
    address_line1 = request.args.get('address-line1')
    address_line2 = request.args.get('address-line2')
    phonenumber = request.args.get('phonenumber')
    active = request.args.get('active')
    if email:
        customers = Customer.find_by_email(redis, email)
    elif last_name:
        customers = Customer.find_by_last_name(redis, last_name)
    elif first_name:
        customers = Customer.find_by_first_name(redis, first_name)
    elif age:
        customers = Customer.find_by_age(redis, age)
    elif gender:
        customers = Customer.find_by_gender(redis, gender)
    elif address_line1:
        customers = Customer.find_by_address_line1(redis, address_line1)
    elif address_line2:
        customers = Customer.find_by_address_line2(redis, address_line2)
    elif phonenumber:
        customers = Customer.find_by_phonenumber(redis, phonenumber)
    elif active:
        customers = Customer.find_by_activity(redis, str(active).lower())
    else:
        customers = Customer.all(redis)

    results = [Customer.serialize(customer) for customer in customers]
    return make_response(jsonify(results), HTTP_200_OK)