From 1b1ded5f369fc3051b1bc0ed5cb56f5d14a0a312 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 11 Mar 2024 22:42:05 -0400 Subject: [PATCH] update class --- app/AnimalShelter.py | 72 ++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/app/AnimalShelter.py b/app/AnimalShelter.py index 4550ed9..8cd72f0 100644 --- a/app/AnimalShelter.py +++ b/app/AnimalShelter.py @@ -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 \ No newline at end of file + print(f'Exception: {e}') # Exception handling + return False # Failed operation, so return false \ No newline at end of file