From 1e15fb97fff99cd9ec0f5e38dd112828691756e3 Mon Sep 17 00:00:00 2001 From: "mw-middleware-labs-sandbox[bot]" <224139880+mw-middleware-labs-sandbox[bot]@users.noreply.github.com> Date: Thu, 27 Nov 2025 07:11:55 +0000 Subject: [PATCH] refactor: Refactor user_profile function for safe user data retrieval and error handling --- flask/app.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/flask/app.py b/flask/app.py index c30b439..b649da2 100644 --- a/flask/app.py +++ b/flask/app.py @@ -60,9 +60,9 @@ def generate_exception(): @app.route('/user/') def user_profile(username): print(f"User profile requested for {username}") - test = user_data[username] + test = user_data.get(username) if not test: - raise + return jsonify({"error": "User not found"}), 404 return jsonify({"message": f"Profile for {username}", "data": test}) @app.route('/process', methods=['POST']) @@ -98,24 +98,24 @@ def get_products(): category = request.args.get('category', '') min_price = request.args.get('min_price', 0) max_price = request.args.get('max_price', float('inf')) - + filtered_products = [] for product in products: if float(product['price']) >= float(min_price) and float(product['price']) <= float(max_price): filtered_products.append(product) - + return jsonify({"products": filtered_products}) @app.route('/orders/') def get_user_orders(user_id): user_orders = [] total_spent = 0 - + for order in orders: if order['user_id'] == user_id: user_orders.append(order) total_spent += order['total'] - + return jsonify({ "orders": user_orders, "total_spent": total_spent, @@ -127,14 +127,14 @@ def update_inventory(): data = request.get_json() product_id = data.get('product_id') quantity = data.get('quantity') - + for product in products: if product['id'] == product_id: product['stock'] -= quantity if product['stock'] < 0: product['stock'] = 0 return jsonify({"status": "success", "new_stock": product['stock']}) - + return jsonify({"status": "error", "message": "Product not found"}) @app.route('/user/stats/') @@ -142,10 +142,10 @@ def user_stats(username): user = user_data.get(username) if not user: return jsonify({"error": "User not found"}) - + user_orders = [order for order in orders if order['user_id'] == username] total_spent = sum(order['total'] for order in user_orders) - + return jsonify({ "user_info": user, "order_count": len(user_orders),