Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,64 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
def greet_json():
|
7 |
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
6 |
+
# Initialize the arrays
|
7 |
+
Greet = []
|
8 |
+
Bye = []
|
9 |
+
Sleep = []
|
10 |
+
Care = []
|
11 |
+
|
12 |
+
class Operation(BaseModel):
|
13 |
+
a: str
|
14 |
+
b: str
|
15 |
+
c: str
|
16 |
+
|
17 |
@app.get("/")
|
18 |
def greet_json():
|
19 |
return {"Hello": "World!"}
|
20 |
+
|
21 |
+
@app.post("/add/")
|
22 |
+
async def add(op: Operation):
|
23 |
+
if op.a == 'Check':
|
24 |
+
target_list = None
|
25 |
+
if op.b == 'Greet':
|
26 |
+
target_list = Greet
|
27 |
+
elif op.b == 'Bye':
|
28 |
+
target_list = Bye
|
29 |
+
elif op.b == 'Sleep':
|
30 |
+
target_list = Sleep
|
31 |
+
elif op.b == 'Care':
|
32 |
+
target_list = Care
|
33 |
+
|
34 |
+
if target_list is not None:
|
35 |
+
target_list.append(op.c)
|
36 |
+
return {"result": target_list}
|
37 |
+
else:
|
38 |
+
return {"result": "Invalid category"}
|
39 |
+
else:
|
40 |
+
return {"result": "Invalid operation"}
|
41 |
+
|
42 |
+
@app.post("/remove/")
|
43 |
+
async def remove(op: Operation):
|
44 |
+
if op.a == 'Check':
|
45 |
+
target_list = None
|
46 |
+
if op.b == 'Greet':
|
47 |
+
target_list = Greet
|
48 |
+
elif op.b == 'Bye':
|
49 |
+
target_list = Bye
|
50 |
+
elif op.b == 'Sleep':
|
51 |
+
target_list = Sleep
|
52 |
+
elif op.b == 'Care':
|
53 |
+
target_list = Care
|
54 |
+
|
55 |
+
if target_list is not None:
|
56 |
+
if op.c in target_list:
|
57 |
+
target_list.remove(op.c)
|
58 |
+
return {"result": target_list}
|
59 |
+
else:
|
60 |
+
return {"result": "Element not found in the list"}
|
61 |
+
else:
|
62 |
+
return {"result": "Invalid category"}
|
63 |
+
else:
|
64 |
+
return {"result": "Invalid operation"}
|