update class

This commit is contained in:
2024-03-11 22:42:05 -04:00
parent 4c525f55c1
commit 1b1ded5f36

View File

@@ -44,48 +44,76 @@ class AnimalShelter(object):
self.collection = self.database['%s' % (COL)] self.collection = self.database['%s' % (COL)]
# Create # Create
def create(self, *args, **kwargs): def create(self, **kwargs):
"""
Creates collection objects using a defined dictionary
:param data:
Dictionary with the data that will be created in the database
"""
data = kwargs.get('data', None) data = kwargs.get('data', None)
if data is not None: if data is not None:
try: try:
self.database.animals.insert_one(data) # data should be dictionary self.database.animals.insert_one(data)
return True return True
except Exception as e: except Exception as e:
print(f'Exception: {e}') print(f'Exception: {e}') # Exception handling
return False return False # Failed operation, so return false
else: else:
print("Nothing to save, because data parameter is empty") print("Nothing to save, because data parameter is empty") # Case of empty 'data' parameter
return False return False # Failed operation, so return false
# Read # Read
def read(self, *args, **kwargs): def read(self, **kwargs):
"""
Accepts a query and returns a list of dictionaries with the requested data from the collection
:param query:
The filter that will be used to query the database
"""
query = kwargs.get('query', None) query = kwargs.get('query', None)
dataList = [] dataList = []
try: try:
data = self.collection.find(query) dataList = [doc for doc in self.collection.find(query)] # MongoDB cursor, we need to convert this to a list of dicts
for object in data: return dataList # Return the list
dataList.append(object)
return dataList
except Exception as e: except Exception as e:
print(f'Exception: {e}') print(f'Exception: {e}') # Exception handling
return False return False # Failed operation, so return false
# Update # Update
def update(self, *args, **kwargs): def update(self, **kwargs):
"""
Updates objects in the collection based on the query, update type, and incoming dict of data
:param query:
The filter that will be used to query the database for the objects that will be updated
:param update_type:
The type of update to be performed, accepts any MongoDB Field Update Operator
(https://www.mongodb.com/docs/manual/reference/operator/update-field/)
:param data:
The incoming data (dictionary) to update with
Not required depending on the update_type specified
"""
query = kwargs.get('query', None) query = kwargs.get('query', None)
update_type = kwargs.get('update_type', None) update_type = kwargs.get('update_type', None)
data = kwargs.get('data', None) data = kwargs.get('data', None)
try: try:
self.collection.update_many(query, {f'${update_type}': data}) self.collection.update_many(query, {f'${update_type}': data}) # Query DB + update with data
return True return True # Operation was successful, so return true
except Exception as e: except Exception as e:
print(f'Exception: {e}') print(f'Exception: {e}') # Exception handling
return False return False # Failed operation, so return false
# Delete # Delete
def delete(self, *args, **kwargs): def delete(self, **kwargs):
"""
Deletes objects in the collection based on the query
:param query:
The filter that will be used to query the database for objects that will be deleted
"""
query = kwargs.get('query', None) query = kwargs.get('query', None)
try: try:
self.collection.delete_many(query) self.collection.delete_many(query)
return True return True # Operation was successful, so return true
except Exception as e: except Exception as e:
print(f'Exception: {e}') print(f'Exception: {e}') # Exception handling
return False return False # Failed operation, so return false