Commit ce9b003c by mohammed.shibili

all done

parent ec2d6c28
import uvicorn
from script.core.handlers.reading_csv import csv_read
from script.util.mongo_util import mongo_connection, insert_many
if __name__ == "__main__":
uvicorn.run("script.service.main:app", port=8000, reload=True)
# getting collection name by establishing connection
collection_name = mongo_connection()
dict_from_file = csv_read()
insert_many(collection_name, dict_from_file)
from mongoengine import Document, IntField, StringField
from pydantic import BaseModel
class table_1(Document):
_id = StringField()
id = IntField()
first_name = StringField(max_length=20)
last_name = StringField(max_length=20)
gender = StringField(max_length=10)
\ No newline at end of file
gender = StringField(max_length=10)
# creating base models
class insert_post(BaseModel):
_id: str
id: int
first_name: str
last_name: str
gender: str
......@@ -3,8 +3,8 @@ import pandas as pd
from script.config.appconfig import csv_path
# reading csv
def csv_read():
dataset = pd.read_csv(csv_path)
dict_dataset = dataset.to_dict(orient='records')
return dict_dataset
from fastapi import FastAPI
import json
from script.config.appconfig import db_name
from script.core.engine.models import table_1
from script.core.engine.models import table_1, insert_post
from mongoengine import connect
import json
from script.core.handlers.reading_csv import csv_read
from script.util.mongo_util import mongo_connection
connect(db=db_name, host="localhost", port=27017)
app = FastAPI()
......@@ -17,44 +18,37 @@ async def get_element():
return datas_list
#
# insert many -->post
@app.post("/insertall", tags=['FIRST'])
async def post_all_element():
collection_name.insert_many(dict_from_file)
return "data inserted"
#
# insert --> post
@app.post("/new", tags=['FIRST'])
async def post_element(body: dict):
@app.post("/insert", tags=['FIRST'])
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)
collection_name.insert_one(dict(new_data))
return {"inserted data": dict(new_data)}
data = table_1.insert_one(body).to_json()
new_data = json.loads(data)
return new_data
#
#
# # update --> put
# @app.put("/new", tags=['FIRST'])
# async def put_element(id: int, body: dict):
# for values in value:
# if int(values['id']) == body['id']:
# values['name'] = body['name']
# return {
# "data": f'name of {values["name"]} is updated with {body["name"]}'
# }
# return {
# "data": f'id {id} not found'
# }
#
#
# # delete
# @app.delete("/new/{id}", tags=['FIRST'])
# async def delete_element(id: int):
# for values in value:
# if int((values['id'])) == id:
# value.remove(values)
# return {"data": f'details of {id} deleted'}
#
#
# value = [
# {"id": 5, "name": "ajil", "age": 23},
# {"id": 6, "name": "arun", "age": 25}
#
# ]
#
# new_val = {"id": 7, "name": "arjun", "age": 26}
# update --> put
@app.put("/update", tags=['FIRST'])
async def put_element(user_id: int, body: dict):
collection_name.update_one({"id": user_id}, {"$set": {"first_name": body["first_name"]}})
return {
"data": f'name updated'
}
# delete
@app.delete("/delete", tags=['FIRST'])
async def delete_element(user_id: int):
collection_name.delete_one({"id": user_id})
return {"data": f'details of {user_id} deleted'}
collection_name = mongo_connection()
dict_from_file = csv_read()
from pymongo import MongoClient
from script.config.appconfig import server_name, db_name, collection_name
from script.core.handlers.reading_csv import csv_read
# mongo connection
def mongo_connection():
try:
client = MongoClient(server_name)
......@@ -13,12 +12,3 @@ def mongo_connection():
return collection
except Exception as e:
print("db connection failed", e)
def insert_many(coll_name, dict_from_file):
try:
coll_name.insert_many(dict_from_file)
# coll_name.update_many({}, {"$unset": {"_id": ""}})
print("data inserted")
except Exception as e:
print("data insertion error", e)
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