added Service Class for MongoDB
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
from app_be.views.mongo_db import MongoManager
|
||||
from django.http import JsonResponse
|
||||
|
||||
|
||||
class MongoDBService:
|
||||
|
||||
def getAll():
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
try:
|
||||
for resp in col.find():
|
||||
print(resp)
|
||||
except:
|
||||
print("Could not find Metadata")
|
||||
return resp
|
||||
|
||||
def getSingle(identifier):
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
metadata = None
|
||||
try:
|
||||
resp = col.find({"identifier": identifier})
|
||||
metadata = resp[0]
|
||||
except:
|
||||
print("Could not find Metadata")
|
||||
if metadata is None:
|
||||
return JsonResponse({'Result': 'Error - could not find metadata.'}, status=404, safe=False)
|
||||
else:
|
||||
if '_id' in metadata:
|
||||
del metadata['_id']
|
||||
return metadata
|
||||
|
||||
def createSingle(metadata, identifier):
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
try:
|
||||
metadata['identifier'] = identifier
|
||||
metadata['location'] = [metadata['longitude'], metadata['latitude']]
|
||||
col.insert_one(metadata)
|
||||
|
||||
except:
|
||||
print("Could not insert Metadata")
|
||||
|
||||
def deleteSingle(identifier):
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
try:
|
||||
resp = col.delete_one({"identifier": identifier})
|
||||
except:
|
||||
print("Could not delete Metadata")
|
||||
return resp
|
||||
Reference in New Issue
Block a user