def update_product(): try: auth_token = request.headers.get("auth_token") except KeyError: return json.dumps({"status": False, "message": "provide auth token"}) data = decode_auth_token(auth_token) if data["role"] == "owner" or data["role"] == "admin": try: product_id = request.json["product_id"] product_name = request.json["product_name"] product_price = request.json["product_price"] product_category_id = request.json["product_category_id"] except KeyError: return json.dumps({"status": False, "message": "Enter all fields"}) result = update_product_db(product_id, product_name, product_price, product_category_id) return result else: return json.dumps({ "status": False, "message": "User not aloowed to modify product" })
def my_cart(): try: auth_token = request.headers.get("auth_token") except KeyError: return json.dumps({"status": False, "message": "No auth token"}) data = decode_auth_token(auth_token) result = get_my_cart(data["user_id"]) return json.dumps({"status": True, "cart": str(result)})
def place_order(): try: auth_token = request.headers.get("auth_token") except KeyError: return json.dumps({"status": False, "message": "No auth token"}) data = decode_auth_token(auth_token) try: product_id = request.json["product_id"] except KeyError: return json.dumps({"status": False, "message": "select product"}) result = place_order_db(data["user_id"], product_id) return json.dumps({"status": True, "message": "Order placed"})
def add_to_cart(): try: auth_token = request.headers.get("auth_token") except KeyError: return json.dumps({"status": False, "message": "No auth token"}) try: product_id = request.json["product_id"] except KeyError: return json.dumps({"status": False, "message": "No product selected"}) data = decode_auth_token(auth_token) result = add_to_cart_db(data["user_id"], product_id) return json.dumps({"status": True, "message": "Product added to cart"})
def delete_product(): try: auth_token = request.headers.get("auth_token") except KeyError: return json.dumps({"status": False, "message": "No auth token"}) data = decode_auth_token(auth_token) if data["role"] == "owner" or data["role"] == "admin": try: product_id = request.json["product_id"] except KeyError: return json.dumps({"status": False, "message": "Enter all fields"}) result = delete_product_db(product_id) return result else: return json.dumps({ "status": True, "message": "User not allowed to delete product" })
def add_product(): try: auth_token = request.headers.get("auth_token") except KeyError: return json.dumps({ "status": False, "message": "Log in to add product" }) data = decode_auth_token() if data["role"] == "owner": try: productname = request.json["productname"] productcategory_id = request.json["productcategory_id"] except KeyError: return json.dumps({"status": False, "message": "Enter all fields"}) data = add_product_db(productname, productcategory_id) return data else: return json.dumps({ "status": False, "message": "user not allowed to add product" })