Spaces:
Runtime error
Runtime error
creat app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from typing import List, Optional
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# 1. Define the blueprint for APIs
|
| 8 |
+
class Customer(BaseModel):
|
| 9 |
+
name: str
|
| 10 |
+
email: str
|
| 11 |
+
phone: Optional[str] = None
|
| 12 |
+
address: Optional[str] = None
|
| 13 |
+
|
| 14 |
+
customers_list = []
|
| 15 |
+
# Create
|
| 16 |
+
@app.post("/customers", response_model=Customer)
|
| 17 |
+
def create_customer(customer: Customer):
|
| 18 |
+
customers_list.append(customer)
|
| 19 |
+
return customer
|
| 20 |
+
# Read
|
| 21 |
+
@app.get("/customers", response_model=List[Customer])
|
| 22 |
+
def get_customers():
|
| 23 |
+
return customers_list
|
| 24 |
+
#Update
|
| 25 |
+
@app.put("/customers/{customer_id}", response_model=Customer)
|
| 26 |
+
def update_customer(customer_id: int, customer: Customer):
|
| 27 |
+
customers_list[customer_id] = customer
|
| 28 |
+
return customer
|
| 29 |
+
#Delete
|
| 30 |
+
@app.delete("/customers/{customer_id}", response_model=Customer)
|
| 31 |
+
def delete_customer(customer_id: int):
|
| 32 |
+
customers_list.pop(customer_id)
|
| 33 |
+
return {"message": "Customer deleted successfully"}
|