Commit 2322bc6c by mohammed.shibili

exception

parent ce9b003c
...@@ -5,6 +5,9 @@ from script.config.appconfig import csv_path ...@@ -5,6 +5,9 @@ from script.config.appconfig import csv_path
# reading csv # reading csv
def csv_read(): def csv_read():
dataset = pd.read_csv(csv_path) try:
dict_dataset = dataset.to_dict(orient='records') dataset = pd.read_csv(csv_path)
return dict_dataset dict_dataset = dataset.to_dict(orient='records')
return dict_dataset
except Exception as e:
print("reading error", e)
...@@ -13,41 +13,56 @@ app = FastAPI() ...@@ -13,41 +13,56 @@ app = FastAPI()
# read --> get # read --> get
@app.get("/get_all") @app.get("/get_all")
async def get_element(): async def get_element():
datas = table_1.objects.to_json() try:
datas_list = json.loads(datas) datas = table_1.objects.to_json()
return datas_list datas_list = json.loads(datas)
return datas_list
except Exception as e:
print("error in get", e)
# insert many -->post # insert many -->post
@app.post("/insertall", tags=['FIRST']) @app.post("/insertall", tags=['FIRST'])
async def post_all_element(): async def post_all_element():
collection_name.insert_many(dict_from_file) try:
return "data inserted" collection_name.insert_many(dict_from_file)
return "data inserted"
except Exception as e:
print("error in post all", e)
# #
# insert --> post # insert --> post
@app.post("/insert", tags=['FIRST']) @app.post("/insert", tags=['FIRST'])
async def post_element(body: insert_post): async def post_element(body: insert_post):
new_data = insert_post(id=body.id, first_name=body.first_name, last_name=body.last_name, gender=body.gender) try:
collection_name.insert_one(dict(new_data)) new_data = insert_post(id=body.id, first_name=body.first_name, last_name=body.last_name, gender=body.gender)
return {"inserted data": dict(new_data)} collection_name.insert_one(dict(new_data))
return {"inserted data": dict(new_data)}
except Exception as e:
print("error in insert", e)
# update --> put # update --> put
@app.put("/update", tags=['FIRST']) @app.put("/update", tags=['FIRST'])
async def put_element(user_id: int, body: dict): async def put_element(user_id: int, body: dict):
collection_name.update_one({"id": user_id}, {"$set": {"first_name": body["first_name"]}}) try:
return { collection_name.update_one({"id": user_id}, {"$set": {"first_name": body["first_name"]}})
"data": f'name updated' return {
} "data": f'name updated'
}
except Exception as e:
print("error while update", e)
# delete # delete
@app.delete("/delete", tags=['FIRST']) @app.delete("/delete", tags=['FIRST'])
async def delete_element(user_id: int): async def delete_element(user_id: int):
collection_name.delete_one({"id": user_id}) try:
return {"data": f'details of {user_id} deleted'} collection_name.delete_one({"id": user_id})
return {"data": f'details of {user_id} deleted'}
except Exception as e:
print("delete error", e)
collection_name = mongo_connection() collection_name = mongo_connection()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment