From a3293d833a8b4160e024dded9fc42a5722c7f8a5 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 15:48:48 -0600 Subject: [PATCH] saving progress for GET single resources --- kennel.sqlite3 | Bin 24576 -> 24576 bytes models/customer.py | 6 ++++-- models/employee.py | 8 ++++++-- views/customer_requests.py | 34 ++++++++++++++++++++++++++-------- views/employees_requests.py | 28 ++++++++++++++++++++++------ 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/kennel.sqlite3 b/kennel.sqlite3 index c087f04f29a75d7c3972af4533a9a4b34a466ab3..9e39ed1b03d1950907c06a08dbcee03740f299aa 100644 GIT binary patch delta 81 zcmV-X0IvUlzyW~30gxL37?B)90T{7hgD(LHljJWT6bKF?01wI!hYw8;5f0l9m=0bJ nBC`r7u+l;z{tN@(Ehyw E08g 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