Commit 206e2ee1 by arjun.b

updated after review

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