diff --git a/kennel.sqlite3 b/kennel.sqlite3 index c087f04..9e39ed1 100644 Binary files a/kennel.sqlite3 and b/kennel.sqlite3 differ diff --git a/models/customer.py b/models/customer.py index 854aaaa..d4d807a 100644 --- a/models/customer.py +++ b/models/customer.py @@ -1,6 +1,8 @@ class Customer(): '''Factory class for customer objects''' - def __init__(self, id, name, email) -> None: + def __init__(self, id, name, address, email, password): self.id = id self.name = name - self.email = email \ No newline at end of file + self.address = address + self.email = email + self.password = password \ No newline at end of file diff --git a/models/employee.py b/models/employee.py index db1256e..ec01721 100644 --- a/models/employee.py +++ b/models/employee.py @@ -1,6 +1,10 @@ +from ctypes import addressof + + class Employee(): - def __init__(self, id, name, email): + def __init__(self, id, name, address, location_id): self.id = id self.name = name - self.email = email \ No newline at end of file + self.address = address + self.location_id = location_id \ No newline at end of file diff --git a/views/customer_requests.py b/views/customer_requests.py index a157259..5f995e4 100644 --- a/views/customer_requests.py +++ b/views/customer_requests.py @@ -1,5 +1,6 @@ import sqlite3 import json +from models.customer import Customer def get_all_customers(): # Open a connection to the database @@ -16,7 +17,7 @@ def get_all_customers(): c.name, c.address, c.email, - c.password, + c.password FROM customer c """) @@ -32,7 +33,7 @@ def get_all_customers(): # Create an customer instance from the current row. # Note that the database fields are specified in # exact order of the parameters defined in the - # Animal class above. + # Customer class above. customer = Customer(row['id'], row['name'], row['address'], row['email'], row['password'], ) @@ -43,15 +44,32 @@ def get_all_customers(): return json.dumps(customers) -def get_single_customer(): - requested_customer = None +def get_single_customer(id): + with sqlite3.connect("./kennel.sqlite3") as conn: + conn.row_factory = sqlite3.Row + db_cursor = conn.cursor() + # Use a ? parameter to inject a variable's value + # into the SQL statement. + db_cursor.execute(""" + SELECT + c.id, + c.name, + c.address, + c.email, + c.password + FROM customer c + WHERE c.id = ? + """, ( id, )) - for customer in CUSTOMERS: - if customer["id"] == id: - requested_customer = customer + # Load the single result into memory + data = db_cursor.fetchone() + + customer = Customer(data['id'], data['name'], data['address'], + data['email'], data['password'] + ) - return requested_customer + return json.dumps(customer.__dict__) def create_customer(customer): # Get the id value of the last location in the list diff --git a/views/employees_requests.py b/views/employees_requests.py index e51a97a..2306983 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -40,15 +40,31 @@ def get_all_employees(): # Use `json` package to properly serialize list as JSON return json.dumps(employees) -def get_single_employee(): - requested_employee = None +def get_single_employee(id): + with sqlite3.connect("./kennel.sqlite3") as conn: + conn.row_factory = sqlite3.Row + db_cursor = conn.cursor() + # Use a ? parameter to inject a variable's value + # into the SQL statement. + db_cursor.execute(""" + SELECT + e.id, + e.name, + e.address, + e.location_id + FROM employee e + WHERE e.id = ? + """, ( id, )) - for employee in EMPLOYEES: - if EMPLOYEES["id"] == id: - requested_EMPLOYEE = employee + # Load the single result into memory + data = db_cursor.fetchone() + + employee = Employee(data['id'], data['name'], data['address'], + data['location_id'] + ) - return requested_employee + return json.dumps(employee.__dict__) def create_employee(employee): # Get the id value of the last location in the list