Exemple #1
0
def callback(ch, method, properties, body):
    print("\nReceived from admin_2_web_mq")
    data = json.loads(body)

    if properties.content_type == "PRODUCT_CREATED":
        print("PRODUCT_CREATED")
        product = Product(
            id=data["id"],
            title=data["title"],
            image=data["image"]
        )
        if product is not None:
            db.session.add(product)
            db.session.commit()
        else:
            print("Sorry! Product cannot be created", data)

    if properties.content_type == "PRODUCT_UPDATED":
        print("PRODUCT_UPDATED")
        product = Product.query.get(data["id"])
        if product is not None:
            product.title = data["title"]
            product.image = data["image"]
            db.session.commit()
        else:
            print("Sorry! Product DoesNotExist", data["id"])

    if properties.content_type == "PRODUCT_DELETED":
        print("PRODUCT_DELETED")
        product = Product.query.get(data["id"])
        if product is not None:
            db.session.delete(product)
            db.session.commit()
        else:
            print("Sorry! Product DoesNotExist", data["id"])
Exemple #2
0
def callback(ch, method, properties, body):
    print("Received in main")
    data = json.loads(body)
    print(data)

    if properties.content_type == 'product_created':
        product = Product(id=data['id'],
                          title=data['title'],
                          image=data['image'])
        db.session.add(product)
        db.session.commit()
        print('Product Created')

    elif properties.content_type == 'product_updated':
        product = Product.query.get(data['id'])
        product.title = data['title']
        product.image = data['image']
        db.session.commit()
        print('Product Updated')

    elif properties.content_type == 'product_deleted':
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
        print('Product Deleted')
def callback(ch,method, properties,body):
    print('recieved in main')
    body = body.decode('utf-8').replace('\0', '')
    data = json.loads(body)
    print(data)

    if properties.content_type == 'product_created':
        product = Product(id=data['id'],title=data['title'],image=data['image'])
        db.session.add(product)
        db.session.commit()
        print('Product created')

    elif properties.content_type == 'product_updated':
        product = Product.query.get(data['id'])
        product.title = data['title']
        product.image = data['image']
        db.session.commit()
        print('Product updated')
    
    elif properties.content_type == 'product_deleted':
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
        print('Product deleted')
    else:
        print('Product listed')
Exemple #4
0
def callback(ch, method, properties, body):
    print('received main...')
    data = json.loads(body)
    #print(data)

    if properties.content_type == 'product_created':
        product = Product(product_id=data['id'],
                          title=data['title'],
                          image=data['image'])
        db.session.add(product)
        db.session.commit()
        print('product_created')

    elif properties.content_type == 'product_updated':
        product = Product.query.filter_by(product_id=data['id']).first()
        product.title = data['title']
        product.image = data['image']
        db.session.commit()
        print('product_updated')

    elif properties.content_type == 'product_deleted':
        print(data, 'deleted data')
        product = Product.query.filter_by(product_id=data).first()
        db.session.delete(product)
        db.session.commit()
        print('product_deleted')
Exemple #5
0
def callback(ch, method, properties, body):
    print('Received in main')
    data = json.loads(body)
    print(data)

    if properties.content_type == 'product_created':
        product = Product(
            id=data['id'],
            sku=data['sku'],
            name=data['name'],
            price=data['price'],
            brand=data['brand'],
            image=data['image'],
            views=data['views']
        )
        db.session.add(product)
        db.session.commit()
        print('Product Created')

    elif properties.content_type == 'product_updated':
        product = Product.query.get(data['id'])
        product.sku = data['sku']
        product.name = data['name']
        product.price = data['price']
        product.brand = data['brand']
        product.image = data['image']
        product.views = data['views']
        db.session.commit()
        print('Product Updated')

    elif properties.content_type == 'product_deleted':
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
        print('Product Deleted')
Exemple #6
0
def callback(ch, method, properties, body):
    print("Recieved in main")
    data = json.loads(body)
    print(data)

    if properties.content_type == "product_created":
        product = Product(id=data["id"], title=data["title"], image=data["image"])
        db.session.add(product)
        db.session.commit()
        print("Product Created")
    elif properties.content_type == "product_updated":
        try:
            product = Product.query.get(data["id"])
            product.title = data["title"]
            product.image = data["image"]
            db.session.commit()
            print("Product Updated")
        except:
            abort(400, "No Product Exists")

    elif properties.content_type == "product_deleted":
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
        print("Product Deleted")
    else:
        print("No properties.content.type chosen")
