From dfdcf6712b770bafc54f0907c95ed2db8a6000f7 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 19 Jan 2022 14:07:38 -0600 Subject: [PATCH] completed chapter 6 --- request_handler.py | 13 ++++++--- views/__init__.py | 7 +++-- views/animal_requests.py | 4 ++- views/customer_requests.py | 9 +++++++ views/employees_requests.py | 53 +++++++++++++++++++------------------ views/location_requests.py | 9 +++++++ 6 files changed, 61 insertions(+), 34 deletions(-) diff --git a/request_handler.py b/request_handler.py index ea99a67..afead64 100644 --- a/request_handler.py +++ b/request_handler.py @@ -1,7 +1,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer -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.customer_requests import get_all_customers, get_single_customer, create_customer, delete_customer, update_customer +from views.employees_requests import create_employee, get_all_employees, get_single_employee, delete_employee, update_employee +from views.location_requests import create_location, get_all_locations, get_single_location, delete_location, update_location from views.animal_requests import create_animal, get_all_animals, get_single_animal, delete_animal, update_animal import json @@ -156,6 +156,13 @@ class HandleRequests(BaseHTTPRequestHandler): # Delete a single animal from the list if resource == "animals": update_animal(id, post_body) + if resource == "customers": + update_customer(id, post_body) + if resource == "employees": + update_employee(id, post_body) + if resource == "locations": + update_location(id, post_body) + # Encode the new animal and send in response self.wfile.write("".encode()) diff --git a/views/__init__.py b/views/__init__.py index 898abff..cd54720 100644 --- a/views/__init__.py +++ b/views/__init__.py @@ -1,6 +1,5 @@ - -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 .employees_requests import get_all_employees, get_single_employee, create_employee, delete_employee, update_employee +from .location_requests import get_all_locations, get_single_location, create_location, delete_location, update_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 +from .customer_requests import get_all_customers, get_single_customer, create_customer, delete_customer, update_customer diff --git a/views/animal_requests.py b/views/animal_requests.py index 4b860eb..a80c985 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -75,7 +75,6 @@ def delete_animal(id): 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. @@ -84,3 +83,6 @@ def update_animal(id, new_animal): # Found the animal. Update the value. ANIMALS[index] = new_animal break + + + \ No newline at end of file diff --git a/views/customer_requests.py b/views/customer_requests.py index d2bb355..d1566be 100644 --- a/views/customer_requests.py +++ b/views/customer_requests.py @@ -88,3 +88,12 @@ def delete_customer(id): if customer_index >= 0: CUSTOMERS.pop(customer_index) +def update_customer(id, new_customer): + # 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. Update the value. + CUSTOMERS[index] = new_customer + break + diff --git a/views/employees_requests.py b/views/employees_requests.py index 9a095cd..991afdc 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -2,80 +2,71 @@ EMPLOYEES = [ { "id": 1, "name": "Jessica Younker", - "email": "jessica@younker.com", - "employee": True + "email": "jessica@younker.com" + }, { "id": 2, "name": "Jordan Nelson", - "email": "jordan@nelson.com", - "employee": True + "email": "jordan@nelson.com" + }, { "id": 3, "name": "Zoe LeBlanc", - "email": "zoe@leblanc.com", - "employee": True + "email": "zoe@leblanc.com" + }, { "name": "Meg Ducharme", "email": "meg@ducharme.com", - "id": 4, - "employee": True + "id": 4 }, { "name": "Hannah Hall", "email": "hannah@hall.com", - "id": 5, - "employee": True + "id": 5 + }, { "name": "Emily Lemmon", "email": "emily@lemmon.com", - "id": 6, - "employee": True + "id": 6 }, { "name": "Jordan Castelloe", "email": "jordan@castelloe.com", - "id": 7, - "employee": True + "id": 7 }, { "name": "Leah Gwin", "email": "leah@gwin.com", - "id": 8, - "employee": True + "id": 8 }, { "name": "Caitlin Stein", "email": "caitlin@stein.com", - "id": 9, - "employee": True + "id": 9 }, { "name": "Greg Korte", "email": "greg@korte.com", - "id": 10, - "employee": True + "id": 10 }, { "name": "Charisse Lambert", "email": "charisse@lambert.com", - "id": 11, - "employee": True + "id": 11 }, { "name": "Madi Peper", "email": "madi@peper.com", - "id": 12, - "employee": True + "id": 12 }, { "name": "Jenna Solis", "email": "jenna@solis.com", - "id": 14, - "employee": True + "id": 14 }, ] @@ -122,3 +113,13 @@ def delete_employee(id): # If the employee was found, use pop(int) to remove it from list if employee_index >= 0: EMPLOYEES.pop(employee_index) + + +def update_employee(id, new_employee): + # 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. Update the value. + EMPLOYEES[index] = new_employee + break diff --git a/views/location_requests.py b/views/location_requests.py index 209aedb..04c3730 100644 --- a/views/location_requests.py +++ b/views/location_requests.py @@ -53,4 +53,13 @@ def delete_location(id): # If the location was found, use pop(int) to remove it from list if location_index >= 0: LOCATIONS.pop(location_index) + +def update_location(id, new_location): + # 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. Update the value. + LOCATIONS[index] = new_location + break