Commit 206e2ee1 by arjun.b

updated after review

(file upload and stored to database)
parent 9fe33060
BASE_PATH=
URI="mongodb://localhost:27017" URI="mongodb://localhost:27017"
import uvicorn import uvicorn
from fastapi import FastAPI
from scripts.constants.db_connection import db_connect from scripts.constants.application_config import server_path, port_no
from scripts.utils.mongo_utils import create_collection
if __name__ == "__main__": if __name__ == "__main__":
try: try:
uvicorn.run("scripts.services.main:app", port=8000, reload=True) uvicorn.run(server_path, port=int(port_no), reload=True)
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
[path] [path]
base_path= scripts/temp/emp.csv base_path= scripts/temp/emp.csv
[mongo] [mongo]
uri=$URI uri=mongodb://localhost:27017
\ No newline at end of file port=27017
[server]
server_path=scripts.services.main:app
port_no=8000
...@@ -5,15 +5,13 @@ from scripts.constants.application_config import mongo_uri ...@@ -5,15 +5,13 @@ from scripts.constants.application_config import mongo_uri
def db_connect(): def db_connect():
try: try:
conn = MongoClient("mongodb://localhost:27017") conn = MongoClient(mongo_uri)
database = conn.employees database = conn.employees
print(conn.list_database_names()) # print(conn.list_database_names())
dblist = conn.list_database_names() dblist = conn.list_database_names()
if database in dblist: if database in dblist:
print("database exist") print("database exist")
collection = database.employee
print("mongo connect successfully") print("mongo connect successfully")
return database return database
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
...@@ -22,5 +22,7 @@ class employee_insert(BaseModel): ...@@ -22,5 +22,7 @@ class employee_insert(BaseModel):
class employee_update(BaseModel): class employee_update(BaseModel):
id: int id: int
class employee_delete(BaseModel): class employee_delete(BaseModel):
id:int id: int
def read_all(collection):
response=collection.find({})
\ No newline at end of file
def insert_data(collection, data):
try:
inserted = collection.insert_many(data.to_dict('records'))
if inserted:
return True
else:
return False
except Exception as e:
print(str(e))
import pandas as pd
from scripts.constants.application_config import file_path
def read_file():
try:
dataset = pd.read_csv(file_path)
return dataset
except Exception as e:
print(str(e))
import json import csv
from fastapi import FastAPI, UploadFile
from fastapi import FastAPI
from mongoengine import connect from mongoengine import connect
from scripts.constants.application_config import port_connect
from scripts.constants.db_connection import db_connect from scripts.constants.db_connection import db_connect
from scripts.core.engine.model import Employee, employee_insert, employee_update, employee_delete from scripts.core.database.model import employee_insert
from scripts.core.handlers.find_all import read_all
from scripts.core.handlers.insert_data import insert_data
from scripts.core.handlers.read_file import read_file
from scripts.utils.mongo_utils import create_collection from scripts.utils.mongo_utils import create_collection
app = FastAPI() app = FastAPI()
connect(db="employees", host="localhost", port=27017) connect(db="employees", host="localhost", port=int(port_connect))
@app.get("/") @app.get("/")
...@@ -19,13 +15,16 @@ async def root(): ...@@ -19,13 +15,16 @@ async def root():
return {"data": "FastAPI CRUD Operations"} return {"data": "FastAPI CRUD Operations"}
# csv file insert into database # upload and insert into database
@app.get("/get_details", tags=["read data"]) @app.post("/upload", tags=["upload file"])
def get_all_details(): async def upload_file(file: UploadFile):
employees = Employee.objects().to_json() file_data = file.file.read()
emp_dict = json.loads(employees) csv_reader = csv.reader(file_data.decode().splitlines(), delimiter=',')
# print(employees) header = next(csv_reader)
return {"data": emp_dict} for row in csv_reader:
dataset = dict(zip(header, row))
collection.insert_one(dataset)
return {"data": "CSV data uploaded successfully"}
# insert data using post method # insert data using post method
...@@ -40,28 +39,25 @@ def send_data(emp: employee_insert): ...@@ -40,28 +39,25 @@ def send_data(emp: employee_insert):
return {"message": "new data has been inserted"} return {"message": "new data has been inserted"}
# update data using # update data
@app.put("/update_data", tags=["update database"]) @app.put("/update_data/{doc_id}", tags=["update database"])
def update_data(emp: employee_update): def update_data(doc_id: int):
update_emp = employee_update(id=emp.id) collection.update_many({"id": doc_id}, {"$set": {"first_name": "ALAN"}})
collection.update_one({"id": update_emp.id}, {"$set": {"first_name": "ARJUN"}})
return {"data": "data updated"} return {"data": "data updated"}
# delete data
@app.delete("/delete_data", tags=["delete data"]) @app.delete("/delete_data", tags=["delete data"])
def delete_data(emp: employee_delete): def delete_data(doc_id: int):
delete_emp = employee_delete(id=emp.id) # delete_emp = employee_delete(id=emp.id)
collection.delete_one({"id": delete_emp.id}) collection.delete_many({"id": doc_id})
return {"data": "data deleted"} return {"data": "data deleted"}
# read csv file
data = read_file()
# create database # create database
db = db_connect() db = db_connect()
print("database created")
# create collection # create collection
collection = create_collection(db) collection = create_collection(db)
# insert the csv file into database print("collection created")
insert = insert_data(collection, data)
print("data inserted from csv")
read = read_all(collection)
def create_collection(db): def create_collection(db):
collection_name = "employee" try:
collection = db[collection_name] collection_name = "employee"
return collection collection = db[collection_name]
return collection
except Exception as e:
print(str(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