Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fastAPI_task
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ajil.k
fastAPI_task
Commits
367a5719
Commit
367a5719
authored
Jan 25, 2023
by
ajil.k
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated
parent
247ccd0e
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
90 additions
and
25 deletions
+90
-25
.env
+3
-0
conf/application.conf
+5
-2
scripts/constants/app_configuration.py
+5
-11
scripts/constants/db_connection.py
+4
-3
scripts/core/handlers/operations.py
+60
-5
scripts/services/main.py
+13
-4
No files found.
.env
View file @
367a5719
MONGO_URI = mongodb://localhost:27017/
MONGO_URI = mongodb://localhost:27017/
DATA_PATH = temp/data.csv
\ No newline at end of file
conf/application.conf
View file @
367a5719
[
MongoDB
]
[
MongoDB
]
mongo_uri
= $
MONGO_URI
mongo_uri
=
mongodb
://
localhost
:
27017
/
\ No newline at end of file
[
DataPath
]
csv_path
=
temp
/
data
.
csv
\ No newline at end of file
scripts/constants/app_configuration.py
View file @
367a5719
import
sys
from
configparser
import
ConfigParser
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
(
"MongoDB"
,
"mongo_uri"
)
uri
=
config
.
get
(
"MONGO_DB"
,
"uri"
)
csv_path
=
config
.
get
(
"DataPath"
,
"csv_path"
)
scripts/constants/db_connection.py
View file @
367a5719
# create a connection
# create a connection
from
pymongo
import
MongoClient
from
pymongo
import
MongoClient
from
mongoengine
import
connect
from
scripts.constants.app_configuration
import
uri
def
db_connect
():
def
db_connect
():
try
:
try
:
# Connect to MongoDB
# Connect to MongoDB
connection
=
MongoClient
(
"mongodb://localhost:27017/"
)
connection
=
MongoClient
(
uri
)
if
connection
:
if
connection
:
print
(
"
Database Connected Successfully !"
)
print
(
"Database Connected Successfully !"
)
# Create a database
# Create a database
db_name
=
'db_person'
db_name
=
'db_person'
db
=
connection
[
db_name
]
db
=
connection
[
db_name
]
...
...
scripts/core/handlers/operations.py
View file @
367a5719
...
@@ -22,6 +22,24 @@ class Employee(BaseModel):
...
@@ -22,6 +22,24 @@ class Employee(BaseModel):
mobile
:
int
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
):
class
UpdateEmployee
(
BaseModel
):
name
:
Optional
[
str
]
=
None
name
:
Optional
[
str
]
=
None
gender
:
Optional
[
str
]
=
None
gender
:
Optional
[
str
]
=
None
...
@@ -37,21 +55,21 @@ connect(
...
@@ -37,21 +55,21 @@ connect(
def
start_operations
(
app_api
):
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
):
def
add_employee
(
employee_id
:
int
,
employee
:
Employee
):
if
employee_id
in
employees
:
if
employee_id
in
employees
:
return
{
"Error"
:
"Employee already Exists"
}
return
{
"Error"
:
"Employee already Exists"
}
employees
[
employee_id
]
=
employee
employees
[
employee_id
]
=
employee
return
employees
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
):
def
view_employee
(
employee_id
:
int
):
if
employee_id
in
employees
:
if
employee_id
in
employees
:
return
employees
[
employee_id
]
return
employees
[
employee_id
]
else
:
else
:
return
{
"Error"
:
"Employee doesn't Exist"
}
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
):
def
update_employee
(
employee_id
:
int
,
employee
:
UpdateEmployee
):
if
employee_id
not
in
employees
:
if
employee_id
not
in
employees
:
return
{
"Error"
:
"Employee not Exists!"
}
return
{
"Error"
:
"Employee not Exists!"
}
...
@@ -66,7 +84,7 @@ def start_operations(app_api):
...
@@ -66,7 +84,7 @@ def start_operations(app_api):
employees
[
employee_id
]
.
mobile
=
employee
.
mobile
employees
[
employee_id
]
.
mobile
=
employee
.
mobile
return
employees
[
employee_id
]
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
):
def
delete_employee
(
employee_id
:
int
):
if
employee_id
not
in
employees
:
if
employee_id
not
in
employees
:
return
{
"Error"
:
"Employee not Exists!"
}
return
{
"Error"
:
"Employee not Exists!"
}
...
@@ -75,8 +93,45 @@ def start_operations(app_api):
...
@@ -75,8 +93,45 @@ def start_operations(app_api):
def
view_documents
(
app_api
):
def
view_documents
(
app_api
):
@app_api.get
(
"/get_details"
,
tags
=
[
"read d
ata
"
])
@app_api.get
(
"/get_details"
,
tags
=
[
"read d
ocuments
"
])
def
get_all_details
():
def
get_all_details
():
documents
=
personal_details
.
objects
()
.
to_json
()
documents
=
personal_details
.
objects
()
.
to_json
()
print
(
documents
)
print
(
documents
)
return
{
"data"
:
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!"
}
scripts/services/main.py
View file @
367a5719
# importing libraries
# importing libraries
from
fastapi
import
FastAPI
from
fastapi
import
FastAPI
# from scripts.constants.app_configuration import csv_path
from
scripts.constants.db_connection
import
db_connect
from
scripts.constants.db_connection
import
db_connect
from
scripts.core.handlers.insert_data_into_db
import
insert_into_db
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.core.handlers.read_file
import
extract_data
from
scripts.utils.mongo_util
import
create_collection
from
scripts.utils.mongo_util
import
create_collection
...
@@ -26,6 +27,14 @@ insert_status = insert_into_db(collection, data)
...
@@ -26,6 +27,14 @@ insert_status = insert_into_db(collection, data)
if
insert_status
:
if
insert_status
:
print
(
"Data inserted from csv to database successfully!"
)
print
(
"Data inserted from csv to database successfully!"
)
# Display
# Display all documents
view_documents
(
app
)
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment