Beispiel #1
0
def put(order_id):

    order = store.find_one('order', order_id)
    for product_id in order['product_ids']:
        rsp = requests.post('http://inventory-service:5000/incr/{}'.format(product_id))
        check_rsp_code(rsp)

    value = request.get_json()
    try:
        order = create_order(value['product_ids'], value['customer_id'])
    except KeyError:
        raise ValueError("missing mandatory parameter 'product_ids' and/or 'customer_id'")

    rsp = requests.post('http://inventory-service:5000/decr_from_order', json=value)
    check_rsp_code(rsp)

    if not rsp.json():
        raise ValueError("out of stock")

    order['id'] = order_id

    # trigger event
    store.publish(Event('order', 'updated', **order))

    for product_id in value['product_ids']:
        rsp = requests.post('http://inventory-service:5000/decr/{}'.format(product_id))
        check_rsp_code(rsp)

    return json.dumps(True)
Beispiel #2
0
def post():

    values = request.get_json()
    if not isinstance(values, list):
        values = [values]

    rsp = requests.post('http://inventory-service:5000/decr_from_order', json=values)
    check_rsp_code(rsp)

    if not rsp.json():
        raise ValueError("out of stock")

    order_ids = []
    for value in values:
        try:
            new_order = create_order(value['product_ids'], value['customer_id'])
        except KeyError:
            raise ValueError("missing mandatory parameter 'product_ids' and/or 'customer_id'")

        # trigger event
        store.publish(Event('order', 'created', **new_order))

        order_ids.append(new_order['id'])

    return json.dumps(order_ids)
Beispiel #3
0
def delete(inventory_id):

    inventory = store.find_one('inventory', inventory_id)
    if inventory:

        # trigger event
        store.publish(Event('inventory', 'deleted', **inventory))

        return json.dumps(True)
    else:
        raise ValueError("could not find inventory")
Beispiel #4
0
def delete(billing_id):

    billing = store.find_one('billing', billing_id)
    if billing:

        # trigger event
        store.publish(Event('billing', 'deleted', **billing))

        return json.dumps(True)
    else:
        raise ValueError("could not find billing")
Beispiel #5
0
def delete(product_id):

    product = store.find_one('product', product_id)
    if product:

        # trigger event
        store.publish(Event('product', 'deleted', **product))

        return json.dumps(True)
    else:
        raise ValueError("could not find product")
Beispiel #6
0
def delete(customer_id):

    customer = store.find_one('customer', customer_id)
    if customer:

        # trigger event
        store.publish(Event('customer', 'deleted', **customer))

        return json.dumps(True)
    else:
        raise ValueError("could not find customer")
Beispiel #7
0
def incr(product_id, value=None):

    inventory = list(filter(lambda x: x['product_id'] == product_id, store.find_all('inventory').values()))
    if not inventory:
        raise ValueError("could not find inventory")

    inventory = inventory[0]
    inventory['amount'] = int(inventory['amount']) - (value if value else 1)

    # trigger event
    store.publish(Event('inventory', 'updated', **inventory))

    return json.dumps(True)
Beispiel #8
0
def put(product_id):

    value = request.get_json()
    try:
        product = create_product(value['name'], value['price'])
    except KeyError:
        raise ValueError("missing mandatory parameter 'name' and/or 'price'")

    product['id'] = product_id

    # trigger event
    store.publish(Event('product', 'updated', **product))

    return json.dumps(True)
Beispiel #9
0
def put(inventory_id):

    value = request.get_json()
    try:
        inventory = create_inventory(value['product_id'], value['amount'])
    except KeyError:
        raise ValueError("missing mandatory parameter 'name' and/or 'price'")

    inventory['id'] = inventory_id

    # trigger event
    store.publish(Event('inventory', 'updated', **inventory))

    return json.dumps(True)
