Ejemplo n.º 1
0
def delete_item(current_user, item_id):
    if current_user.admin is False:
        return jsonify(
            {"message": "You are not authorised to perform this function"})
    # r = request.json
    item = Item.get_item_by_id(id=item_id)
    if not item:
        return jsonify({"message": "Item does not exist"})
    try:
        Item.delete_item(id=item_id)
        return jsonify({"message": "{} deleted".format(item["name"])})
    except:
        return jsonify(
            {"message": "An error occurred, Item could not be deleted"})
Ejemplo n.º 2
0
def update_item(current_user, item_id):
    if current_user.admin is False:
        return jsonify(
            {"message": "You are not authorised to perform this function"})
    item = Item.get_item_by_id(id=item_id)
    if not item:
        return jsonify({"message": "Item does not exist"})
    try:
        r = request.json
        Item.update_item(id=item_id, data=r)
        return jsonify({"message": "Item updated"})
    except:
        return jsonify(
            {"message": "An error occurred, Item could not be updated"})
Ejemplo n.º 3
0
def buy(item_id):
    try:
        item = Item.get_item_by_id(id=item_id)
        if not item:
            return jsonify({"message": "Item does not exist"})

        if request.data and request.json["quantity"]:
            quantity = int(request.json["quantity"])
        else:
            quantity = 1
        item["quantity"] -= quantity
        Item.update_item(id=item_id, data=item)
        return jsonify({
            "message":
            "{} unit(s) of {} Purchased".format(quantity, item["name"])
        })
    except:
        return jsonify(
            {"message": "An error occurred, Item could not be purchased"})
Ejemplo n.º 4
0
def view_one_item(item_id):
    item = Item.get_item_by_id(id=item_id)
    if not item:
        return jsonify({"message": "Item does not exist"})
    return jsonify(item)