Repository navigation
Expand file tree
/
Copy pathHW-responses.py
More file actions
128 lines (120 loc) · 3.26 KB
/
Copy pathHW-responses.py
File metadata and controls
128 lines (120 loc) · 3.26 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
app = FastAPI()
class Product(BaseModel):
name: str = Field(min_length=2)
price: float = Field(gt=0)
quantity: int = Field(gt=0)
category: str
class ProductResponse(BaseModel):
id: int
name: str
price: float
quantity: int
category: str
message: str
products = [
{
"id": 1,
"name": "Keyboard",
"price": 1200,
"quantity": 10,
"category": "Computer"
},
{
"id": 2,
"name": "Mouse",
"price": 600,
"quantity": 15,
"category": "Computer"
},
{
"id": 3,
"name": "Monitor",
"price": 8000,
"quantity": 5,
"category": "Electronics"
},
{
"id": 4,
"name": "Chair",
"price": 3000,
"quantity": 20,
"category": "Furniture"
}
]
@app.get("/products")
def get_products(category: str = None):
if category:
filtered = []
for p in products:
if p["category"].lower() == category.lower():
filtered.append(p)
return JSONResponse(
status_code=200,
content={
"status": "success",
"message": f"Products filtered by category '{category}'",
"total_products": len(filtered),
"data": filtered
}
)
return {
"status": "success",
"message": "Products retrieved successfully",
"total_products": len(products),
"data": products
}
@app.get("/products/{product_id}")
def get_product(product_id: int):
for product in products:
if product["id"] == product_id:
return {
"status": "success",
"message": "Product found successfully",
"data": product
}
return JSONResponse(
status_code=404,
content={
"status": "error",
"message": "Product not found",
"data": None
}
)
@app.post("/products", status_code=201)
def create_product(product: Product):
new_id = len(products) + 1
new_product = {
"id": new_id,
"name": product.name,
"price": product.price,
"quantity": product.quantity,
"category": product.category
}
products.append(new_product)
return {
"status": "success",
"message": "Product created successfully",
"data": new_product
}
@app.post("/products/with-response-model", response_model=ProductResponse, status_code=201)
def create_product_with_response_model(product: Product):
new_id = len(products) + 1
new_product = {
"id": new_id,
"name": product.name,
"price": product.price,
"quantity": product.quantity,
"category": product.category
}
products.append(new_product)
return {
"id": new_id,
"name": product.name,
"price": product.price,
"quantity": product.quantity,
"category": product.category,
"message": "Product created successfully"
}