def create_address(): data = request.get_json() address = AddressSchema().load(data) session.add(address) session.commit() return json_response(address=AddressSchema().dump(address))
def create_wallet_transaction(): data = request.get_json() wallet_transaction = WalletTransactionSchema().load(data) payer = User.query.get(wallet_transaction.payer_id) if payer is None: return json_response(ResponseCode.NOT_FOUND) payee = User.query.get(wallet_transaction.payee_id) if payee is None: return json_response(ResponseCode.NOT_FOUND) count1 = User.query.filter( and_(User.id == payer.id, User.wallet_money >= wallet_transaction.amount, User.wallet_money == payer.wallet_money)).update({ User.wallet_money: payer.wallet_money - wallet_transaction.amount }) count2 = User.query.filter( and_(User.id == payee.id, User.wallet_money == payee.wallet_money)).update({ User.wallet_money: payee.wallet_money + wallet_transaction.amount }) if count1 == 0 or count2 == 0: session.rollback() return json_response(ResponseCode.TRANSACTION_FAILURE) session.add(wallet_transaction) session.commit() return json_response( wallet_transaction=WalletTransactionSchema().dump(wallet_transaction))
def create_cart_product(): """添加购物车商品 """ data = request.get_json() cart_product = CartProductSchema().load(data) cart_products = CartProduct.query.filter( CartProduct.user_id == cart_product.user_id).all() # 商品是否已在购物车 existed = None for v in cart_products: if v.product_id == cart_product.product_id: existed = v break # 购物车商品数量不能超过限制 if len(cart_products) >= current_app.config['CART_PRODUCT_LIMIT'] and existed is None: return json_response(ResponseCode.QUANTITY_EXCEEDS_LIMIT) # 商品已在购物车则更新数量,否则添加一条新纪录 if existed is None: session.add(cart_product) else: existed.amount += cart_product.amount session.commit() return json_response(cart_product=CartProductSchema().dump(cart_product if existed is None else existed))
def create_cart_product(): data = request.get_json() cart_product = CartProductSchema().load(data) session.add(cart_product) session.commit() return json_response(cart_product=CartProductSchema().dump(cart_product))
def create_shop(): data = request.get_json() shop = ShopSchema().load(data) session.add(shop) session.commit() return json_response(shop=ShopSchema().dump(shop))
def create_user(): data = request.get_json() user = UserSchema().load(data) session.add(user) session.commit() return json_response(user=UserSchema().dump(user))
def create_favorite_product(): data = request.get_json() favorite_product = FavoriteProductSchema().load(data) session.add(favorite_product) session.commit() return json_response( favorite_product=FavoriteProductSchema().dump(favorite_product))
def create_user(): '''register''' data = request.get_json() password = data.pop('password') user = UserSchema().load(data) user.password = password session.add(user) session.commit() return json_response(user=UserSchema().dump(user))
def create_product(): """创建商品 """ data = request.get_json() product = ProductSchema().load(data) session.add(product) session.commit() return json_response(product=ProductSchema().dump(product))
def create_order(): data = request.get_json() if data.get('pay_amount') is None: data['pay_amount'] = sum( [x['price'] * x['amount'] for x in data['order_products']]) order = OrderSchema().load(data) session.add(order) session.commit() return json_response(order=OrderSchema().dump(order))
def create_wallet_transaction(): """创建交易 """ data = request.get_json() wallet_transaction = WalletTransactionSchema().load(data) # 采用乐观锁来防止并发情况下可能出现的数据不一致性,也可使用悲观锁(query 时使用 with_for_update),但资源消耗较大 payer = User.query.get(wallet_transaction.payer_id) if payer is None: return json_response(ResponseCode.NOT_FOUND) payee = User.query.get(wallet_transaction.payee_id) if payee is None: return json_response(ResponseCode.NOT_FOUND) count = User.query.filter( and_(User.id == payer.id, User.wallet_money >= wallet_transaction.amount, User.wallet_money == payer.wallet_money) ).update({ User.wallet_money: payer.wallet_money - wallet_transaction.amount }) if count == 0: session.rollback() return json_response(ResponseCode.TRANSACTION_FAILURE) count = User.query.filter( and_(User.id == payee.id, User.wallet_money == payee.wallet_money) ).update({ User.wallet_money: payee.wallet_money + wallet_transaction.amount }) if count == 0: session.rollback() return json_response(ResponseCode.TRANSACTION_FAILURE) session.add(wallet_transaction) session.commit() return json_response(wallet_transaction=WalletTransactionSchema().dump(wallet_transaction))