geeksiddhant/enrollmentAPI
0
1from typing import List, Optional2from pydantic import BaseModel3from fastapi import FastAPI, HTTPException4import uvicorn5 6app = FastAPI()7 8# 1. Define the blueprint for APIs9class Customer(BaseModel):10 id: int11 name: str12 email: str13 phone: Optional[str] = None14 address: Optional[str] = None15 16# 2. Create the API endpoint17 18customers_list = []19#Create20@app.post("/customers", response_model=Customer)21def create_customer(customer: Customer):22 customers_list.append(customer)23 return customer24 25#Read26@app.get("/customers", response_model=List[Customer])27def get_customers():28 return customers_list29 30#Update31@app.put("/customers/{id}", response_model=Customer)32def update_customer(id: int, customer: Customer):33 for i, existing_customer in enumerate(customers_list):34 if existing_customer.id == id:35 customers_list[i] = customer36 return customer37 raise HTTPException(status_code=404, detail="Customer not found")38 39#Delete40@app.delete("/customers/{id}", response_model=Customer)41def delete_customer(id: int):42 for i, customer in enumerate(customers_list):43 if customer.id == id:44 deleted_customer = customers_list.pop(i)45 return deleted_customer46 raise HTTPException(status_code=404, detail="Customer not found")47 48if __name__ == "__main__":49 uvicorn.run(app, host="0.0.0.0", port=7860)50 51 