Untitled
user_8780899
plain_text
10 days ago
1.9 kB
5
Indexable
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
class CustomerCreate(BaseModel):
name: str
email: str
age: int
class CustomerResponse(BaseModel):
name: str
app = FastAPI()
tasks=[]
@app.get("/")
def read_root():
return {"message": "Welcome to BookShelf"}
@app.get("/books")
def get_books():
books = [
{"title": "The Alchemist", "author": "Paulo Coelho"},
{"title": "1984", "author": "George Orwell"},
]
return books
@app.get("/books/{book_id}")
def get_book(book_id: int):
books = [
{"title": "The Alchemist", "author": "Paulo Coelho"},
{"title": "1984", "author": "George Orwell"},
]
return books[book_id]
@app.post("/tasks", status_code=201)
def create_task(task: dict):
new_task = {
"id": len(tasks) + 1,
"title": task["title"],
"done": False,
}
tasks.append(new_task)
return tasks
@app.post("/customers",response_model=CustomerResponse)
def create_customer(customer: CustomerCreate):
return {
"name":customer.name,
"age":customer.age,
"email":customer.email
}
temp.py code
from pydantic import BaseModel,Field,field_validator
from typing import Optional
class CustomerCreate(BaseModel):
name : str
email: str
age: Optional[int] = None
@field_validator("email")
@classmethod
def email_not_contain(cls,value):
if "@" not in value:
raise ValueError("doesnt contain @")
return value
c = CustomerCreate(name ="Alice",email="amangmail.com",age=21)
print(c)
class item(BaseModel):
prouduct_name:str
quantity:int
price:float
class order(BaseModel):
customer_name:str
deleivery_addres:strEditor is loading...
Leave a Comment