Exemple #7
0
def callback(ch, method, properties, body):
    print("Received in main")
    data = json.loads(body)
    print(data)

    try:
        if properties.content_type == "product_created":
            product = Product(
                id=data["id"], title=data["title"], image=data["image"])
            db.session.add(product)
            db.session.commit()
            print("Product Created Successfully")

        elif properties.content_type == "product_updated":
            product = Product.query.get(data["id"])
            product.title = data["title"]
            product.image = data["image"]
            db.session.commit()
            print("Product Updated Successfully")

        elif properties.content_type == "product_deleted":
            product = Product.query.get(data)
            db.session.delete(product)
            db.session.commit()
            print("Product Deleted Successfully")
    except Exception as e:
        print("Error while updating database :", e)
def callback(ch, method, properties, body):
    print('Received in main')
    data = json.loads(body)
    print(data)

    if properties.content_type == 'product created':
        product = Product(id=data['id'],
                          title=data['title'],
                          image=data['image'])
        db.session.add(product)
        db.session.commit()
        print('Product created')

    elif properties.content_type == 'product updated':
        product = Product.query.get(data['id'])
        product.title = data['title']
        product.image = data['image']
        db.session.commit()
        print('Product changed')

    elif properties.content_type == 'product list requested':

        requests.get('http://docker.for.mac.localhost:8001/api/products')
        print('Product list sent')

    elif properties.content_type == 'product deleted':
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
        print('Product deleted')
Exemple #9
0
def callback(ch, method, properties, body):
    logging.info('Received in main')  # test
    data = json.loads(body)
    logging.info('Data Received: %s', data)  # test

    if properties.content_type == event_created:
        product = Product(id=data['id'],
                          title=data['title'],
                          image=data['image'])
        # creating the object with SQLAlchemy
        db.session.add(product)
        db.session.commit()
        logging.info('Product created')

    elif properties.content_type == event_updated:
        product = Product.query.get(data['id'])
        product.title = data['title']
        product.image = data['image']
        # updated the object with SQLAlchemy
        db.session.commit()
        logging.info('Product updated')

    elif properties.content_type == event_deleted:
        product = Product.query.get(data)
        # deleting the object in SQLAlchemy
        db.session.delete(product)
        db.session.commit()
        logging.info('Product deleted')
Exemple #10
0
def callback(ch, method, properties, body):
    print("Recieve in main")
    data = json.loads(body)
    print(data)

    if properties.content_type == "product_created":
        product = Product(id=data["id"], image=data["image"], title=data["image"])
        db.session.add(product)
        db.session.commit()
    elif properties.content_type == "product_updated":
        product = Product.query.get(id=data["id"])
        product.title = data["title"]
        product.image = data["image"]
        db.session.commit()
    elif properties.content_type == "product_deleted":
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
def callback(ch, method, properties, body):
    print('Receieved in client')
    data = json.loads(body)
    print(body, data)
    if properties.content_type == "product_created":
        product = Product(id=data['id'], title=data['title'], image=data['image'],
                          price=data['price'], discounted_price=data['discounted_price'])
        db.session.add(product)
        db.session.commit()
    elif properties.content_type == "product_updated":
        product = Product.query.get(data['id'])
        product.title = data['title']
        product.image = data['image']
        product.price = data['price']
        product.discounted_price = data['discounted_price']
        db.session.commit()
    elif properties.content_type == "product_deleted":
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
Exemple #12
0
def callback(ch, method: str, properties: pika.BasicProperties, body):
    data = loads(body)

    content_type = properties.content_type.split("_", 1)[1]
    if content_type == "created":
        product = Product(id=data["id"],
                          title=data["title"],
                          image=data["image"])
        db.session.add(product)
        db.session.commit()
    elif content_type == "updated":
        product = Product.query.get(data["id"])
        product.title = data["title"]
        product.image = data["image"]
        db.session.commit()
    elif content_type == "deleted":
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
    else:
        print("Wrong code")
        return
Exemple #13
0
def callback(ch, method, properties, body):
    print(f'Receive in main: {body}')
    data = json.loads(body)
    print(data)

    if properties.content_type == 'product created':
        product = Product(id=data['id'], title=data['title'], image=data['image'])
        db.session.add(product)
        db.session.commit()
        print("Product created")

    elif properties.content_type == 'product updated':
        product = Product.query.get(data['id'])
        product.title = data['title']
        product.image = data['image']
        db.session.commit()
        print("Product updated")

    elif properties.content_type == 'product deleted':
        product = Product.query.get(data)
        db.session.delete(product)
        db.session.commit()
        print("Product deleted")