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)]
# 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)
if data is not None:
try:
self.database.animals.insert_one(data) # data should be dictionary
self.database.animals.insert_one(data)
return True
except Exception as e:
print(f'Exception: {e}')
return False
print(f'Exception: {e}') # Exception handling
return False # Failed operation, so return false
else:
print("Nothing to save, because data parameter is empty")
return False
print("Nothing to save, because data parameter is empty") # Case of empty 'data' parameter
return False # Failed operation, so return false
# 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)
dataList = []
try:
data = self.collection.find(query)
for object in data:
dataList.append(object)
return dataList
dataList = [doc for doc in self.collection.find(query)] # MongoDB cursor, we need to convert this to a list of dicts
return dataList # Return the list
except Exception as e:
print(f'Exception: {e}')
return False
print(f'Exception: {e}') # Exception handling
return False # Failed operation, so return false
# 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)
update_type = kwargs.get('update_type', None)
data = kwargs.get('data', None)
try:
self.collection.update_many(query, {f'${update_type}': data})
return True
self.collection.update_many(query, {f'${update_type}': data}) # Query DB + update with data
return True # Operation was successful, so return true
except Exception as e:
print(f'Exception: {e}')
return False
print(f'Exception: {e}') # Exception handling
return False # Failed operation, so return false
# 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)
try:
self.collection.delete_many(query)
return True
return True # Operation was successful, so return true
except Exception as e:
print(f'Exception: {e}')
return False
print(f'Exception: {e}') # Exception handling
return False # Failed operation, so return false