File size: 950 Bytes
d71bfc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d09bbf9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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"}