Archived
completed ch. 3 and 4
This commit is contained in:
Vendored
+3
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"python.pythonPath": "/Users/jason.ross/.pyenv/versions/3.9.1/bin/python"
|
||||||
|
}
|
||||||
+56
-12
@@ -1,10 +1,8 @@
|
|||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
from views import get_all_animals, get_single_animal
|
from views.customer_requests import get_all_customers, get_single_customer, create_customer, delete_customer
|
||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
from views.employees_requests import create_employee, get_all_employees, get_single_employee, delete_employee
|
||||||
from views.customer_requests import get_all_customers, get_single_customer
|
from views.location_requests import create_location, get_all_locations, get_single_location, delete_location
|
||||||
from views.employees_requests import get_all_employees, get_single_employee
|
from views.animal_requests import create_animal, get_all_animals, get_single_animal, delete_animal, update_animal
|
||||||
from views.location_requests import get_all_locations, get_single_location
|
|
||||||
from views.animal_requests import create_animal
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
@@ -123,23 +121,69 @@ class HandleRequests(BaseHTTPRequestHandler):
|
|||||||
(resource, id) = self.parse_url(self.path)
|
(resource, id) = self.parse_url(self.path)
|
||||||
|
|
||||||
# Initialize new animal
|
# Initialize new animal
|
||||||
new_animal = None
|
new_obj = None
|
||||||
|
|
||||||
# Add a new animal to the list. Don't worry about
|
# Add a new animal to the list. Don't worry about
|
||||||
# the orange squiggle, you'll define the create_animal
|
# the orange squiggle, you'll define the create_animal
|
||||||
# function next.
|
# function next.
|
||||||
if resource == "animals":
|
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
|
# 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.
|
# Here's a method on the class that overrides the parent's method.
|
||||||
# It handles any PUT request.
|
# It handles any PUT request.
|
||||||
|
|
||||||
def do_PUT(self):
|
def do_PUT(self):
|
||||||
"""Handles PUT requests to the server
|
self._set_headers(204)
|
||||||
"""
|
content_len = int(self.headers.get('content-length', 0))
|
||||||
self.do_POST()
|
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())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+6
-7
@@ -1,7 +1,6 @@
|
|||||||
from .animal_requests import get_all_animals, get_single_animal
|
|
||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
from .employees_requests import get_all_employees, get_single_employee, create_employee, delete_employee
|
||||||
from views import get_all_animals, get_single_animal
|
from .location_requests import get_all_locations, get_single_location, create_location, delete_location
|
||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
from .animal_requests import create_animal, delete_animal, get_all_animals, get_single_animal, update_animal
|
||||||
from views.employees_requests import get_all_employees, get_single_employee
|
from .customer_requests import get_all_customers, get_single_customer, create_customer, delete_customer
|
||||||
from views.location_requests import get_all_locations, get_single_location
|
|
||||||
from views.animal_requests import create_animal
|
|
||||||
|
|||||||
@@ -4,21 +4,24 @@ ANIMALS = [
|
|||||||
"name": "Snickers",
|
"name": "Snickers",
|
||||||
"species": "Dog",
|
"species": "Dog",
|
||||||
"locationId": 1,
|
"locationId": 1,
|
||||||
"customerId": 4
|
"customerId": 4,
|
||||||
|
"status": "Admitted"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 2,
|
"id": 2,
|
||||||
"name": "Gypsy",
|
"name": "Gypsy",
|
||||||
"species": "Dog",
|
"species": "Dog",
|
||||||
"locationId": 1,
|
"locationId": 1,
|
||||||
"customerId": 2
|
"customerId": 2,
|
||||||
|
"status": "Admitted"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 3,
|
"id": 3,
|
||||||
"name": "Blue",
|
"name": "Blue",
|
||||||
"species": "Cat",
|
"species": "Cat",
|
||||||
"locationId": 2,
|
"locationId": 2,
|
||||||
"customerId": 1
|
"customerId": 1,
|
||||||
|
"status": "Admitted"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -56,3 +59,28 @@ def create_animal(animal):
|
|||||||
|
|
||||||
# Return the dictionary with `id` property added
|
# Return the dictionary with `id` property added
|
||||||
return animal
|
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
|
||||||
|
|||||||
@@ -56,3 +56,35 @@ def get_single_customer():
|
|||||||
requested_customer = customer
|
requested_customer = customer
|
||||||
|
|
||||||
return requested_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)
|
||||||
|
|
||||||
|
|||||||
@@ -91,4 +91,34 @@ def get_single_employee():
|
|||||||
if EMPLOYEES["id"] == id:
|
if EMPLOYEES["id"] == id:
|
||||||
requested_EMPLOYEE = employee
|
requested_EMPLOYEE = employee
|
||||||
|
|
||||||
return requested_employee
|
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)
|
||||||
|
|||||||
@@ -22,3 +22,35 @@ def get_single_location():
|
|||||||
requested_location = location
|
requested_location = location
|
||||||
|
|
||||||
return requested_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)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user