testfastapi1 / app.py
geeksiddhant's picture
Update app.py
d09bbf9 verified
raw
history blame
1.04 kB
from pydantic import BaseModel
from fastapi import FastAPI
from typing import List, Optional
app = FastAPI()
# 1. Define the blueprint for APIs
class Customer(BaseModel):
name: str
email: str
phone: Optional[str] = None
address: Optional[str] = None
customers_list = []
# Create
@app.post("/customers", response_model=Customer)
def create_customer(customer: Customer):
customers_list.append(customer)
return customer
# Read
@app.get("/customers", response_model=List[Customer])
def get_customers():
return customers_list
#Update
@app.put("/customers/{customer_id}", response_model=Customer)
def update_customer(customer_id: int, customer: Customer):
customers_list[customer_id] = customer
return customer
#Delete
@app.delete("/customers/{customer_id}", response_model=Customer)
def delete_customer(customer_id: int):
customers_list.pop(customer_id)
return {"message": "Customer deleted successfully"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)