def product_create()

in cqrs/services/product/main.py [0:0]


def product_create():
    json_data = request.get_json()
    product_id, product_name, unit_price = None, None, None
    invalid_fields = []
    for key in json_data.keys():
        if key == 'product_id':
            product_id = json_data[key]
        elif key == 'product_name':
            product_name = json_data[key]
        elif key == 'unit_price':
            unit_price = json_data[key]
        else:
            invalid_fields.append(key)
    if product_id is None or product_name is None or unit_price is None:
        return error500()

    product = {
        'product_id': product_id,
        'product_name': product_name,
        'unit_price': unit_price
    }

    # Check the existing prodcut_id
    query = ds_client.query(kind='ProductCQRS')
    query.add_filter('product_id', '=', product_id)
    exist = False
    for result in query.fetch(): # This should return a single entity.
        result.update(product)
        ds_client.put(result)
        exist = True
        break
    if not exist:
        incomplete_key = ds_client.key('ProductCQRS')
        product_entity = datastore.Entity(key=incomplete_key)
        product_entity.update(product)
        ds_client.put(product_entity)

    return product, 200