RKrish03 commited on
Commit
28af7f0
·
verified ·
1 Parent(s): 9205b3e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Optional
2
+
3
+ from fastapi import FastAPI, HTTPException
4
+ from pydantic import BaseModel
5
+
6
+ app = FastAPI()
7
+
8
+ # Customer Model
9
+ class Customer(BaseModel):
10
+ id: int
11
+ name: str
12
+ email: str
13
+ phone: Optional[str] = None
14
+ address: Optional[str] = None
15
+
16
+ # In-memory storage
17
+ customers_list: List[Customer] = []
18
+
19
+ # Welcome API
20
+ @app.get("/")
21
+ def welcome():
22
+ return {"message": "Customer API is running"}
23
+
24
+ # Create Customer
25
+ @app.post("/customers", response_model=Customer)
26
+ def create_customer(customer: Customer):
27
+ customers_list.append(customer)
28
+ return customer
29
+
30
+ # Read All Customers
31
+ @app.get("/customers", response_model=List[Customer])
32
+ def get_customers():
33
+ return customers_list
34
+
35
+ # Update Customer
36
+ @app.put("/customers/{id}", response_model=Customer)
37
+ def update_customer(id: int, customer: Customer):
38
+ for i, existing_customer in enumerate(customers_list):
39
+ if existing_customer.id == id:
40
+ customers_list[i] = customer
41
+ return customer
42
+
43
+ raise HTTPException(status_code=404, detail="Customer not found")
44
+
45
+ # Delete Customer
46
+ @app.delete("/customers/{id}")
47
+ def delete_customer(id: int):
48
+ for i, customer in enumerate(customers_list):
49
+ if customer.id == id:
50
+ customers_list.pop(i)
51
+ return {"message": "Customer deleted successfully"}
52
+
53
+ raise HTTPException(status_code=404, detail="Customer not found")
54
+
55
+ if __name__ == "__main__":
56
+ uvicorn.run(
57
+ "app:app",
58
+ host="0.0.0.0",
59
+ port=7860,
60
+ reload=True
61
+ )