示例#1
0
def createUser():
    body = request.get_json()
    user = User(name=body.get('name'))
    db.session.add(user)
    db.session.commit()
    publish('events.users.created',
            UserCreated(user.name, user.id).to_string())
    return {"success": True, "data": user.toDict()}
示例#2
0
 def handleProductAccpetOrder(self, payload):
     order = Order.query.get(payload['orderId'])
     order.status = OrderStatus['AWAIT_USER_BALANCE_CHECK']
     order.totalPrice = payload['price']
     db.session.add(order)
     db.session.commit()
     command = CheckUserBalance(order.id, order.userId, order.totalPrice)
     publish('user/order_created', command.to_string())
     self.saveEvent(payload, 'handleProductAccpetOrder')
示例#3
0
 def handleUserRejectOrder(self, payload):
     print(payload, 'handleUserRejectOrder')
     order = Order.query.get(payload['orderId'])
     order.status = OrderStatus['REJECTED_USER_CANNOT_ACCEPT']
     db.session.add(order)
     db.session.commit()
     command = RollBackOrderFromProduct(order.id, order.productId,
                                        order.totalQuantity)
     publish('product/roll_back_order', command.to_string())
     self.saveEvent(payload, 'handleUserRejectOrder')
def orderCreated(ctedh, method, properties, body):
    event = OrderCreatedEvent(str(body, 'utf-8'))
    user = User.query.get(event.userId)
    user.ordersCount =  user.ordersCount + 1
    user.balance = user.balance - event.totalPrice 
    if(user.balance > 0):
        db.session.add(user)
        db.session.commit()
        event = AccpetOrder(event.orderId,event.userId)
        publish('order/user_accpet_order',event.to_string())
    else:
        event = RejectOrder(event.orderId,event.userId)
        publish('order/user_reject_order' ,event.to_string())
def orderCreated(ctedh, method, properties, body):
    event = OrderCreatedEvent(str(body, 'utf-8'))
    product = Product.query.get(event.productId)
    product.quantity = product.quantity - event.quantity
    if (product.quantity > 0):
        db.session.add(product)
        db.session.commit()
        event = AccpetOrder(event.orderId, event.productId,
                            event.quantity * product.price)
        publish('order/product_accpet_order', event.to_string())
    else:
        event = RejectOrder(
            event.orderId,
            event.productId,
        )
        publish('order/product_reject_order', event.to_string())
def api_root():
    print(request.form)
    if request.method == 'POST' and request.form:
        try:
            session = session_factory()
            user = User(request.form["name"])
            session.add(user)
            session.commit()
            session.close()
            publish('user_created', user.to_json_string())
            return user.to_json_string()
        except Exception as ex:
            return ex

    else:
        return "Where is the image?"
示例#7
0
 def createOrder(self, productId, userId, quantity):
     order = Order(userId=userId,
                   productId=productId,
                   totalQuantity=quantity,
                   status=OrderStatus['AWAIT_PRODUCT_QUANTITY_CHECK'])
     db.session.add(order)
     db.session.commit()
     command = CheckProductQuantity(order.id, order.productId,
                                    order.totalQuantity)
     publish('product/order_created', command.to_string())
     payload = {
         "userId": userId,
         "productId": productId,
         "totalQuantity": quantity,
         "orderId": order.id,
     }
     self.saveEvent(payload, 'createOrder')
     return order