testfastapi1 / app.py
geeksiddhant's picture
Update app.py
e24b1ef verified
raw
history blame contribute delete
950 Bytes
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"}