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