-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (45 loc) · 1.05 KB
/
app.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
from fastapi import FastAPI
app = FastAPI(title="My App")
from pydantic import BaseModel
data = [
{"id": 2,
"name": "ABC",
"description": "[email protected]"
}
, {"id": 3,
"name": "DEF",
"description": "[email protected]"
}
, {"id": 5,
"name": "GHI",
"description": "[email protected]"
}]
class Item(BaseModel):
id: int
name: str
description: str
@app.get("/save/{id}")
def hello(id: int):
print(data)
for i in data:
if i['id'] == id:
return i
return {"message": "Data not found"}
@app.post("/save")
def add(item: Item):
print(item)
d = {
"id": item.id,
"name": item.name,
"description": item.description
}
data.append(d)
return {"message": "Data saved successfully"}
@app.delete("/save/{id}")
def delete_data(id: int):
if len(data) != 0:
for i in data:
if i['id'] == id:
data.remove(i)
return {"message": "Data deleted"}
return {"message": "Data not found"}