Beispiel #10
0
def delete(order_id):

    order = store.find_one('order', order_id)
    if order:
        for product_id in order['product_ids']:
            rsp = requests.post('http://inventory-service:5000/incr/{}'.format(product_id))
            check_rsp_code(rsp)

        # trigger event
        store.publish(Event('order', 'deleted', **order))

        return json.dumps(True)
    else:
        raise ValueError("could not find order")
Beispiel #11
0
def put(customer_id):

    value = request.get_json()
    try:
        customer = create_customer(value['name'], value['email'])
    except KeyError:
        raise ValueError("missing mandatory parameter 'name' and/or 'email'")

    customer['id'] = customer_id

    # trigger event
    store.publish(Event('customer', 'updated', **customer))

    return json.dumps(True)
Beispiel #12
0
def put(billing_id):

    value = request.get_json()
    try:
        billing = create_billing(value['order_id'])
    except KeyError:
        raise ValueError("missing mandatory parameter 'order_id'")

    billing['id'] = billing_id

    # trigger event
    store.publish(Event('billing', 'updated', **billing))

    return json.dumps(True)
Beispiel #13
0
def post():

    values = request.get_json()
    if not isinstance(values, list):
        values = [values]

    billing_ids = []
    for value in values:
        try:
            new_billing = create_billing(value['order_id'])
        except KeyError:
            raise ValueError("missing mandatory parameter 'order_id'")

        # trigger event
        store.publish(Event('billing', 'created', **new_billing))

        billing_ids.append(new_billing['id'])

    return json.dumps(billing_ids)
Beispiel #14
0
def post():

    values = request.get_json()
    if not isinstance(values, list):
        values = [values]

    inventory_ids = []
    for value in values:
        try:
            new_inventory = create_inventory(value['product_id'], value['amount'])
        except KeyError:
            raise ValueError("missing mandatory parameter 'product_id' and/or 'amount'")

        # trigger event
        store.publish(Event('inventory', 'created', **new_inventory))

        inventory_ids.append(new_inventory['id'])

    return json.dumps(inventory_ids)
Beispiel #15
0
def post():

    values = request.get_json()
    if not isinstance(values, list):
        values = [values]

    product_ids = []
    for value in values:
        try:
            new_product = create_product(value['name'], value['price'])
        except KeyError:
            raise ValueError(
                "missing mandatory parameter 'name' and/or 'price'")

        # trigger event
        store.publish(Event('product', 'created', **new_product))

        product_ids.append(new_product['id'])

    return json.dumps(product_ids)
Beispiel #16
0
def post():

    values = request.get_json()
    if not isinstance(values, list):
        values = [values]

    customer_ids = []
    for value in values:
        try:
            new_customer = create_customer(value['name'], value['email'])
        except KeyError:
            raise ValueError(
                "missing mandatory parameter 'name' and/or 'email'")

        # trigger event
        store.publish(Event('customer', 'created', **new_customer))

        customer_ids.append(new_customer['id'])

    return json.dumps(customer_ids)
Beispiel #17
0
def decr_from_order():

    values = request.get_json()
    if not isinstance(values, list):
        values = [values]

    occurs = {}
    for value in values:
        try:
            product_ids = value['product_ids']
        except KeyError:
            raise ValueError("missing mandatory parameter 'product_ids'")

        for inventory in store.find_all('inventory').values():

            if not inventory['product_id'] in occurs:
                occurs[inventory['product_id']] = 0

            occurs[inventory['product_id']] += product_ids.count(inventory['product_id'])
            if occurs[inventory['product_id']] <= int(inventory['amount']):
                continue
            else:
                return json.dumps(False)

    for k, v in occurs.items():
        inventory = list(filter(lambda x: x['product_id'] == k, store.find_all('inventory').values()))
        if not inventory:
            raise ValueError("could not find inventory")

        inventory = inventory[0]
        if int(inventory['amount']) - v >= 0:

            inventory['amount'] = int(inventory['amount']) - v

            # trigger event
            store.publish(Event('inventory', 'updated', **inventory))

        else:
            return json.dumps(False)

    return json.dumps(True)