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
mohammed.shibili
fastAPI-task
Commits
ce9b003c
Commit
ce9b003c
authored
Jan 25, 2023
by
mohammed.shibili
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
all done
parent
ec2d6c28
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
55 additions
and
61 deletions
+55
-61
.idea/sonarlint/issuestore/1/e/1e9075f5bf079c01ef2c910709e91a497d262080
+0
-0
.idea/sonarlint/issuestore/b/c/bc9e48eed1265992cbf93b723b83dff1c740b1f7
+0
-0
.idea/sonarlint/issuestore/index.pb
+6
-0
app.py
+2
-7
script/core/engine/models.py
+10
-1
script/core/handlers/reading_csv.py
+1
-1
script/service/main.py
+35
-41
script/util/mongo_util.py
+1
-11
No files found.
.idea/sonarlint/issuestore/1/e/1e9075f5bf079c01ef2c910709e91a497d262080
0 → 100644
View file @
ce9b003c
.idea/sonarlint/issuestore/b/c/bc9e48eed1265992cbf93b723b83dff1c740b1f7
0 → 100644
View file @
ce9b003c
.idea/sonarlint/issuestore/index.pb
View file @
ce9b003c
No preview for this file type
app.py
View file @
ce9b003c
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
)
script/core/engine/models.py
View file @
ce9b003c
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
)
# creating base models
class
insert_post
(
BaseModel
):
_id
:
str
id
:
int
first_name
:
str
last_name
:
str
gender
:
str
script/core/handlers/reading_csv.py
View file @
ce9b003c
...
...
@@ -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
script/service/main.py
View file @
ce9b003c
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
()
script/util/mongo_util.py
View file @
ce9b003c
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
)
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