atharvad999/enrollment_API
0
1from pydantic import BaseModel2from fastapi import FastAPI,HTTPException3from typing import List,Optional4import uvicorn5 6 7app = FastAPI()8 9#1 Defining the blueprint for the API10 11class Customer(BaseModel):12 id : int13 name : str14 email : str15 address : Optional[str] = None16 phone : Optional[str] = None17 18 19customer_list = [ ]20 21#Create22@app.post("/customers", response_model = Customer)23 24def add_customer(customer : Customer):25 customer_list.append(customer)26 return customer27 28 29#Read30@app.get("/customers", response_model= List[Customer])31 32def get_customer():33 return customer_list34 35 36 37#Update38@app.put("/customers/{id}", response_model = Customer)39 40def update_customer(id : int, customer : Customer):41 for i,existing_customer in enumerate(customer_list):42 if existing_customer.id == id:43 customer_list[i] = customer44 return customer45 raise HTTPException(status_code = 404, detail = "Customer not found") 46 47 48 49 50#Delete51 52@app.delete("/customers/{id}", response_model = Customer)53 54def delete_customer(id : int):55 for i,existing_customer in enumerate(customer_list):56 if existing_customer.id == id:57 deleted_customer = customer_list.pop(i)58 return deleted_customer59 raise HTTPException(status_code = 404, detail = "Customer not found")60 61 62 63if __name__ == "__main__":64 uvicorn.run(app,host="0.0.0.0",port = 7860)65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 