Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
crud-operations
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vipul.v
crud-operations
Commits
347a0256
Commit
347a0256
authored
Mar 14, 2023
by
vipul.v
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
crud operation in mongo
parent
f983ca62
Changes
12
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
70 additions
and
52 deletions
+70
-52
.env
.env
+5
-0
conf/application.conf
conf/application.conf
+2
-2
main.py
main.py
+2
-0
mongo_file.py
mongo_file.py
+9
-0
script/config/app_confing.py
script/config/app_confing.py
+4
-1
script/core/db/mongo/interns2023/__init__.py
script/core/db/mongo/interns2023/__init__.py
+4
-1
script/core/db/mongo/interns2023/inventory.py
script/core/db/mongo/interns2023/inventory.py
+3
-7
script/core/handlers/inventory.py
script/core/handlers/inventory.py
+14
-20
script/core/schema/inventory.py
script/core/schema/inventory.py
+5
-4
script/service/inventory.py
script/service/inventory.py
+5
-5
script/utils/mongo_connector.py
script/utils/mongo_connector.py
+3
-1
script/utils/mongo_utils.py
script/utils/mongo_utils.py
+14
-11
No files found.
.env
0 → 100644
View file @
347a0256
#mongodb
MONGO_URI=mongodb://intern_23:intern%40123@192.168.0.220:2717/?authSource=interns_b2_23&authMechanism=SCRAM-SHA-256
conf/application.conf
View file @
347a0256
[
SERVICE
]
[
SERVICE
]
port
=
7998
port
=
8091
host
=
0
.
0
.
0
.
0
host
=
0
.
0
.
0
.
0
[
MONGO
-
DB
]
[
MONGO
-
DB
]
mongo_uri
=
mongodb
://
192168
.
0
.
220
:
2717
/
mongo_uri
=$
MONGO_URI
\ No newline at end of file
\ No newline at end of file
main.py
View file @
347a0256
from
dotenv
import
load_dotenv
load_dotenv
()
import
uvicorn
import
uvicorn
from
fastapi
import
FastAPI
from
fastapi
import
FastAPI
...
...
mongo_file.py
0 → 100644
View file @
347a0256
import
pymongo
client
=
pymongo
.
MongoClient
(
"mongodb://intern_23:intern
%40123
@192.168.0.220:2717/?authSource=interns_b2_23&authMechanism=SCRAM-SHA-256"
)
db
=
client
[
"interns_b2_23"
]
col
=
db
[
"Vipul-inventory"
]
r
=
col
.
find
()
print
(
list
(
r
))
script/config/app_confing.py
View file @
347a0256
import
os
from
configparser
import
SafeConfigParser
from
configparser
import
SafeConfigParser
config
=
SafeConfigParser
()
config
=
SafeConfigParser
()
config
.
read
(
'conf/application.conf'
)
config
.
read
(
'conf/application.conf'
)
...
@@ -10,4 +12,5 @@ class Service:
...
@@ -10,4 +12,5 @@ class Service:
class
Mongo
:
class
Mongo
:
mon
=
config
.
get
(
'MONGO-DB'
,
'mongo_uri'
)
mongo_uri
:
str
=
os
.
environ
.
get
(
"MONGO_URI"
)
print
(
mongo_uri
)
script/core/db/mongo/interns2023/__init__.py
View file @
347a0256
database_name
=
"interns2023"
from
script.config.app_confing
import
Mongo
from
script.utils.mongo_utils
import
MongoConnect
mongo_client
=
MongoConnect
(
uri
=
Mongo
.
mongo_uri
)()
script/core/db/mongo/interns2023/inventory.py
View file @
347a0256
from
script.core.db.mongo.interns2023
import
database_name
from
script.utils.mongo_utils
import
MongoCollectionBaseClass
from
script.utils.mongo_utils
import
MongoCollectionBaseClass
class
InventoryCollection
(
MongoCollectionBaseClass
):
class
InventoryCollection
(
MongoCollectionBaseClass
):
def
__init__
(
self
,
mongo_client
):
def
__init__
(
self
,
mongo_client
):
super
()
.
__init__
(
mongo_client
=
mongo_client
,
database
=
"interns_b2_23"
,
collection
=
"inventory"
)
super
()
.
__init__
(
mongo_client
=
mongo_client
,
database
=
"interns_b2_23"
,
collection
=
"Vipul-inventory"
)
self
.
mongo_client
=
mongo_client
self
.
database_name
=
database_name
self
.
collection_name
=
"inventory"
def
insert_item
(
self
,
data
):
def
insert_item
(
self
,
data
):
self
.
insert_one
(
data
)
self
.
insert_one
(
data
)
...
@@ -18,5 +14,5 @@ class InventoryCollection(MongoCollectionBaseClass):
...
@@ -18,5 +14,5 @@ class InventoryCollection(MongoCollectionBaseClass):
def
delete_item
(
self
,
data
):
def
delete_item
(
self
,
data
):
self
.
delete_one
(
data
)
self
.
delete_one
(
data
)
def
update_item
(
self
,
data
):
def
update_item
(
self
,
query
,
data
):
self
.
update_one
(
data
)
self
.
update_one
(
query
,
data
)
script/core/handlers/inventory.py
View file @
347a0256
# class for CRUD operations
# class for CRUD operations
from
script.core.db.mongo.interns2023
import
mongo_client
from
script.core.db.mongo.interns2023.inventory
import
InventoryCollection
from
script.core.db.mongo.interns2023.inventory
import
InventoryCollection
from
script.core.errors
import
NameDoesNotExist
from
script.core.schema.inventory
import
Inventory
from
script.core.schema.inventory
import
Inventory
class
InventoryData
:
class
InventoryData
:
def
__init__
(
self
):
def
__init__
(
self
):
self
.
inventory_col
=
InventoryCollection
(
mongo_client
=
""
)
self
.
inventory_col
=
InventoryCollection
(
mongo_client
=
mongo_client
)
def
delete_data
(
self
,
order_id
,
int
):
def
delete_data
(
self
,
order_id
):
try
:
try
:
d
=
{
"order_id"
:
order_id
}
self
.
inventory_col
.
delete_one
(
query
=
{
"order_id"
:
order_id
})
if
3
not
in
d
.
values
():
raise
NameDoesNotExist
except
ValueError
:
except
ValueError
:
raise
ValueError
(
"ID"
)
raise
ValueError
(
"ID"
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
,
"Error Detected in Deleion"
)
print
(
e
,
"Error Detected in Dele
t
ion"
)
@
staticmethod
def
update_data
(
self
,
request_data
:
Inventory
):
def
update_data
(
self
,
order_id
:
int
):
try
:
try
:
d
=
{
"order_id"
:
order_id
}
d
=
{
"order_id"
:
request_data
.
order_id
,
if
2
not
in
d
.
values
():
"customer_name"
:
request_data
.
customer_name
,
raise
NameDoesNotExist
"status"
:
request_data
.
status
,
"sales_order"
:
request_data
.
sales_order
}
self
.
inventory_col
.
update_one
(
query
=
{
"order_id"
:
request_data
.
order_id
},
data
=
d
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
,
"Error Detected in Updation"
)
print
(
e
,
"Error Detected in Updation"
)
@
staticmethod
def
find_data
(
self
,
order_id
:
int
):
def
find_data
(
self
,
order_id
:
int
):
try
:
try
:
d
=
{
"order_id"
:
order_id
}
data
=
self
.
inventory_col
.
find_one
(
query
=
{
"order_id"
:
order_id
})
if
2
not
in
d
.
values
():
return
data
raise
NameDoesNotExist
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
,
"Error Detected in Finding"
)
print
(
e
,
"Error Detected in Finding"
)
def
insert_data
(
self
,
request_data
:
Inventory
):
def
insert_data
(
self
,
request_data
:
Inventory
):
try
:
try
:
d
=
{
"order_id"
:
request_data
.
order_id
,
d
=
{
"order_id"
:
request_data
.
order_id
,
"data"
:
request_data
.
date
,
"customer_name"
:
request_data
.
customer_name
,
"customer_name"
:
request_data
.
customer_name
,
"status"
:
request_data
.
status
,
"status"
:
request_data
.
status
,
"sales_order"
:
request_data
.
sales_order
}
"sales_order"
:
request_data
.
sales_order
}
self
.
inventory_col
.
insert_one
(
data
)
self
.
inventory_col
.
insert_one
(
d
)
if
5
not
in
d
.
values
():
raise
NameDoesNotExist
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
,
"Error Detected in inserting"
)
print
(
e
,
"Error Detected in inserting"
)
script/core/schema/inventory.py
View file @
347a0256
import
datetime
from
typing
import
Optional
from
typing
import
Optional
from
pydantic
import
BaseModel
from
pydantic
import
BaseModel
...
@@ -5,8 +6,8 @@ from pydantic import BaseModel
...
@@ -5,8 +6,8 @@ from pydantic import BaseModel
# define a model for Item
# define a model for Item
class
Inventory
(
BaseModel
):
class
Inventory
(
BaseModel
):
order_id
:
Optional
[
int
]
order_id
:
int
date
:
int
customer_name
:
str
customer_name
:
str
status
:
str
status
:
Optional
[
str
]
sales_order
:
int
date
:
Optional
[
datetime
.
date
]
sales_order
:
Optional
[
int
]
script/service/inventory.py
View file @
347a0256
...
@@ -15,7 +15,7 @@ handler = InventoryData()
...
@@ -15,7 +15,7 @@ handler = InventoryData()
async
def
delete_item
(
order_id
:
int
):
async
def
delete_item
(
order_id
:
int
):
"""This API deletes an item"""
"""This API deletes an item"""
try
:
try
:
handler
.
delete_data
(
order_id
,
int
)
handler
.
delete_data
(
order_id
)
return
DefaultResponse
(
message
=
"Successfully deleted"
,
status
=
"success"
)
return
DefaultResponse
(
message
=
"Successfully deleted"
,
status
=
"success"
)
except
ValueError
:
except
ValueError
:
return
DefaultResponse
(
message
=
"Due to Value Error"
)
return
DefaultResponse
(
message
=
"Due to Value Error"
)
...
@@ -25,9 +25,9 @@ async def delete_item(order_id: int):
...
@@ -25,9 +25,9 @@ async def delete_item(order_id: int):
@
router
.
post
(
APIEndpoints
.
update
)
@
router
.
post
(
APIEndpoints
.
update
)
async
def
update_item
(
order_id
:
int
):
async
def
update_item
(
request_data
:
Inventory
):
try
:
try
:
handler
.
update_data
(
order_id
,
int
)
handler
.
update_data
(
request_data
)
return
DefaultResponse
(
message
=
"Successfully Updated"
,
status
=
"success"
)
return
DefaultResponse
(
message
=
"Successfully Updated"
,
status
=
"success"
)
except
ValueError
:
except
ValueError
:
return
DefaultResponse
(
message
=
"Due to value error"
)
return
DefaultResponse
(
message
=
"Due to value error"
)
...
@@ -39,8 +39,8 @@ async def update_item(order_id: int):
...
@@ -39,8 +39,8 @@ async def update_item(order_id: int):
@
router
.
get
(
APIEndpoints
.
find
)
@
router
.
get
(
APIEndpoints
.
find
)
async
def
find_item
(
order_id
:
int
):
async
def
find_item
(
order_id
:
int
):
try
:
try
:
handler
.
find_data
(
order_id
,
int
)
data
=
handler
.
find_data
(
order_id
)
return
DefaultResponse
(
message
=
"Successfully Found"
,
status
=
"success"
)
return
DefaultResponse
(
message
=
"Successfully Found"
,
status
=
"success"
,
data
=
data
)
except
ValueError
:
except
ValueError
:
return
DefaultResponse
(
message
=
"Due to value error"
)
return
DefaultResponse
(
message
=
"Due to value error"
)
except
Exception
as
e
:
except
Exception
as
e
:
...
...
script/utils/mongo_connector.py
View file @
347a0256
from
fastapi
import
APIRouter
from
fastapi
import
APIRouter
from
pymongo
import
MongoClient
from
pymongo
import
MongoClient
from
script.config.app_confing
import
Mongo
router
=
APIRouter
()
router
=
APIRouter
()
# Create a MongoClient instance
# Create a MongoClient instance
client
=
MongoClient
(
"Mongo_uri"
)
client
=
MongoClient
(
Mongo
.
mongo_uri
)
# Connect to a database
# Connect to a database
db
=
client
.
mydatabase
db
=
client
.
mydatabase
script/utils/mongo_utils.py
View file @
347a0256
import
logging
from
typing
import
Dict
,
Optional
from
typing
import
Dict
,
Optional
from
pymongo
import
MongoClient
from
pymongo
import
MongoClient
import
logging
class
MongoConnect
:
class
MongoConnect
:
def
__init__
(
self
,
uri
):
def
__init__
(
self
,
uri
):
...
@@ -14,6 +13,11 @@ class MongoConnect:
...
@@ -14,6 +13,11 @@ class MongoConnect:
logging
.
error
(
f
"Exception in connection {(str(e))}"
)
logging
.
error
(
f
"Exception in connection {(str(e))}"
)
raise
e
raise
e
def
__call__
(
self
,
*
args
,
**
kwargs
):
return
self
.
client
def
__repr__
(
self
):
return
f
"Mongo Client(uri:{self.uri}, server_info={self.client.server_info()})"
class
MongoCollectionBaseClass
:
class
MongoCollectionBaseClass
:
def
__init__
(
self
,
mongo_client
,
database
,
collection
):
def
__init__
(
self
,
mongo_client
,
database
,
collection
):
...
@@ -21,6 +25,9 @@ class MongoCollectionBaseClass:
...
@@ -21,6 +25,9 @@ class MongoCollectionBaseClass:
self
.
database
=
database
self
.
database
=
database
self
.
collection
=
collection
self
.
collection
=
collection
def
__repr__
(
self
):
return
f
"{self.__class__.__name__}(database={self.database}, collection={self.collection})"
def
insert_one
(
self
,
data
:
Dict
):
def
insert_one
(
self
,
data
:
Dict
):
try
:
try
:
...
@@ -34,15 +41,11 @@ class MongoCollectionBaseClass:
...
@@ -34,15 +41,11 @@ class MongoCollectionBaseClass:
logging
.
error
(
f
"Error in inserting the data {str(e)}"
)
logging
.
error
(
f
"Error in inserting the data {str(e)}"
)
raise
e
raise
e
def
find_one
(
self
,
data
:
Dict
,
filter_dict
:
Optional
[
Dict
]
=
None
):
def
find_one
(
self
,
query
,
filter_dict
:
Optional
[
Dict
]
=
None
):
try
:
try
:
database_name
=
self
.
database
database_name
=
self
.
client
[
self
.
database
]
collection_name
=
self
.
collection
collection_name
=
database_name
[
self
.
collection
]
if
filter_dict
is
None
:
response
=
collection_name
.
find_one
(
query
)
filter_dict
=
{
"_id"
:
0
}
db
=
self
.
client
[
database_name
]
collection
=
db
[
collection_name
]
response
=
collection
.
find_one
(
query
,
filter_dict
)
return
response
return
response
except
Exception
as
e
:
except
Exception
as
e
:
logging
.
error
(
f
"Failed to fetch {str(e)}"
)
logging
.
error
(
f
"Failed to fetch {str(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