def delete_product_from_cart(cart_id):
     sql = "DELETE FROM product_cart WHERE product_cart_id = %s RETURNING *"
     cursor = connection.cursor()
     cursor.execute(sql, [cart_id])
     connection.commit()
     record = cursor.fetchone()
     if record:
         return ProductCart(record[0], record[1], record[2], record[3], record[4]).json()
     else:
         raise ResourceNotFound(f"Cart with ID {cart_id} does not exist. Please try again.")
 def return_largest_order_number(cls):
     sql = "SELECT MAX(order_number) FROM orders"
     cursor = connection.cursor()
     cursor.execute(sql)
     connection.commit()
     record = cursor.fetchone()
     if not record[0]:
         record = 1
     number = re.sub("[^0-9]", "", str(record))
     return int(number) + 1
 def delete_cart_items_from_user_id(user_id):
     try:
         print(user_id)
         sql = "DELETE  FROM product_cart WHERE user_id = %s"
         cursor = connection.cursor()
         print("Here1")
         cursor.execute(sql, [user_id])
         print("Here2")
         connection.commit()
         return "Cart Deleted"
     except Exception as e:
         raise ResourceNotFound(f"Order does not exist. Please try again.")
    def get_all_products():
        sql = "SELECT * FROM product_cart"
        cursor = connection.cursor()
        cursor.execute(sql)
        connection.commit()
        records = cursor.fetchall()
        cart_list = []
        for record in records:
            product = ProductCart(record[0], record[1], record[2], record[3], record[4])
            cart_list.append(product.json())

        return cart_list
 def add_product(product_cart):
     sql = "INSERT INTO product_cart VALUES(default,%s, %s, %s, %s, %s) RETURNING *"
     cursor = connection.cursor()
     cursor.execute(sql, [product_cart.product_id, product_cart.user_id,
                          product_cart.product_name, product_cart.product_price, product_cart.quantity])
     connection.commit()
     record = cursor.fetchone()
     if record:
         returned_product = ProductCart(record[0], record[1], record[2], record[3], record[4], record[5])
         return returned_product
     else:
         raise ResourceNotFound(f"User with ID {product_cart.user_id} does not exist. Please try again.")
 def get_all_products_from_cart_by_user_id(user_id):
     sql = "SELECT * FROM product_cart where user_id = %s"
     cursor = connection.cursor()
     cursor.execute(sql, [user_id])
     connection.commit()
     records = cursor.fetchall()
     cart_list = []
     if records:
         for record in records:
             product = ProductCart(record[0], record[1], record[2], record[3], record[4], record[5])
             cart_list.append(product)
         return cart_list
     else:
         raise ResourceNotFound(f"User with ID {user_id} does not exist. Please try again.")
    def add_order(cls, user_id):
        try:
            order_num = cls.return_largest_order_number()
            products_in_cart = ProductCartDAO.get_all_products_from_cart_by_user_id(
                user_id)
            data = []
            for cart_item in products_in_cart:
                data.append((order_num, cart_item.quantity,
                             cart_item.product_id, user_id))
            sql = "INSERT INTO orders values(default, %s, %s, %s, %s) RETURNING *"
            cursor = connection.cursor()
            cursor.executemany(sql, data)
            connection.commit()

            ProductCartDAO.delete_cart_items_from_user_id(user_id)

            return "Order submitted", 200
        except Exception as e:
            raise ResourceNotFound(f"Order does not exist. Please try again.")