Spaces:
Sleeping
Sleeping
| from typing import List, Optional | |
| from pydantic import BaseModel | |
| from fastapi import FastAPI, HTTPException | |
| import uvicorn | |
| app = FastAPI() | |
| #1. define the blueprint for APIs | |
| class Customers(BaseModel): | |
| id: int | |
| name: str | |
| email: str | |
| phone: Optional[str] = None | |
| address: Optional[str] = None | |
| #2. create the APIs endpoint | |
| customers_list = [] | |
| # create | |
| def create_customer(customer: Customers): | |
| customers_list.append(customer) | |
| return customer | |
| # read | |
| def get_customers(): | |
| return customers_list | |
| # update | |
| def update_customer(id: int, customer: Customers): | |
| for i, existing_customer in enumerate(customers_list): | |
| if existing_customer.id == id: | |
| customers_list[i] = customer | |
| return customer | |
| raise HTTPException(status_code=404, detail="Customer not found") | |
| # delete | |
| def delete_customer(id: int): | |
| for i, existing_customer in enumerate(customers_list): | |
| if existing_customer.id == id: | |
| deleted_customer = customers_list.pop(i) | |
| return deleted_customer | |
| raise HTTPException(status_code=404, detail="Customer not found") | |
| #3. run the app | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |