CoolFace
Apppublic

AliMalik2003/ecommerce_flask_flutter_app

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
app.py201 linesDownload Raw Back to root
1from flask import Flask, jsonify, request
2from flask_cors import CORS
3
4app = Flask(__name__)
5
6
7PRODUCTS = [
8    {
9        "id": 1,
10        "name": "Nike Air Max 270",
11        "category": "Shoes",
12        "price": 12500,
13        "description": "Lightweight running shoes with Air Max cushioning.",
14        "stock": 40,
15        "image_url": "https://via.placeholder.com/300x300?text=Nike+Air+Max"
16    },
17    {
18        "id": 2,
19        "name": "Samsung Galaxy A54",
20        "category": "Phones",
21        "price": 89999,
22        "description": "6.4-inch AMOLED display, 5000mAh battery, 128GB storage.",
23        "stock": 15,
24        "image_url": "https://via.placeholder.com/300x300?text=Samsung+A54"
25    },
26    {
27        "id": 3,
28        "name": "Levi's 511 Slim Jeans",
29        "category": "Clothing",
30        "price": 6999,
31        "description": "Classic slim-fit jeans in stretch denim, dark wash.",
32        "stock": 60,
33        "image_url": "https://via.placeholder.com/300x300?text=Levis+511"
34    },
35    {
36        "id": 4,
37        "name": "Anker PowerCore 20000",
38        "category": "Electronics",
39        "price": 4500,
40        "description": "20000mAh portable charger, dual USB-A, USB-C output.",
41        "stock": 30,
42        "image_url": "https://via.placeholder.com/300x300?text=Anker+PowerCore"
43    },
44    {
45        "id": 5,
46        "name": "Dawlance Microwave 25L",
47        "category": "Home Appliances",
48        "price": 18500,
49        "description": "25-litre solo microwave with 5 power levels and timer.",
50        "stock": 10,
51        "image_url": "https://via.placeholder.com/300x300?text=Dawlance+MW"
52    },
53    {
54        "id": 6,
55        "name": "Logitech MX Master 3",
56        "category": "Electronics",
57        "price": 15000,
58        "description": "Ergonomic wireless mouse, 8K DPI, USB-C charging.",
59        "stock": 20,
60        "image_url": "https://via.placeholder.com/300x300?text=MX+Master+3"
61    },
62    {
63        "id": 7,
64        "name": "Gul Ahmed Lawn Suit",
65        "category": "Clothing",
66        "price": 3200,
67        "description": "3-piece unstitched lawn suit with printed dupatta.",
68        "stock": 80,
69        "image_url": "https://via.placeholder.com/300x300?text=Gul+Ahmed"
70    },
71    {
72        "id": 8,
73        "name": "Haier 1.5 Ton AC",
74        "category": "Home Appliances",
75        "price": 95000,
76        "description": "Inverter split AC, energy-saving, turbo cool mode.",
77        "stock": 8,
78        "image_url": "https://via.placeholder.com/300x300?text=Haier+AC"
79    },
80    {
81        "id": 9,
82        "name": "JBL Tune 510BT",
83        "category": "Electronics",
84        "price": 8500,
85        "description": "Wireless on-ear headphones, 40hr battery, foldable.",
86        "stock": 25,
87        "image_url": "https://via.placeholder.com/300x300?text=JBL+510BT"
88    },
89    {
90        "id": 10,
91        "name": "Nestle Pure Life 12-pack",
92        "category": "Groceries",
93        "price": 480,
94        "description": "12 x 500ml mineral water bottles.",
95        "stock": 200,
96        "image_url": "https://via.placeholder.com/300x300?text=Nestle+Water"
97    },
98    {
99        "id": 11,
100        "name": "HP 15 Laptop",
101        "category": "Electronics",
102        "price": 145000,
103        "description": "Intel Core i5, 8GB RAM, 512GB SSD, 15.6-inch FHD.",
104        "stock": 12,
105        "image_url": "https://via.placeholder.com/300x300?text=HP+Laptop"
106    },
107    {
108        "id": 12,
109        "name": "Bonanza Satrangi Kurta",
110        "category": "Clothing",
111        "price": 2800,
112        "description": "Men's embroidered cotton kurta, regular fit.",
113        "stock": 50,
114        "image_url": "https://via.placeholder.com/300x300?text=Bonanza+Kurta"
115    },
116]
117
118
119
120@app.route("/")
121def hello_world():
122    return ("Hello World")
123
124@app.route("/products", methods = ["GET"])
125def get_all_products():
126    return jsonify(PRODUCTS), 200
127
128@app.route("/products/<int:product_id>", methods = ["GET"])
129def get_product_by_id(product_id):
130    product = next((i for i in PRODUCTS if i["id"] == product_id), None)
131
132    if product == None:
133        return jsonify("Item not found"), 500
134    else:
135        return jsonify(product), 200
136    # index = -1
137    # for i in PRODUCTS:
138    #     if i["id"] == product_id:
139    #         index = i
140    
141    # if index == -1:
142    #     return jsonify("Product not found"), 500
143    # else:
144    #     return jsonify(index),200
145def max_existing_id():
146    max = -1
147    for i in PRODUCTS:
148        if max < i["id"]:
149            max = i["id"]
150    return max
151
152
153@app.route("/products/add", methods = ["POST"])
154def add_product():
155    data = request.get_json()
156
157    req_fields = ["name","category", "price", "description" , "stock"]
158
159    if not data:
160         return jsonify("Json response required"), 404
161
162    missing_fields = [field for field in req_fields if field not in data]
163    if missing_fields:
164            return jsonify(f"Missing field error: {missing_fields}"), 404
165    else:
166            new_id = max_existing_id() + 1
167            product = {
168                "id" : new_id,
169                "name" : data["name"],
170                "category" : data["category"],
171                "price" : data["price"],
172                "description" : data["description"],
173                "stock" : data["stock"],
174                "image_url" : "https://via.placeholder.com/300x300?text=Bonanza+Kurta"
175
176            }
177            PRODUCTS.append(product)
178            return jsonify(product), 200
179    
180@app.route("/products/delete", methods = ["DELETE"])
181def delete_product_by_id():
182     data = request.get_json()
183     if not data:
184          return jsonify("JSON response required only"), 404
185     product_to_delete = next((i for i in PRODUCTS if i["id"] == data["id"]),None)
186
187     if product_to_delete != None:
188               PRODUCTS.remove(product_to_delete)
189               return jsonify(product_to_delete), 200
190
191
192
193    
194
195
196
197
198
199
200if __name__ == "__main__":
201    app.run(debug=True)