Spaces:
Running
Running
sdacelo commited on
Commit ·
bbef16b
1
Parent(s): 7235198
update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
from ecommerce_fake_data import EcommerceFakeData
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
efd = EcommerceFakeData()
|
| 9 |
+
|
| 10 |
+
efd.generate(n_cust=500, n_prod=300, n_sell=300, n_ord=1000)
|
| 11 |
+
|
| 12 |
+
class Customer(BaseModel):
|
| 13 |
+
customer_id: str
|
| 14 |
+
name: str
|
| 15 |
+
email: str
|
| 16 |
+
lat: float
|
| 17 |
+
lon: float
|
| 18 |
+
signup_date: str
|
| 19 |
+
|
| 20 |
+
class Product(BaseModel):
|
| 21 |
+
product_id: str
|
| 22 |
+
category: str
|
| 23 |
+
price: float
|
| 24 |
+
|
| 25 |
+
class Seller(BaseModel):
|
| 26 |
+
seller_id: str
|
| 27 |
+
lat: float
|
| 28 |
+
lon: float
|
| 29 |
+
|
| 30 |
+
class Order(BaseModel):
|
| 31 |
+
order_id: str
|
| 32 |
+
customer_id: str
|
| 33 |
+
product_id: str
|
| 34 |
+
seller_id: str
|
| 35 |
+
order_date: str
|
| 36 |
+
quantity: int
|
| 37 |
+
total_amount: float
|
| 38 |
+
|
| 39 |
+
class Payment(BaseModel):
|
| 40 |
+
payment_id: str
|
| 41 |
+
order_id: str
|
| 42 |
+
payment_method: str
|
| 43 |
+
payment_status: str
|
| 44 |
+
payment_date: str
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# GET
|
| 48 |
+
@app.get("/customers/", response_model=List[Customer])
|
| 49 |
+
def get_customers():
|
| 50 |
+
return efd.customers
|
| 51 |
+
|
| 52 |
+
@app.get("/customer_id/{customer_id}", response_model=str)
|
| 53 |
+
def get_customer_by_id(customer_id: str):
|
| 54 |
+
customer = next((c for c in efd.customers if c["customer_id"] == customer_id), None)
|
| 55 |
+
|
| 56 |
+
if customer is None:
|
| 57 |
+
raise HTTPException(status_code=404, detail="Client not found")
|
| 58 |
+
|
| 59 |
+
return customer["customer_id"]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@app.get("/products/", response_model=List[Product])
|
| 63 |
+
def get_products():
|
| 64 |
+
return efd.products
|
| 65 |
+
|
| 66 |
+
@app.get("/sellers/", response_model=List[Seller])
|
| 67 |
+
def get_sellers():
|
| 68 |
+
return efd.sellers
|
| 69 |
+
|
| 70 |
+
@app.get("/orders/", response_model=List[Order])
|
| 71 |
+
def get_orders():
|
| 72 |
+
return efd.orders
|
| 73 |
+
|
| 74 |
+
@app.get("/payments/", response_model=List[Payment])
|
| 75 |
+
def get_payments():
|
| 76 |
+
return efd.payments
|
| 77 |
+
|
| 78 |
+
# POST
|
| 79 |
+
@app.post("/customers/")
|
| 80 |
+
def create_customer(customer: Customer):
|
| 81 |
+
efd.customers.append(customer.model_dump())
|
| 82 |
+
return {"message": "Client successfuly added !"}
|
| 83 |
+
|
| 84 |
+
@app.post("/products/")
|
| 85 |
+
def create_product(product: Product):
|
| 86 |
+
efd.products.append(product.model_dump())
|
| 87 |
+
return {"message": "Product successfully added !"}
|
| 88 |
+
|
| 89 |
+
# PUT
|
| 90 |
+
@app.put("/customers/{customer_id}")
|
| 91 |
+
def update_customer(customer_id: str, updated_customer: Customer):
|
| 92 |
+
for customer in efd.customers:
|
| 93 |
+
if customer["customer_id"] == customer_id:
|
| 94 |
+
customer.update(updated_customer.model_dump())
|
| 95 |
+
return {"message": "Client successfully updated !"}
|
| 96 |
+
raise HTTPException(status_code=404, detail="Client not found")
|
| 97 |
+
|
| 98 |
+
@app.put("/products/{product_id}")
|
| 99 |
+
def update_product(product_id: str, updated_product: Product):
|
| 100 |
+
for product in efd.products:
|
| 101 |
+
if product["product_id"] == product_id:
|
| 102 |
+
product.update(updated_product.model_dump())
|
| 103 |
+
return {"message": "Product successfully updated "}
|
| 104 |
+
raise HTTPException(status_code=404, detail="Produit not found")
|
| 105 |
+
|
| 106 |
+
# DELETE
|
| 107 |
+
@app.delete("/customers/{customer_id}")
|
| 108 |
+
def delete_customer(customer_id: str):
|
| 109 |
+
efd.customers = [c for c in efd.customers if c["customer_id"] != customer_id]
|
| 110 |
+
return {"message": "Client succsessfully deleted !"}
|
| 111 |
+
|
| 112 |
+
@app.delete("/products/{product_id}")
|
| 113 |
+
def delete_product(product_id: str):
|
| 114 |
+
efd.products = [p for p in efd.products if p["product_id"] != product_id]
|
| 115 |
+
return {"message": "Product successfully deleted !"}
|