added Service Class for MongoDB
This commit is contained in:
parent
d7e2bf651b
commit
8b170b61a6
63
middleware/app_be/services/mongodbservice.py
Normal file
63
middleware/app_be/services/mongodbservice.py
Normal file
@ -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
|
||||
@ -3,8 +3,8 @@ import json
|
||||
import base64
|
||||
|
||||
from app_be.services.dropboxservice import DropboxService
|
||||
from app_be.services.mongodbservice import MongoDBService
|
||||
from app_be.services.wrapperservice import WrapperService
|
||||
from app_be.views.mongo_db import MongoManager
|
||||
|
||||
from django.http import JsonResponse
|
||||
from django.http import HttpRequest
|
||||
@ -29,17 +29,9 @@ class ImageEndpoint:
|
||||
logger.debug('Image GET all call: {}'.format(request))
|
||||
print(request)
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
try:
|
||||
for resp in col.find():
|
||||
print(resp)
|
||||
|
||||
except:
|
||||
print("Could not find Metadata")
|
||||
metadata = MongoDBService.getAll()
|
||||
print(metadata)
|
||||
|
||||
return JsonResponse({'Result': 'success1'}, safe=False)
|
||||
|
||||
@ -49,21 +41,9 @@ class ImageEndpoint:
|
||||
def image_api_get_single(request, identifier):
|
||||
logger.debug('Image GET single call: {}'.format(request))
|
||||
|
||||
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']
|
||||
metadata = MongoDBService.getSingle(identifier)
|
||||
print(metadata)
|
||||
|
||||
dropbox_image_bytes = DropboxService.read_file(metadata['filename'])
|
||||
|
||||
@ -88,18 +68,8 @@ class ImageEndpoint:
|
||||
metadata = payload['metadata']
|
||||
filename = payload['metadata']['filename']
|
||||
|
||||
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")
|
||||
MongoDBService.createSingle(metadata,identifier)
|
||||
|
||||
decoded_image = WrapperService.unwrap_file(b64encoded_image)
|
||||
if not DropboxService.create_file(filename, decoded_image):
|
||||
@ -114,14 +84,9 @@ class ImageEndpoint:
|
||||
|
||||
logger.debug('Image DELETE single call: {}'.format(request))
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
resp = MongoDBService.deleteSingle(identifier)
|
||||
print(resp)
|
||||
|
||||
try:
|
||||
col.delete_one({"identifier": identifier})
|
||||
except:
|
||||
print("Could not delete Metadata")
|
||||
|
||||
return JsonResponse({'Result': 'success1'}, safe=False)
|
||||
Loading…
x
Reference in New Issue
Block a user