Spaces:
Running
Running
sdacelo commited on
Commit ·
0854db3
1
Parent(s): 9cb264a
update put
Browse files
app.py
CHANGED
|
@@ -95,25 +95,28 @@ def create_product(product: Product):
|
|
| 95 |
# PUT
|
| 96 |
@app.put("/customers/{customer_id}", summary="Update a customer", description="Allows you to update a customer's details by their ID.", tags=["Customers"])
|
| 97 |
def update_customer(customer_id: str, updated_customer: Customer):
|
| 98 |
-
for customer in efd.customers:
|
| 99 |
if customer["customer_id"] == customer_id:
|
| 100 |
-
|
| 101 |
return {"message": "Customer successfully updated !"}
|
| 102 |
raise HTTPException(status_code=404, detail="Customer not found")
|
| 103 |
|
| 104 |
@app.put("/products/{product_id}", summary="Update a product", description="Allows you to update a product's details by its ID.", tags=["Products"])
|
| 105 |
def update_product(product_id: str, updated_product: Product):
|
| 106 |
-
for product in efd.products:
|
| 107 |
if product["product_id"] == product_id:
|
| 108 |
-
|
| 109 |
return {"message": "Product successfully updated !"}
|
| 110 |
raise HTTPException(status_code=404, detail="Product not found")
|
| 111 |
|
| 112 |
# DELETE
|
| 113 |
@app.delete("/customers/{customer_id}", summary="Delete a customer", description="Allows you to delete a customer by their ID.", tags=["Customers"])
|
| 114 |
def delete_customer(customer_id: str):
|
|
|
|
| 115 |
efd.customers = [c for c in efd.customers if c["customer_id"] != customer_id]
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
|
| 118 |
@app.delete("/products/{product_id}", summary="Delete a product", description="Allows you to delete a product by its ID.", tags=["Products"])
|
| 119 |
def delete_product(product_id: str):
|
|
|
|
| 95 |
# PUT
|
| 96 |
@app.put("/customers/{customer_id}", summary="Update a customer", description="Allows you to update a customer's details by their ID.", tags=["Customers"])
|
| 97 |
def update_customer(customer_id: str, updated_customer: Customer):
|
| 98 |
+
for index, customer in enumerate(efd.customers):
|
| 99 |
if customer["customer_id"] == customer_id:
|
| 100 |
+
efd.customers[index] = updated_customer.model_dump()
|
| 101 |
return {"message": "Customer successfully updated !"}
|
| 102 |
raise HTTPException(status_code=404, detail="Customer not found")
|
| 103 |
|
| 104 |
@app.put("/products/{product_id}", summary="Update a product", description="Allows you to update a product's details by its ID.", tags=["Products"])
|
| 105 |
def update_product(product_id: str, updated_product: Product):
|
| 106 |
+
for index, product in enumerate(efd.products):
|
| 107 |
if product["product_id"] == product_id:
|
| 108 |
+
efd.products[index] = updated_product.model_dump()
|
| 109 |
return {"message": "Product successfully updated !"}
|
| 110 |
raise HTTPException(status_code=404, detail="Product not found")
|
| 111 |
|
| 112 |
# DELETE
|
| 113 |
@app.delete("/customers/{customer_id}", summary="Delete a customer", description="Allows you to delete a customer by their ID.", tags=["Customers"])
|
| 114 |
def delete_customer(customer_id: str):
|
| 115 |
+
initial_length = len(efd.customers)
|
| 116 |
efd.customers = [c for c in efd.customers if c["customer_id"] != customer_id]
|
| 117 |
+
if len(efd.customers) < initial_length:
|
| 118 |
+
return {"message": "Customer successfully deleted !"}
|
| 119 |
+
raise HTTPException(status_code=404, detail="Customer not found")
|
| 120 |
|
| 121 |
@app.delete("/products/{product_id}", summary="Delete a product", description="Allows you to delete a product by its ID.", tags=["Products"])
|
| 122 |
def delete_product(product_id: str):
|