feat(POST): add POST function to insert new data on the DB
This commit is contained in:
parent
daa1061dce
commit
be1d1ff9a6
10
main.py
10
main.py
@ -70,9 +70,9 @@ async def read_item(test_id: int):
|
|||||||
return {"test_string": result}
|
return {"test_string": result}
|
||||||
|
|
||||||
|
|
||||||
@app.put("/test/add")
|
@app.post("/test/add")
|
||||||
async def add_item(test_string: str):
|
async def add_item(test_string: Union[str, None] = None):
|
||||||
if test_string != "":
|
if test_string:
|
||||||
with engine.connect() as conn:
|
with engine.connect() as conn:
|
||||||
trans = conn.begin()
|
trans = conn.begin()
|
||||||
insert = test.insert().values(testString=test_string)
|
insert = test.insert().values(testString=test_string)
|
||||||
@ -81,9 +81,11 @@ async def add_item(test_string: str):
|
|||||||
rows = conn.execute(select(test))
|
rows = conn.execute(select(test))
|
||||||
for row in rows:
|
for row in rows:
|
||||||
print(row)
|
print(row)
|
||||||
|
msg = {"message": "Item added"}
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=404, detail="Item not found")
|
raise HTTPException(status_code=404, detail="Item not found")
|
||||||
return None
|
msg = {"message": "err"}
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
with engine.connect() as conn:
|
with engine.connect() as conn:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user