Add files via upload

This commit is contained in:
Cody Cook 2024-04-14 19:31:38 -07:00 committed by GitHub
commit ac3a200a7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 876 additions and 0 deletions

125
AnimalShelter.py Normal file
View file

@ -0,0 +1,125 @@
# Cody Cook
# CS-340 Client/Server Development
# SNHU
# 2024/04/02
# Project One
# Import necessary libraries
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure, OperationFailure
from urllib.parse import quote_plus
from bson.objectid import ObjectId
# Define the AnimalShelter class
class AnimalShelter():
""" CRUD operations for Animal collection in MongoDB """
# Initialize the MongoClient and the database
def __init__(self, username, password, hostname, port, database, collection):
""" Initialize the MongoClient and the database """
connection = "mongodb://%s:%s@%s:%d" % (quote_plus(username), quote_plus(password), hostname, port)
# Try to connect to the database
try:
self.client = MongoClient(connection) # The client object is an instance of the MongoClient class
self.database = self.client['%s' % (database)] # The database object is an instance of the database class
self.collection = self.database['%s' % (collection)] # The collection object is an instance of the collection class
# If the connection fails, print an error message
except ConnectionFailure as e:
# If the connection fails, print an error message
print("Could not connect to MongoDB: %s" % e)
# Create method
def create(self, data):
""" Create a new animal in the database """
if data is not None:
# Try to insert the data into the database
try:
self.collection.insert_one(data) # Insert the data into the collection
print("Animal added successfully") # Print a success message
return True # Return True if successful
# If the operation fails, print an error message
except OperationFailure as e:
# If the operation fails, print an error message
print("Could not add animal: %s" % e)
# Return False if the operation fails
return False
else:
# If the data is empty, print an error message
print("Data is empty, nothing to add")
# Return False if the data is empty
return False
# Read method
def read(self, data):
""" Read an animal from the database """
if data is not None:
# Try to read the data from the database
try:
# Read the data from the collection
documents = list(self.collection.find(data, {"_id": False}))
# Return the documents
return documents
# If the operation fails, print an error message
except OperationFailure as e:
# Print an error message
print("Could not read animal: %s" % e)
# Return an empty list
return []
else:
# If the data is empty, print an error message
print("Data is empty, nothing to read")
# Return an empty list
return []
# Update method
def update(self, query, data):
""" Update an animal in the database """
# Check if the query and data are not empty
if query is not None and data is not None:
# Try to update the data in the database
try:
# Update the data in the collection
result = self.collection.update_many(query, {'$set': data})
# Print a success message
print(f"Animals updated: {result.modified_count}")
# Return the number of animals updated
return result.modified_count
except OperationFailure as e:
# If the operation fails, print an error message
print(f"Could not update animal: {e}")
return 0
else:
# If the data or query is empty, print an error message and return 0
print("Data and/or query is empty, nothing to update")
return 0
# Delete method
def delete(self, data):
""" Delete an animal from the database """
# Check if the data is not empty
if data is not None:
# Try to delete the data from the database
try:
# Delete the data from the collection
result = self.collection.delete_many(data)
# Print a success message
print(f"Animals deleted: {result.deleted_count}")
# Return the number of animals deleted
return result.deleted_count
except OperationFailure as e:
# If the operation fails, print an error message and return 0
print(f"Could not delete animal: {e}")
return 0
else:
# If the data is empty, print an error message and return 0
print("Data is empty, nothing to delete")
return 0