-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (63 loc) · 1.58 KB
/
main.py
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
from fastapi import FastAPI
from pydantic import BaseModel
from products import products
app = FastAPI()
class Product(BaseModel):
name: str
quantity: int
price: float
description: str | None = None
@app.get("/")
async def listProducts():
status = ""
if len(products) >= 1:
status = 200
else:
status = 204
return {
"status": status,
"message": products
}
@app.post("/product/")
async def createProduct(product: Product):
products.append({
"name": product.name,
"quantity": product.quantity,
"price": product.price,
"description": product.description
})
return {
"status": "200",
"mensagem": f"{product.name} cadastrado com sucesso!"
}
@app.put("/product/{name}")
async def updateProduct(name: str, product: Product):
status = 204
msg = ""
for p in products:
if p["name"] == name:
p["name"] = product.name
p["quantity"] = product.quantity
p["price"] = product.price
p["description"] = product.description
msg = "alteracao feita com sucesso"
status = 200
break
return {
"status": status,
"messagem": msg
}
@app.delete("/product/{name}")
async def deleteProdutc(name: str):
msg = ""
status = 204
for i in range(0, len(products)):
if products[i]["name"] == name:
products.pop(i)
status = 200
msg = "produto excluido"
break
return {
"status": status,
"msg": msg
}