Commit 367a5719 by ajil.k

updated

parent 247ccd0e
MONGO_URI = mongodb://localhost:27017/
DATA_PATH = temp/data.csv
\ No newline at end of file
[MongoDB]
mongo_uri = $MONGO_URI
\ No newline at end of file
mongo_uri = mongodb://localhost:27017/
[DataPath]
csv_path = temp/data.csv
\ No newline at end of file
import sys
from configparser import ConfigParser
try:
config = ConfigParser()
config.read(f"conf/application.conf")
except Exception as e:
print(f"Error while loading the config: {e}")
print("Failed to Load Configuration. Exiting!!!")
sys.stdout.flush()
sys.exit()
config = ConfigParser()
config.read(f"conf/application.conf")
class Mongo:
uri = config.get("MONGO_DB", "uri")
uri = config.get("MongoDB", "mongo_uri")
csv_path = config.get("DataPath", "csv_path")
# create a connection
from pymongo import MongoClient
from mongoengine import connect
from scripts.constants.app_configuration import uri
def db_connect():
try:
# Connect to MongoDB
connection = MongoClient("mongodb://localhost:27017/")
connection = MongoClient(uri)
if connection:
print(" Database Connected Successfully !")
print("Database Connected Successfully !")
# Create a database
db_name = 'db_person'
db = connection[db_name]
......
......@@ -22,6 +22,24 @@ class Employee(BaseModel):
mobile: int
class PersonalDetails(BaseModel):
_id: Optional[str] = None
id: int
first_name: str
last_name: str
email: str
phone_no: int
class UpdatePersonalDetails(BaseModel):
_id: Optional[str] = None
id: Optional[int] = None
first_name: Optional[str] = None
last_name: Optional[str] = None
email: Optional[str] = None
phone_no: Optional[int] = None
class UpdateEmployee(BaseModel):
name: Optional[str] = None
gender: Optional[str] = None
......@@ -37,21 +55,21 @@ connect(
def start_operations(app_api):
@app_api.post("/add-employee/{employee_id}")
@app_api.post("/add-employee/{employee_id}", tags=["add employee"])
def add_employee(employee_id: int, employee: Employee):
if employee_id in employees:
return {"Error": "Employee already Exists"}
employees[employee_id] = employee
return employees
@app_api.get("/view-employee/{employee_id}")
@app_api.get("/view-employee/{employee_id}", tags=["view employee"])
def view_employee(employee_id: int):
if employee_id in employees:
return employees[employee_id]
else:
return {"Error": "Employee doesn't Exist"}
@app_api.put("/update-employee/{employee_id}")
@app_api.put("/update-employee/{employee_id}", tags=["update employee"])
def update_employee(employee_id: int, employee: UpdateEmployee):
if employee_id not in employees:
return {"Error": "Employee not Exists!"}
......@@ -66,7 +84,7 @@ def start_operations(app_api):
employees[employee_id].mobile = employee.mobile
return employees[employee_id]
@app_api.delete("/delete-employee/{employee_id}")
@app_api.delete("/delete-employee/{employee_id}", tags=["delete employee"])
def delete_employee(employee_id: int):
if employee_id not in employees:
return {"Error": "Employee not Exists!"}
......@@ -75,8 +93,45 @@ def start_operations(app_api):
def view_documents(app_api):
@app_api.get("/get_details", tags=["read data"])
@app_api.get("/get_details", tags=["read documents"])
def get_all_details():
documents = personal_details.objects().to_json()
print(documents)
return {"data": documents}
def insert_document(app_api, collection):
@app_api.post("/create-new-document", tags=["create document"])
async def create_new_document(details: PersonalDetails):
p_details = PersonalDetails(id=details.id,
first_name=details.first_name,
last_name=details.last_name,
email=details.email,
phone_no=details.phone_no)
result = collection.insert_one(dict(p_details))
return {"inserted document id": str(result.inserted_id)}
def update_documents(app_api, collection):
@app_api.put("/update-document{document_id}", tags=["update document"])
async def update_document(document_id: int, update_details: UpdatePersonalDetails):
condition_data = {"id": document_id}
set_data = {"$set": {}}
if update_details.first_name is not None:
set_data["$set"]["first_name"] = update_details.first_name
if update_details.last_name is not None:
set_data["$set"]["last_name"] = update_details.last_name
if update_details.email is not None:
set_data["$set"]["email"] = update_details.email
if update_details.phone_no is not None:
set_data["$set"]["phone_no"] = update_details.phone_no
updated = collection.update_one(condition_data, set_data)
return {"Message": "Document updated"}
def delete_document(app_api, collection):
@app_api.delete("/delete-document/{document_id}")
async def delete_documents(document_id: int):
condition_data = {"id": document_id}
deleted_one = collection.delete_one(condition_data)
return {"Message": "Document deleted Successfully!"}
# importing libraries
from fastapi import FastAPI
# from scripts.constants.app_configuration import csv_path
from scripts.constants.db_connection import db_connect
from scripts.core.handlers.insert_data_into_db import insert_into_db
from scripts.core.handlers.operations import start_operations, view_documents
from scripts.core.handlers.operations import start_operations, view_documents, insert_document, update_documents, \
delete_document
from scripts.core.handlers.read_file import extract_data
from scripts.utils.mongo_util import create_collection
......@@ -26,6 +27,14 @@ insert_status = insert_into_db(collection, data)
if insert_status:
print("Data inserted from csv to database successfully!")
# Display
# Display all documents
view_documents(app)
# Insert one document to db
insert_document(app, collection)
# Update one Document
update_documents(app, collection)
# Delete one Document
delete_document(app, collection)
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