Exemplo n.º 1
0
def user_buy_book():
    params = get_param()
    book_store_name = params.get('book_store_name', None)
    book_name = params.get('book_name', None)
    user_id = params.get('id', None)
    if book_store_name is None or book_name is None or user_id is None:
        return {
            'result': 'Error!'
        }
    book_store = BookStore.get(book_store_name)
    if book_store is None:
        return {
            'result': 'Book Store is not found!'
        }
    price = Book.get_price_by_store_name_book_name(book_store.storeName, book_name)
    if price is None:
        return {
            'result': 'Book is not found!'
        }
    user = User.get_by_id(user_id)
    if user is None:
        return {
            'result': 'User is not found!'
        }
    user.cashBalance -= price[0]
    book_store.cashBalance += price[0]
    user.update()
    book_store.update()
    purchase = PurchaseHistory(user_id, book_name, book_store_name, price[0], "today")
    purchase.insert()
    return {
        'result': purchage_history_schema.dump(purchase)
    }