diff --git a/.idea/CS-340_Project1.iml b/.idea/CS-340_Project1.iml index d0876a7..3345876 100644 --- a/.idea/CS-340_Project1.iml +++ b/.idea/CS-340_Project1.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index a971a2c..2502ee3 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ - + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4fe5733 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.11.8-alpine3.18 +COPY ./app /app +WORKDIR /app +RUN pip3 install -r requirements.txt +CMD ["python", "/app/main.py"] \ No newline at end of file diff --git a/app/AnimalShelter.py b/app/AnimalShelter.py new file mode 100644 index 0000000..62fd6fd --- /dev/null +++ b/app/AnimalShelter.py @@ -0,0 +1,59 @@ +from pymongo import MongoClient +from bson.objectid import ObjectId +class AnimalShelter(object): + """ CRUD operations for Animal collection in MongoDB """ + + def __init__(self): + # Initializing the MongoClient. This helps to + # access the MongoDB databases and collections. + # This is hard-wired to use the aac database, the + # animals collection, and the aac user. + # Definitions of the connection string variables are + # unique to the individual Apporto environment. + # + # You must edit the connection variables below to reflect + # your own instance of MongoDB! + # + # Connection Variables + # + USER = 'aacuser' + PASS = 'Ex9Ai3Y2eP' + HOST = 'db' + PORT = 27017 + DB = 'AAC' + COL = 'animals' + # + # Initialize Connection + # + self.client = MongoClient('mongodb://%s:%s@%s:%d' % (USER,PASS,HOST,PORT)) + self.database = self.client['%s' % (DB)] + self.collection = self.database['%s' % (COL)] + +# Complete this create method to implement the C in CRUD. + def create(self, *args, **kwargs): + data = kwargs.get('data', None) + if data is not None: + try: + self.database.animals.insert_one(data) # data should be dictionary + except Exception as e: + raise Exception(f'Error: {e}') + else: + raise Exception("Nothing to save, because data parameter is empty") + +# Create method to implement the R in CRUD. + def read(self, *args, **kwargs): + criteria = kwargs.get('criteria', None) + find_one = kwargs.get('once', False) + + if find_one: + try: + data = self.collection.find_one(criteria) + return data + except Exception as e: + raise Exception(f"Error: {e}") + else: + try: + data = self.collection.find(criteria) + return data + except Exception as e: + raise Exception(f"Error: {e}") \ No newline at end of file diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..e3f648f --- /dev/null +++ b/app/main.py @@ -0,0 +1,29 @@ +from AnimalShelter import AnimalShelter +test = AnimalShelter() + +test_criteria = {"breed": "Pit Bull Mix"} +test_data = test.read(criteria=test_criteria) + +for object in test_data: + print(object) + +test_data = { + 'rec_num': 10001, + 'age_upon_outcome': '3 years', + 'animal_id': 'FA69420', + 'animal_type': 'Furry', + 'breed': 'Protogen', + 'color': 'Purple', + 'date_of_birth': '1999-12-25', + 'datetime': '2019-10-11 10:03:00', + 'monthyear': '2019-10-11T10:03:00', + 'name': '*Brex', + 'outcome_subtype': 'Foster', + 'outcome_type': 'Adoption', + 'sex_upon_outcome': 'Male i think', + 'location_lat': 30.7118766251975, + 'location_long': -97.2999365773623, + 'age_upon_outcome_in_weeks': 558.059821428571 +} + +test.create(test_data) \ No newline at end of file diff --git a/app/requirements.txt b/app/requirements.txt new file mode 100644 index 0000000..8c7d698 --- /dev/null +++ b/app/requirements.txt @@ -0,0 +1 @@ +pymongo \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 22ae393..d6c19ed 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,8 @@ services: + app: + build: + context: . + dockerfile: Dockerfile db: image: mongo restart: unless-stopped