-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTO.py
More file actions
88 lines (71 loc) · 2.9 KB
/
DTO.py
File metadata and controls
88 lines (71 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Description: The DTO class connects to and communicates with the MongoDB database.
# Author: Chloe Lee-Hone
# Date: 14-07-2023
import os
from flask import current_app, g
from flask_pymongo import PyMongo
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from bson.objectid import ObjectId
from dotenv import load_dotenv
import certifi
class DTO:
load_dotenv()
URI = os.getenv('PYTHON_MONGODB_URI')
DATABASE = os.getenv('PYTHON_MONGODB_DB_NAME')
REVIEW_COLLECTION = os.getenv('PYTHON_MONGODB_REVIEW_COLLECTION')
DESCRIPTION_COLLECTION = os.getenv('PYTHON_MONGODB_DESCRIPTION_COLLECTION')
# Retrieves the database
@staticmethod
def get_database():
# Set the Stable API version when creating a new client
client = MongoClient(DTO.URI, tlsCAFile=certifi.where(), server_api=ServerApi('1'))
# Creates the item database
return client[DTO.DATABASE]
# Inserts an item in to the description collection
@staticmethod
def insert_item(data):
# insert data in try block
database = DTO.get_database()
collection = database[DTO.DESCRIPTION_COLLECTION]
collection.insert_one(data)
# Inserts a review to a specific item
@staticmethod
def insert_review(data):
database = DTO.get_database()
collection = database[DTO.REVIEW_COLLECTION]
collection.insert_one(data)
@staticmethod
def get_all_item_data(self):
database = DTO.get_database()
collection = database[DTO.DESCRIPTION_COLLECTION]
return collection.find({"description": {'$exists': True}})
@staticmethod
def get_item_data(self, item_id):
database = DTO.get_database()
collection = database[DTO.DESCRIPTION_COLLECTION]
return collection.find_one({"_id": ObjectId(item_id)})
@staticmethod
def get_item_reviews(self, item_id):
database = DTO.get_database()
collection = database[DTO.REVIEW_COLLECTION]
# return collection.find_one({"item_id": {"$eq": ObjectId(item_id)}})
return collection.find({"item_id": {"$eq": ObjectId(item_id)}})
@staticmethod
def insert_item_review(self, item_id):
database = DTO.get_database()
collection_name = database[DTO.DESCRIPTION_COLLECTION]
collection_name.insert_one(item_id)
# Pings deployment to determine if a connection to the database can be established
@staticmethod
def can_connect():
# Set the Stable API version when creating a new client
client = MongoClient(DTO.URI, tlsCAFile=certifi.where(), server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged the deployment. Successfully connected to MongoDB!")
return True
except Exception as e:
print(e)
return False