From 06b4ecc96c3429fcbd366410cd617f8c96bee2cd Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 12 Jan 2022 13:32:51 -0600 Subject: [PATCH 1/5] saving changes --- request_handler.py | 53 ++++++++++++++++++++++++++-------------- views/__init__.py | 2 +- views/animal_requests.py | 17 ++++++++++++- views/employees_requests | 0 views/location_requests | 0 5 files changed, 52 insertions(+), 20 deletions(-) delete mode 100644 views/employees_requests delete mode 100644 views/location_requests diff --git a/request_handler.py b/request_handler.py index 6b168ac..76e3fdb 100644 --- a/request_handler.py +++ b/request_handler.py @@ -1,13 +1,34 @@ from http.server import BaseHTTPRequestHandler, HTTPServer -from views import get_all_animals +from views import get_all_animals, get_single_animal from http.server import BaseHTTPRequestHandler, HTTPServer + # Here's a class. It inherits from another class. # For now, think of a class as a container for functions that # work together for a common purpose. In this case, that # common purpose is to respond to HTTP requests from a client. class HandleRequests(BaseHTTPRequestHandler): + def parse_url(self, path): + # Just like splitting a string in JavaScript. If the + # path is "/animals/1", the resulting list will + # have "" at index 0, "animals" at index 1, and "1" + # at index 2. + path_params = path.split("/") + resource = path_params[1] + id = None + + # Try to get the item at index 2 + try: + # Convert the string "1" to the integer 1 + # This is the new parseInt() + id = int(path_params[2]) + except IndexError: + pass # No route parameter exists: /animals + except ValueError: + pass # Request had trailing slash: /animals/ + + return (resource, id) # This is a tuple # This is a Docstring it should be at the beginning of all classes and functions # It gives a description of the class or function """Controls the functionality of any GET, PUT, POST, DELETE requests to the server @@ -34,32 +55,28 @@ class HandleRequests(BaseHTTPRequestHandler): self.send_response(200) self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Methods', - 'GET, POST, PUT, DELETE') + 'GET, POST, PUT, DELETE') self.send_header('Access-Control-Allow-Headers', - 'X-Requested-With, Content-Type, Accept') + 'X-Requested-With, Content-Type, Accept') self.end_headers() # Here's a method on the class that overrides the parent's method. # It handles any GET request. - def do_GET(self): - """Handles GET requests to the server - """ - # Set the response code to 'Ok' - self._set_headers(200) + def do_GET(self): + self._set_headers(200) + response = {} # Default response - # Your new console.log() that outputs to the terminal - print(self.path) + # Parse the URL and capture the tuple that is returned + (resource, id) = self.parse_url(self.path) - if self.path == "/animals": - response = get_all_animals() -# It's an if..else statement + if resource == "animals": + if id is not None: + response = f"{get_single_animal(id)}" + else: + response = f"{get_all_animals()}" -else: - response = [] - - # This weird code sends a response back to the client - self.wfile.write(f"{response}".encode()) + self.wfile.write(response.encode()) # Here's a method on the class that overrides the parent's method. # It handles any POST request. diff --git a/views/__init__.py b/views/__init__.py index fa5bd8d..3f9c8fe 100644 --- a/views/__init__.py +++ b/views/__init__.py @@ -1 +1 @@ -from .animal_requests import get_all_animals \ No newline at end of file +from .animal_requests import get_all_animals, get_single_animal \ No newline at end of file diff --git a/views/animal_requests.py b/views/animal_requests.py index 75baa55..8d04552 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -24,4 +24,19 @@ ANIMALS = [ def get_all_animals(): - return ANIMALS \ No newline at end of file + return ANIMALS + +# Function with a single parameter +def get_single_animal(id): + # Variable to hold the found animal, if it exists + requested_animal = None + + # Iterate the ANIMALS list above. Very similar to the + # for..of loops you used in JavaScript. + for animal in ANIMALS: + # Dictionaries in Python use [] notation to find a key + # instead of the dot notation that JavaScript used. + if animal["id"] == id: + requested_animal = animal + + return requested_animal \ No newline at end of file diff --git a/views/employees_requests b/views/employees_requests deleted file mode 100644 index e69de29..0000000 diff --git a/views/location_requests b/views/location_requests deleted file mode 100644 index e69de29..0000000 From c119fa6921ece4ec5730ef3d614514575206762f Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 12 Jan 2022 13:33:46 -0600 Subject: [PATCH 2/5] saving changes for ch 3 &4 --- views/employees_requests.py | 0 views/location_requests.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 views/employees_requests.py create mode 100644 views/location_requests.py diff --git a/views/employees_requests.py b/views/employees_requests.py new file mode 100644 index 0000000..e69de29 diff --git a/views/location_requests.py b/views/location_requests.py new file mode 100644 index 0000000..e69de29 From 17040c282ff95964a1ef392477ee80f0e6950df2 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 12 Jan 2022 15:19:02 -0600 Subject: [PATCH 3/5] save completed ch. 3 work (not tested) --- request_handler.py | 20 ++++++++ views/customer_requests.py | 58 +++++++++++++++++++++++ views/employees_requests.py | 94 +++++++++++++++++++++++++++++++++++++ views/location_requests.py | 24 ++++++++++ 4 files changed, 196 insertions(+) create mode 100644 views/customer_requests.py diff --git a/request_handler.py b/request_handler.py index 76e3fdb..5262ef6 100644 --- a/request_handler.py +++ b/request_handler.py @@ -1,6 +1,8 @@ from http.server import BaseHTTPRequestHandler, HTTPServer from views import get_all_animals, get_single_animal from http.server import BaseHTTPRequestHandler, HTTPServer +from views.employees_requests import get_all_employees, get_single_employee +from views.location_requests import get_all_locations, get_single_location @@ -77,6 +79,24 @@ class HandleRequests(BaseHTTPRequestHandler): response = f"{get_all_animals()}" self.wfile.write(response.encode()) + + if resource == "locations": + if id is not None: + response = f"{get_single_location(id)}" + + else: + response = f"{get_all_locations()}" + + self.wfile.write(response.encode()) + + if resource == "employees": + if id is not None: + response = f"{get_single_employee(id)}" + + else: + response = f"{get_all_employees()}" + + self.wfile.write(response.encode()) # Here's a method on the class that overrides the parent's method. # It handles any POST request. diff --git a/views/customer_requests.py b/views/customer_requests.py new file mode 100644 index 0000000..ff0c418 --- /dev/null +++ b/views/customer_requests.py @@ -0,0 +1,58 @@ +CUSTOMERS = [ + { + "id": 1, + "name": "Ryan Tanay", + "email": "ryan@tanay.com", + "employee": false + }, + { + "id": 2, + "name": "Emma Beaton", + "email": "emma@beaton.com", + "employee": false + }, + { + "id": 3, + "name": "Dani Adkins", + "email": "dani.adkins.com", + "employee": false + }, + { + "id": 4, + "name": "Adam Oswalt", + "email": "adam@oswalt.com", + "employee": false + }, + { + "id": 5, + "name": "Fletcher Bangs", + "email": "flangs@bangs.com", + "employee": false + }, + { + "id": 6, + "name": "Angela Lee", + "email": "lee@lee.com", + "employee": false + }, + { + "name": "mike mike", + "email": "m@m.com", + "employee": false, + "id": 7 + }, + + ] + +def get_all_customers(): + return CUSTOMERS + +def get_single_customer(): + requested_customer = None + + + for customer in CUSTOMERS: + if customer["id"] == id: + requested_customer = customer + + return requested_customer diff --git a/views/employees_requests.py b/views/employees_requests.py index e69de29..80c4632 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -0,0 +1,94 @@ +EMPLOYEES = [ + { + "id": 1, + "name": "Jessica Younker", + "email": "jessica@younker.com", + "employee": true + }, + { + "id": 2, + "name": "Jordan Nelson", + "email": "jordan@nelson.com", + "employee": true + }, + { + "id": 3, + "name": "Zoe LeBlanc", + "email": "zoe@leblanc.com", + "employee": true + }, + { + "name": "Meg Ducharme", + "email": "meg@ducharme.com", + "id": 4, + "employee": true + }, + { + "name": "Hannah Hall", + "email": "hannah@hall.com", + "id": 5, + "employee": true + }, + { + "name": "Emily Lemmon", + "email": "emily@lemmon.com", + "id": 6, + "employee": true + }, + { + "name": "Jordan Castelloe", + "email": "jordan@castelloe.com", + "id": 7, + "employee": true + }, + { + "name": "Leah Gwin", + "email": "leah@gwin.com", + "id": 8, + "employee": true + }, + { + "name": "Caitlin Stein", + "email": "caitlin@stein.com", + "id": 9, + "employee": true + }, + { + "name": "Greg Korte", + "email": "greg@korte.com", + "id": 10, + "employee": true + }, + { + "name": "Charisse Lambert", + "email": "charisse@lambert.com", + "id": 11, + "employee": true + }, + { + "name": "Madi Peper", + "email": "madi@peper.com", + "id": 12, + "employee": true + }, + { + "name": "Jenna Solis", + "email": "jenna@solis.com", + "id": 14, + "employee": true + }, + +] + +def get_all_employees(): + return EMPLOYEES + +def get_single_employee(): + requested_employee = None + + + for employee in EMPLOYEES: + if EMPLOYEES["id"] == id: + requested_EMPLOYEE = employee + + return requested_employee \ No newline at end of file diff --git a/views/location_requests.py b/views/location_requests.py index e69de29..a14366c 100644 --- a/views/location_requests.py +++ b/views/location_requests.py @@ -0,0 +1,24 @@ +LOCATIONS = [ + { + "id": 1, + "name": "Nashville North" + }, + { + "id": 2, + "name": "Nashville South" + } + , +] + +def get_all_locations(): + return LOCATIONS + +def get_single_location(): + requested_location = None + + + for location in LOCATIONS: + if location["id"] == id: + requested_location = location + + return requested_location From cb4a93108c49f202aa4dcfe2efee38e73379eb7d Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Fri, 14 Jan 2022 10:48:24 -0600 Subject: [PATCH 4/5] working on post requests --- .vscode/launch.json | 1 + request_handler.py | 43 ++++++++++++++++++++++++++++--------- views/__init__.py | 8 ++++++- views/animal_requests.py | 18 +++++++++++++++- views/customer_requests.py | 14 ++++++------ views/employees_requests.py | 26 +++++++++++----------- 6 files changed, 78 insertions(+), 32 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index d8984a9..2b16460 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,7 @@ { "version": "0.2.0", "configurations": [ + {"name":"Python: Current File","type":"python","request":"launch","program":"${file}","console":"integratedTerminal"}, { "name": "Request Handler", "type": "python", diff --git a/request_handler.py b/request_handler.py index 5262ef6..3762760 100644 --- a/request_handler.py +++ b/request_handler.py @@ -1,8 +1,11 @@ from http.server import BaseHTTPRequestHandler, HTTPServer from views import get_all_animals, get_single_animal from http.server import BaseHTTPRequestHandler, HTTPServer +from views.customer_requests import get_all_customers, get_single_customer from views.employees_requests import get_all_employees, get_single_employee from views.location_requests import get_all_locations, get_single_location +from views.animal_requests import create_animal +import json @@ -64,8 +67,8 @@ class HandleRequests(BaseHTTPRequestHandler): # Here's a method on the class that overrides the parent's method. # It handles any GET request. - def do_GET(self): - self._set_headers(200) + def do_GET(self): + self._set_headers(200) response = {} # Default response # Parse the URL and capture the tuple that is returned @@ -78,7 +81,7 @@ class HandleRequests(BaseHTTPRequestHandler): else: response = f"{get_all_animals()}" - self.wfile.write(response.encode()) + if resource == "locations": if id is not None: @@ -87,7 +90,7 @@ class HandleRequests(BaseHTTPRequestHandler): else: response = f"{get_all_locations()}" - self.wfile.write(response.encode()) + if resource == "employees": if id is not None: @@ -96,21 +99,40 @@ class HandleRequests(BaseHTTPRequestHandler): else: response = f"{get_all_employees()}" + + if resource == "customers": + if id is not None: + response = f"{get_single_customer(id)}" + + else: + response = f"{get_all_customers()}" + self.wfile.write(response.encode()) # Here's a method on the class that overrides the parent's method. # It handles any POST request. def do_POST(self): - """Handles POST requests to the server - """ - # Set response code to 'Created' self._set_headers(201) - content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) - response = f"received post request:
{post_body}" - self.wfile.write(response.encode()) + # Convert JSON string to a Python dictionary + post_body = json.loads(post_body) + + # Parse the URL + (resource, id) = self.parse_url(self.path) + + # Initialize new animal + new_animal = None + + # Add a new animal to the list. Don't worry about + # the orange squiggle, you'll define the create_animal + # function next. + if resource == "animals": + new_animal = create_animal(post_body) + + # Encode the new animal and send in response + self.wfile.write(f"{new_animal}".encode()) # Here's a method on the class that overrides the parent's method. # It handles any PUT request. @@ -118,6 +140,7 @@ class HandleRequests(BaseHTTPRequestHandler): """Handles PUT requests to the server """ self.do_POST() + # This function is not inside the class. It is the starting diff --git a/views/__init__.py b/views/__init__.py index 3f9c8fe..f195576 100644 --- a/views/__init__.py +++ b/views/__init__.py @@ -1 +1,7 @@ -from .animal_requests import get_all_animals, get_single_animal \ No newline at end of file +from .animal_requests import get_all_animals, get_single_animal +from http.server import BaseHTTPRequestHandler, HTTPServer +from views import get_all_animals, get_single_animal +from http.server import BaseHTTPRequestHandler, HTTPServer +from views.employees_requests import get_all_employees, get_single_employee +from views.location_requests import get_all_locations, get_single_location +from views.animal_requests import create_animal diff --git a/views/animal_requests.py b/views/animal_requests.py index 8d04552..ef1fe97 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -39,4 +39,20 @@ def get_single_animal(id): if animal["id"] == id: requested_animal = animal - return requested_animal \ No newline at end of file + return requested_animal + +def create_animal(animal): + # Get the id value of the last animal in the list + max_id = ANIMALS[-1]["id"] + + # Add 1 to whatever that number is + new_id = max_id + 1 + + # Add an `id` property to the animal dictionary + animal["id"] = new_id + + # Add the animal dictionary to the list + ANIMALS.append(animal) + + # Return the dictionary with `id` property added + return animal diff --git a/views/customer_requests.py b/views/customer_requests.py index ff0c418..2e27b8f 100644 --- a/views/customer_requests.py +++ b/views/customer_requests.py @@ -3,42 +3,42 @@ CUSTOMERS = [ "id": 1, "name": "Ryan Tanay", "email": "ryan@tanay.com", - "employee": false + "employee": False }, { "id": 2, "name": "Emma Beaton", "email": "emma@beaton.com", - "employee": false + "employee": False }, { "id": 3, "name": "Dani Adkins", "email": "dani.adkins.com", - "employee": false + "employee": False }, { "id": 4, "name": "Adam Oswalt", "email": "adam@oswalt.com", - "employee": false + "employee": False }, { "id": 5, "name": "Fletcher Bangs", "email": "flangs@bangs.com", - "employee": false + "employee": False }, { "id": 6, "name": "Angela Lee", "email": "lee@lee.com", - "employee": false + "employee": False }, { "name": "mike mike", "email": "m@m.com", - "employee": false, + "employee": False, "id": 7 }, diff --git a/views/employees_requests.py b/views/employees_requests.py index 80c4632..cb96176 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -3,79 +3,79 @@ EMPLOYEES = [ "id": 1, "name": "Jessica Younker", "email": "jessica@younker.com", - "employee": true + "employee": True }, { "id": 2, "name": "Jordan Nelson", "email": "jordan@nelson.com", - "employee": true + "employee": True }, { "id": 3, "name": "Zoe LeBlanc", "email": "zoe@leblanc.com", - "employee": true + "employee": True }, { "name": "Meg Ducharme", "email": "meg@ducharme.com", "id": 4, - "employee": true + "employee": True }, { "name": "Hannah Hall", "email": "hannah@hall.com", "id": 5, - "employee": true + "employee": True }, { "name": "Emily Lemmon", "email": "emily@lemmon.com", "id": 6, - "employee": true + "employee": True }, { "name": "Jordan Castelloe", "email": "jordan@castelloe.com", "id": 7, - "employee": true + "employee": True }, { "name": "Leah Gwin", "email": "leah@gwin.com", "id": 8, - "employee": true + "employee": True }, { "name": "Caitlin Stein", "email": "caitlin@stein.com", "id": 9, - "employee": true + "employee": True }, { "name": "Greg Korte", "email": "greg@korte.com", "id": 10, - "employee": true + "employee": True }, { "name": "Charisse Lambert", "email": "charisse@lambert.com", "id": 11, - "employee": true + "employee": True }, { "name": "Madi Peper", "email": "madi@peper.com", "id": 12, - "employee": true + "employee": True }, { "name": "Jenna Solis", "email": "jenna@solis.com", "id": 14, - "employee": true + "employee": True }, ] From 449b94d3fdbf852c1fd377afb9d5100b53940f0c Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 19 Jan 2022 09:12:06 -0600 Subject: [PATCH 5/5] completed ch. 3 and 4 --- .vscode/settings.json | 3 ++ request_handler.py | 68 ++++++++++++++++++++++++++++++------- views/__init__.py | 13 ++++--- views/animal_requests.py | 34 +++++++++++++++++-- views/customer_requests.py | 32 +++++++++++++++++ views/employees_requests.py | 32 ++++++++++++++++- views/location_requests.py | 32 +++++++++++++++++ 7 files changed, 191 insertions(+), 23 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..29a20f8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/Users/jason.ross/.pyenv/versions/3.9.1/bin/python" +} \ No newline at end of file diff --git a/request_handler.py b/request_handler.py index 3762760..ea99a67 100644 --- a/request_handler.py +++ b/request_handler.py @@ -1,10 +1,8 @@ from http.server import BaseHTTPRequestHandler, HTTPServer -from views import get_all_animals, get_single_animal -from http.server import BaseHTTPRequestHandler, HTTPServer -from views.customer_requests import get_all_customers, get_single_customer -from views.employees_requests import get_all_employees, get_single_employee -from views.location_requests import get_all_locations, get_single_location -from views.animal_requests import create_animal +from views.customer_requests import get_all_customers, get_single_customer, create_customer, delete_customer +from views.employees_requests import create_employee, get_all_employees, get_single_employee, delete_employee +from views.location_requests import create_location, get_all_locations, get_single_location, delete_location +from views.animal_requests import create_animal, get_all_animals, get_single_animal, delete_animal, update_animal import json @@ -123,23 +121,69 @@ class HandleRequests(BaseHTTPRequestHandler): (resource, id) = self.parse_url(self.path) # Initialize new animal - new_animal = None + new_obj = None # Add a new animal to the list. Don't worry about # the orange squiggle, you'll define the create_animal # function next. if resource == "animals": - new_animal = create_animal(post_body) + new_obj = create_animal(post_body) + + if resource == "location": + new_obj = create_location(post_body) + + if resource == "employees": + new_obj = create_employee(post_body) + + if resource == "customers": + new_obj = create_customer(post_body) + # Encode the new animal and send in response - self.wfile.write(f"{new_animal}".encode()) + self.wfile.write(f"{new_obj}".encode()) # Here's a method on the class that overrides the parent's method. # It handles any PUT request. def do_PUT(self): - """Handles PUT requests to the server - """ - self.do_POST() + self._set_headers(204) + content_len = int(self.headers.get('content-length', 0)) + post_body = self.rfile.read(content_len) + post_body = json.loads(post_body) + + # Parse the URL + (resource, id) = self.parse_url(self.path) + + # Delete a single animal from the list + if resource == "animals": + update_animal(id, post_body) + + # Encode the new animal and send in response + self.wfile.write("".encode()) + + def do_DELETE(self): + # Set a 204 response code + self._set_headers(204) + + # Parse the URL + (resource, id) = self.parse_url(self.path) + + # Delete a single animal from the list + if resource == "animals": + delete_animal(id) + + if resource == "customers": + delete_customer(id) + + if resource == "employees": + delete_employee(id) + + if resource == "locations": + delete_location(id) + + # Encode the new animal and send in response + self.wfile.write("".encode()) + + diff --git a/views/__init__.py b/views/__init__.py index f195576..898abff 100644 --- a/views/__init__.py +++ b/views/__init__.py @@ -1,7 +1,6 @@ -from .animal_requests import get_all_animals, get_single_animal -from http.server import BaseHTTPRequestHandler, HTTPServer -from views import get_all_animals, get_single_animal -from http.server import BaseHTTPRequestHandler, HTTPServer -from views.employees_requests import get_all_employees, get_single_employee -from views.location_requests import get_all_locations, get_single_location -from views.animal_requests import create_animal + +from .employees_requests import get_all_employees, get_single_employee, create_employee, delete_employee +from .location_requests import get_all_locations, get_single_location, create_location, delete_location +from .animal_requests import create_animal, delete_animal, get_all_animals, get_single_animal, update_animal +from .customer_requests import get_all_customers, get_single_customer, create_customer, delete_customer + diff --git a/views/animal_requests.py b/views/animal_requests.py index ef1fe97..4b860eb 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -4,21 +4,24 @@ ANIMALS = [ "name": "Snickers", "species": "Dog", "locationId": 1, - "customerId": 4 + "customerId": 4, + "status": "Admitted" }, { "id": 2, "name": "Gypsy", "species": "Dog", "locationId": 1, - "customerId": 2 + "customerId": 2, + "status": "Admitted" }, { "id": 3, "name": "Blue", "species": "Cat", "locationId": 2, - "customerId": 1 + "customerId": 1, + "status": "Admitted" } ] @@ -56,3 +59,28 @@ def create_animal(animal): # Return the dictionary with `id` property added return animal + +def delete_animal(id): + # Initial -1 value for animal index, in case one isn't found + animal_index = -1 + + # Iterate the ANIMALS list, but use enumerate() so that you + # can access the index value of each item + for index, animal in enumerate(ANIMALS): + if animal["id"] == id: + # Found the animal. Store the current index. + animal_index = index + + # If the animal was found, use pop(int) to remove it from list + if animal_index >= 0: + ANIMALS.pop(animal_index) + + +def update_animal(id, new_animal): + # Iterate the ANIMALS list, but use enumerate() so that + # you can access the index value of each item. + for index, animal in enumerate(ANIMALS): + if animal["id"] == id: + # Found the animal. Update the value. + ANIMALS[index] = new_animal + break diff --git a/views/customer_requests.py b/views/customer_requests.py index 2e27b8f..d2bb355 100644 --- a/views/customer_requests.py +++ b/views/customer_requests.py @@ -56,3 +56,35 @@ def get_single_customer(): requested_customer = customer return requested_customer + +def create_customer(customer): + # Get the id value of the last location in the list + max_id = CUSTOMERS[-1]["id"] + + # Add 1 to whatever that number is + new_id = max_id + 1 + + # Add an `id` property to the location dictionary + customer["id"] = new_id + + # Add the location dictionary to the list + CUSTOMERS.append(customer) + + # Return the dictionary with `id` property added + return customer + +def delete_customer(id): + # Initial -1 value for customer index, in case one isn't found + customer_index = -1 + + # Iterate the CUSTOMERS list, but use enumerate() so that you + # can access the index value of each item + for index, customer in enumerate(CUSTOMERS): + if customer["id"] == id: + # Found the customer. Store the current index. + customer_index = index + + # If the customer was found, use pop(int) to remove it from list + if customer_index >= 0: + CUSTOMERS.pop(customer_index) + diff --git a/views/employees_requests.py b/views/employees_requests.py index cb96176..9a095cd 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -91,4 +91,34 @@ def get_single_employee(): if EMPLOYEES["id"] == id: requested_EMPLOYEE = employee - return requested_employee \ No newline at end of file + return requested_employee + +def create_employee(employee): + # Get the id value of the last location in the list + max_id = EMPLOYEES[-1]["id"] + + # Add 1 to whatever that number is + new_id = max_id + 1 + + # Add an `id` property to the location dictionary + employee["id"] = new_id + + # Add the location dictionary to the list + EMPLOYEES.append(employee) + + # Return the dictionary with `id` property added + return employee +def delete_employee(id): + # Initial -1 value for employee index, in case one isn't found + employee_index = -1 + + # Iterate the EMPLOYEES list, but use enumerate() so that you + # can access the index value of each item + for index, employee in enumerate(EMPLOYEES): + if employee["id"] == id: + # Found the employee. Store the current index. + employee_index = index + + # If the employee was found, use pop(int) to remove it from list + if employee_index >= 0: + EMPLOYEES.pop(employee_index) diff --git a/views/location_requests.py b/views/location_requests.py index a14366c..209aedb 100644 --- a/views/location_requests.py +++ b/views/location_requests.py @@ -22,3 +22,35 @@ def get_single_location(): requested_location = location return requested_location + +def create_location(location): + # Get the id value of the last location in the list + max_id = LOCATIONS[-1]["id"] + + # Add 1 to whatever that number is + new_id = max_id + 1 + + # Add an `id` property to the location dictionary + location["id"] = new_id + + # Add the location dictionary to the list + LOCATIONS.append(location) + + # Return the dictionary with `id` property added + return location + +def delete_location(id): + # Initial -1 value for location index, in case one isn't found + location_index = -1 + + # Iterate the LOCATIONS list, but use enumerate() so that you + # can access the index value of each item + for index, location in enumerate(LOCATIONS): + if location["id"] == id: + # Found the location. Store the current index. + location_index = index + + # If the location was found, use pop(int) to remove it from list + if location_index >= 0: + LOCATIONS.pop(location_index) +