示例#1
0
def create_address():
    data = request.get_json()
    address = AddressSchema().load(data)
    session.add(address)
    session.commit()

    return json_response(address=AddressSchema().dump(address))
示例#2
0
def update_order(id):
    """更新订单,支持部分更新,但只能更新地址、备注、状态都信息
    注意订单商品要么不更新,要么整体一起更新
    """

    data = request.get_json()

    order = Order.query.get(id)
    if order is None:
        return json_response(ResponseCode.NOT_FOUND)
    if data.get('address_id') is not None:
        order.address_id = data.get('address_id')
    if data.get('note') is not None:
        order.note = data.get('note')
    if data.get('status') is not None:
        order.status = data.get('status')

    if data.get('order_products') is not None:
        order_products = []
        for op in data.get('order_products'):
            order_product = OrderProduct.query.get(op.get('id'))
            if order_product is None:
                return json_response(ResponseCode.NOT_FOUND)
            if op.get('amount') is not None:
                order_product.amount = op.get('amount')
            if op.get('price') is not None:
                order_product.price = op.get('price')
            order_products.append(order_product)
        order.order_products = order_products

    session.commit()

    return json_response(order=OrderSchema().dump(order))
示例#3
0
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))
示例#4
0
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))
示例#5
0
def create_shop():
    data = request.get_json()

    shop = ShopSchema().load(data)
    session.add(shop)
    session.commit()

    return json_response(shop=ShopSchema().dump(shop))
示例#6
0
def create_user():
    data = request.get_json()

    user = UserSchema().load(data)
    session.add(user)
    session.commit()

    return json_response(user=UserSchema().dump(user))
示例#7
0
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))
示例#8
0
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))
示例#9
0
def update_user(id):
	data = request.get_json()
	user = User.query.get(id)
	if user is None:
		return json_response(ResponseCode.NOT_FOUND)
	for key, value in data.items():
		setattr(user, key, value)
	session.commit()
	return json_response(user=UserSchema().dump(user))
示例#10
0
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))
示例#11
0
def update_user(user_id):
    data = request.get_json()

    count = User.query.filter(User.id == user_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    user = User.query.get(user_id)
    session.commit()

    return json_response(user=UserSchema().dump(user))
示例#12
0
def update_wallet_transaction(id):
    data = request.get_json()
    count = WalletTransaction.query.filter(
        WalletTransaction.id == id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    wallet_transaction = WalletTransaction.query.get(id)
    session.commit()
    return json_response(
        wallet_transaction=WalletTransactionSchema().dump(wallet_transaction))
示例#13
0
def delete_cart_products():
    """清空某个用户的购物车商品
    """

    user_id = request.args.get('user_id', type=int)

    CartProduct.query.filter(CartProduct.user_id == user_id).delete()
    session.commit()

    return json_response()
示例#14
0
def update_product(product_id):
    data = request.get_json()

    count = Product.query.filter(Product.id == product_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    product = Product.query.get(product_id)
    session.commit()

    return json_response(product=ProductSchema().dump(product))
示例#15
0
def delete_cart_product(user_id, product_id):
    cart_product = CartProduct.query.filter(
        and_(CartProduct.user_id == user_id,
             CartProduct.product_id == product_id)).first()
    if cart_product is None:
        return json_response(ResponseCode.NOT_FOUND)
    session.delete(cart_product)
    session.commit()

    return json_response(cart_product=CartProductSchema().dump(cart_product))
示例#16
0
def update_order(order_id):
    data = request.get_json()

    count = Order.query.filter(Order.id == order_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    order = Order.query.get(order_id)
    session.commit()

    return json_response(order=OrderSchema().dump(order))
示例#17
0
def update_shop(shop_id):
    data = request.get_json()

    count = Shop.query.filter(Shop.id == shop_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    shop = Shop.query.get(shop_id)
    session.commit()

    return json_response(shop=ShopSchema().dump(shop))
示例#18
0
def create_product():
    """创建商品
    """

    data = request.get_json()

    product = ProductSchema().load(data)
    session.add(product)
    session.commit()

    return json_response(product=ProductSchema().dump(product))
示例#19
0
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))
示例#20
0
def update_address(address_id):
    data = request.get_json()

    count = Address.query.filter(
        Address.id == address_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    address = Address.query.get(address_id)
    session.commit()

    return json_response(address=AddressSchema().dump(address))
示例#21
0
def update_address(id):
    data = request.get_json()
    if data.get('is_default'):
        Address.query.filter(Address.is_default == True).update(
            {'is_default': False})
    count = Address.query.filter(Address.id == id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    address = Address.query.get(id)
    session.commit()
    return json_response(address=AddressSchema().dump(adress))
示例#22
0
def delete_favorite_product(user_id, product_id):
    favorite_product = FavoriteProduct.query.filter(
        and_(FavoriteProduct.user_id == user_id,
             FavoriteProduct.product_id == product_id)).first()
    if favorite_product is None:
        return json_response(ResponseCode.NOT_FOUND)
    session.delete(favorite_product)
    session.commit()

    return json_response(
        favorite_product=FavoriteProductSchema().dump(favorite_product))
示例#23
0
def delete_cart_product(id):
    """删除购物车商品
    """

    cart_product = CartProduct.query.filter(CartProduct.id == id).first()
    if cart_product is None:
        return json_response(ResponseCode.NOT_FOUND)

    session.delete(cart_product)
    session.commit()

    return json_response(cart_product=CartProductSchema().dump(cart_product))
示例#24
0
def delete_favorite_product(id):
    """取消收藏商品
    """

    favorite_product = FavoriteProduct.query.get(id)
    if favorite_product is None:
        return json_response(ResponseCode.NOT_FOUND)
    session.delete(favorite_product)
    session.commit()

    return json_response(
        favorite_product=FavoriteProductSchema().dump(favorite_product))
示例#25
0
def update_cart_product(id):
    """更新购物车商品,比如数量
    """

    data = request.get_json()

    count = CartProduct.query.filter(CartProduct.id == id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    cart_product = CartProduct.query.get(id)
    session.commit()

    return json_response(cart_product=CartProductSchema().dump(cart_product))
示例#26
0
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))