from flask import Flask, request, jsonify import logging from datetime import datetime from swagger import setup_swagger app = Flask(__name__) # Setup logging logging.basicConfig( filename='api.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s' ) products = [ {"id": 1, "name": "Laptop", "price": 5000, "quantity": 10}, {"id": 2, "name": "Mouse", "price": 150, "quantity": 50} ] orders = [] # Home page @app.route('/') def home(): logging.info("GET / - Home page accessed") return jsonify({"message": "Welcome to my first API!"}) # Get all products @app.route('/products', methods=['GET']) def get_products(): logging.info("GET /products - Fetching all products") return jsonify({"products": products}) # Get single product @app.route('/products/', methods=['GET']) def get_product(product_id): logging.info(f"GET /products/{product_id} - Fetching product") for product in products: if product['id'] == product_id: return jsonify(product) logging.error(f"404 - Product {product_id} not found") return jsonify({"error": "Product not found"}), 404 # Add new product @app.route('/products', methods=['POST']) def add_product(): data = request.get_json() logging.info(f"POST /products - Adding new product: {data}") if not data or 'name' not in data or 'price' not in data: logging.error("400 - Missing required fields") return jsonify({"error": "name and price are required"}), 400 new_product = { "id": len(products) + 1, "name": data['name'], "price": data['price'], "quantity": data.get('quantity', 0) } products.append(new_product) return jsonify({"message": "Product added successfully", "product": new_product}), 201 # Update product @app.route('/products/', methods=['PUT']) def update_product(product_id): data = request.get_json() logging.info(f"PUT /products/{product_id} - Updating product") for product in products: if product['id'] == product_id: if 'name' in data: product['name'] = data['name'] if 'price' in data: product['price'] = data['price'] if 'quantity' in data: product['quantity'] = data['quantity'] return jsonify({"message": "Product updated", "product": product}) logging.error(f"404 - Product {product_id} not found for update") return jsonify({"error": "Product not found"}), 404 # Delete product @app.route('/products/', methods=['DELETE']) def delete_product(product_id): logging.info(f"DELETE /products/{product_id} - Deleting product") for i, product in enumerate(products): if product['id'] == product_id: deleted = products.pop(i) return jsonify({"message": "Product deleted", "product": deleted}) logging.error(f"404 - Product {product_id} not found for deletion") return jsonify({"error": "Product not found"}), 404 # Create order @app.route('/orders', methods=['POST']) def create_order(): data = request.get_json() logging.info(f"POST /orders - Creating new order: {data}") if not data or 'product_id' not in data or 'quantity' not in data: logging.error("400 - Missing order information") return jsonify({"error": "product_id and quantity are required"}), 400 # Find product product = None for p in products: if p['id'] == data['product_id']: product = p break if not product: logging.error(f"404 - Product {data['product_id']} not found for order") return jsonify({"error": "Product not found"}), 404 if product['quantity'] < data['quantity']: logging.error("400 - Not enough stock") return jsonify({"error": "Not enough stock"}), 400 # Create order order = { "id": len(orders) + 1, "product_id": data['product_id'], "product_name": product['name'], "quantity": data['quantity'], "total": product['price'] * data['quantity'], "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S") } # Update stock product['quantity'] -= data['quantity'] orders.append(order) return jsonify({"message": "Order created successfully", "order": order}), 201 # Get all orders @app.route('/orders', methods=['GET']) def get_orders(): logging.info("GET /orders - Fetching all orders") return jsonify({"orders": orders}) setup_swagger(app) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)