Completed Ch. 9 #5

Merged
JMR-dev merged 6 commits from jr-sqlandpython into main 2022-02-03 21:50:04 +00:00
5 changed files with 58 additions and 18 deletions
Showing only changes of commit a3293d833a - Show all commits
BIN
View File
Binary file not shown.
+4 -2
View File
@@ -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
self.address = address
self.email = email
self.password = password
+6 -2
View File
@@ -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
self.address = address
self.location_id = location_id
+26 -8
View File
@@ -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
+22 -6
View File
@@ -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