File size: 5,009 Bytes
bbef16b
 
 
 
 
b5e9668
 
 
 
 
bbef16b
 
 
051dec3
bbef16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e538957
c271ba7
9cb264a
c271ba7
e538957
bbef16b
 
 
e538957
bbef16b
 
 
 
e538957
bbef16b
f553204
bbef16b
e538957
bbef16b
 
 
e538957
bbef16b
 
 
e538957
bbef16b
 
 
e538957
bbef16b
 
 
7e2efd0
e538957
bbef16b
 
e538957
bbef16b
e538957
bbef16b
 
 
 
 
e538957
bbef16b
0854db3
bbef16b
0854db3
e538957
 
bbef16b
e538957
bbef16b
0854db3
bbef16b
0854db3
5ce52ad
e538957
bbef16b
d9bf42a
2c00ac0
 
 
 
 
 
 
 
 
 
 
 
6ca711c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List
from ecommerce_fake_data import EcommerceFakeData

app = FastAPI(
    title="E-Commerce API",
    description="API for managing customers, products, sellers, orders, and payments in an e-commerce platform.",
    version="1.0.0"
)

efd = EcommerceFakeData()

efd.generate(n_cust=5000, n_prod=300, n_sell=300, n_ord=10000)

class Customer(BaseModel):
    customer_id: str
    name: str
    email: str
    lat: float
    lon: float
    signup_date: str

class Product(BaseModel):
    product_id: str
    category: str
    price: float

class Seller(BaseModel):
    seller_id: str
    lat: float
    lon: float

class Order(BaseModel):
    order_id: str
    customer_id: str
    product_id: str
    seller_id: str
    order_date: str
    quantity: int
    total_amount: float

class Payment(BaseModel):
    payment_id: str
    order_id: str
    payment_method: str
    payment_status: str
    payment_date: str

# GET
@app.get("/", summary="Home", description="Returns a welcome message to the user.")
def home():
    return {"Hello": "Welcome to the E-Commerce API!"}

@app.get("/customers/", response_model=List[Customer], summary="Get all customers", description="Retrieves a list of all customers.", tags=["Customers"])
def get_customers():
    return efd.customers

@app.get("/customer_id/{customer_id}", response_model=Customer, summary="Get a customer by ID", description="Returns a specific customer by the provided ID.", tags=["Customers"])
def get_customer_by_id(customer_id: str):
    customer = next((c for c in efd.customers if c["customer_id"] == customer_id), None)

    if customer is None:
        raise HTTPException(status_code=404, detail="Customer not found")

    return customer

@app.get("/products/", response_model=List[Product], summary="Get all products", description="Returns a list of all products.", tags=["Products"])
def get_products():
    return efd.products

@app.get("/sellers/", response_model=List[Seller], summary="Get all sellers", description="Returns a list of all sellers.", tags=["Sellers"])
def get_sellers():
    return efd.sellers

@app.get("/orders/", response_model=List[Order], summary="Get all orders", description="Returns a list of all orders placed.", tags=["Orders"])
def get_orders():
    return efd.orders

@app.get("/payments/", response_model=List[Payment], summary="Get all payments", description="Returns a list of all payments made.", tags=["Payments"])
def get_payments():
    return efd.payments

# POST
@app.post("/customers/", summary="Add a customer", description="Allows you to add a new customer to the customers list.", tags=["Customers"])
def create_customer(customer: Customer):
    efd.customers.append(customer.model_dump())
    return {"message": "Customer successfully added !"}

@app.post("/products/", summary="Add a product", description="Allows you to add a new product to the products list.", tags=["Products"])
def create_product(product: Product):
    efd.products.append(product.model_dump())
    return {"message": "Product successfully added !"}

# PUT
@app.put("/customers/{customer_id}", summary="Update a customer", description="Allows you to update a customer's details by their ID.", tags=["Customers"])
def update_customer(customer_id: str, updated_customer: Customer):
    for index, customer in enumerate(efd.customers):
        if customer["customer_id"] == customer_id:
            efd.customers[index] = updated_customer.model_dump()
            return {"message": "Customer successfully updated !"}
    raise HTTPException(status_code=404, detail="Customer not found")

@app.put("/products/{product_id}", summary="Update a product", description="Allows you to update a product's details by its ID.", tags=["Products"])
def update_product(product_id: str, updated_product: Product):
    for index, product in enumerate(efd.products):
        if product["product_id"] == product_id:
            efd.products[index] = updated_product.model_dump()
            return {"message": "Product successfully updated !"}
    raise HTTPException(status_code=404, detail="Product not found")

#DELETE
# @app.delete("/customers/{customer_id}", summary="Delete a customer", description="Allows you to delete a customer by their ID.", tags=["Customers"])
# def delete_customer(customer_id: str):
#     initial_length = len(efd.customers)
#     efd.customers = [c for c in efd.customers if c["customer_id"] != customer_id]
#     if len(efd.customers) < initial_length:
#          return {"message": "Customer successfully deleted !"}
#     raise HTTPException(status_code=404, detail="Customer not found")

# @app.delete("/products/{product_id}", summary="Delete a product", description="Allows you to delete a product by its ID.", tags=["Products"])
# def delete_product(product_id: str):
#     efd.products = [p for p in efd.products if p["product_id"] != product_id]
#     return {"message": "Product successfully deleted !"}
#test