def get_all_products():
    result = Item.query.all()

    schema = ProductSchema(many=True)
    output = schema.dump(result)

    return jsonify(output), 200
def get_all_users():

    result = User.query.all()

    schema = UserSchema(many=True)
    output = schema.dump(result)

    return jsonify(output), 200
def get_user_orders(current_user):

    result = Orders.query.filter_by(user_id=current_user.id).all()

    schema = OrdersSchema(many=True)
    output = schema.dump(result)

    return jsonify(output), 200
def get_user_cart_items(current_user):

    result = Cart_Items.query.filter_by(cart_id=current_user.cart.id).all()

    schema = CartItemSchema(many=True, exclude=["cart"])
    output = schema.dump(result)

    return jsonify(output), 200
def get_user_defined_items():

    result = Defined_Items.query.all()

    schema = DefinedItemSchema(many=True)
    output = schema.dump(result)

    return jsonify(output), 200
def get_reviews(item_id):
    result = Reviews.query.filter_by(item_id=item_id).all()

    if result:
        schema = ReviewSchema(many=True)
        output = schema.dump(result)

        return jsonify(output), 200
    else:
        return jsonify({'message': 'No comments!'}), 404
def get_info(current_user):
    result = User_Info.query.filter_by(user_id=current_user.id).first()

    if result:
        schema = UserInfoSchema()
        output = schema.dump(result)

        return jsonify(output), 200
    else:
        return jsonify({'message': 'Info not found!'}), 404
def get_user(public_id):
    result = User.query.filter_by(public_id=public_id).first()

    if result:
        schema = UserSchema()
        output = schema.dump(result)

        return jsonify(output), 200
    else:
        return jsonify({'message': 'User not found!'}), 404
def get_user_details(current_user):
    result = User.query.filter_by(id=current_user.id).first()

    if result:
        schema = UserSchema()
        output = schema.dump(result)

        return jsonify(output), 200
    else:
        return jsonify({'message': 'User not found!'}), 404
def check_user(uname):
    result = User.query.filter_by(username=uname).first()

    if result:
        schema = UserSchema()
        output = schema.dump(result)

        return jsonify(output), 200
    else:
        return jsonify({'message': 'User not found!'}), 404
def get_product(item_id):
    result = Item.query.filter_by(id=item_id).first()

    if result:
        schema = ProductSchema()
        output = schema.dump(result)

        return jsonify(output), 200
    else:
        return jsonify({'message': 'Item not found!'}), 404
def create_user_order(current_user):

    new_order = Orders(paid=False, user_id=current_user.id)
    db.session.add(new_order)

    cart_items = Cart_Items.query.filter_by(cart_id=current_user.cart.id).all()
    amount = 0
    for item in cart_items:

        new_order_item = Order_Items(defined_item_id=item.id,
                                     order_id=new_order.id)
        db.session.add(new_order_item)
        amount += item.defined_item.item.price * item.defined_item.amount

    new_order.price = amount
    db.session.commit()

    schema = OrdersSchema(many=False)
    output = schema.dump(new_order)

    return jsonify(output), 200