Completed Ch. 9 #5

Merged
JMR-dev merged 6 commits from jr-sqlandpython into main 2022-02-03 21:50:04 +00:00
8 changed files with 274 additions and 173 deletions
-3
View File
@@ -1,3 +0,0 @@
{
"python.pythonPath": "/Users/jason.ross/.pyenv/versions/3.9.1/bin/python"
}
+69
View File
@@ -0,0 +1,69 @@
CREATE TABLE `Location` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL,
`address` TEXT NOT NULL
);
CREATE TABLE `Customer` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL,
`address` TEXT NOT NULL,
`email` TEXT NOT NULL,
`password` TEXT NOT NULL
);
CREATE TABLE `Animal` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL,
`status` TEXT NOT NULL,
`breed` TEXT NOT NULL,
`customer_id` INTEGER NOT NULL,
`location_id` INTEGER,
FOREIGN KEY(`customer_id`) REFERENCES `Customer`(`id`),
FOREIGN KEY(`location_id`) REFERENCES `Location`(`id`)
);
CREATE TABLE `Employee` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL,
`address` TEXT NOT NULL,
`location_id` INTEGER NOT NULL,
FOREIGN KEY(`location_id`) REFERENCES `Location`(`id`)
);
INSERT INTO `Location` VALUES (null, 'Nashville North', "64 Washington Heights");
INSERT INTO `Location` VALUES (null, 'Nashville South', "101 Penn Ave");
INSERT INTO `Employee` VALUES (null, "Madi Peper", "35498 Madison Ave", 1);
INSERT INTO `Employee` VALUES (null, "Kristen Norris", "100 Main St", 1);
INSERT INTO `Employee` VALUES (null, "Meg Ducharme", "404 Unknown Ct", 2);
INSERT INTO `Employee` VALUES (null, "Hannah Hall", "204 Empty Ave", 1);
INSERT INTO `Employee` VALUES (null, "Leah Hoefling", "200 Success Way", 2);
INSERT INTO `Customer` VALUES (null, "Mo Silvera", "201 Created St", "mo@silvera.com", "password");
INSERT INTO `Customer` VALUES (null, "Bryan Nilsen", "500 Internal Error Blvd", "bryan@nilsen.com", "password");
INSERT INTO `Customer` VALUES (null, "Jenna Solis", "301 Redirect Ave", "jenna@solis.com", "password");
INSERT INTO `Customer` VALUES (null, "Emily Lemmon", "454 Mulberry Way", "emily@lemmon.com", "password");
INSERT INTO `Animal` VALUES (null, "Snickers", "Recreation", "Dalmation", 4, 1);
INSERT INTO `Animal` VALUES (null, "Jax", "Treatment", "Beagle", 1, 1);
INSERT INTO `Animal` VALUES (null, "Falafel", "Treatment", "Siamese", 4, 2);
INSERT INTO `Animal` VALUES (null, "Doodles", "Kennel", "Poodle", 3, 1);
INSERT INTO `Animal` VALUES (null, "Daps", "Kennel", "Boxer", 2, 2);
-- Get only the animal rows where the `id` field value is 3
SELECT
a.id,
a.name,
a.breed,
a.status,
a.location_id,
a.customer_id
FROM animal a
WHERE a.id = 3
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
+68 -41
View File
@@ -1,48 +1,48 @@
ANIMALS = [
{
"id": 1,
"name": "Snickers",
"species": "Dog",
"locationId": 1,
"customerId": 4,
"status": "Admitted"
},
{
"id": 2,
"name": "Gypsy",
"species": "Dog",
"locationId": 1,
"customerId": 2,
"status": "Admitted"
},
{
"id": 3,
"name": "Blue",
"species": "Cat",
"locationId": 2,
"customerId": 1,
"status": "Admitted"
}
]
import sqlite3
import json
from models.animal import Animal
def get_all_animals():
return ANIMALS
# Open a connection to the database
with sqlite3.connect("./kennel.sqlite3") as conn:
# Function with a single parameter
def get_single_animal(id):
# Variable to hold the found animal, if it exists
requested_animal = None
# Just use these. It's a Black Box.
conn.row_factory = sqlite3.Row
db_cursor = conn.cursor()
# Iterate the ANIMALS list above. Very similar to the
# for..of loops you used in JavaScript.
for animal in ANIMALS:
# Dictionaries in Python use [] notation to find a key
# instead of the dot notation that JavaScript used.
if animal["id"] == id:
requested_animal = animal
# Write the SQL query to get the information you want
db_cursor.execute("""
SELECT
a.id,
a.name,
a.breed,
a.status,
a.location_id,
a.customer_id
FROM animal a
""")
return requested_animal
# Initialize an empty list to hold all animal representations
animals = []
# Convert rows of data into a Python list
dataset = db_cursor.fetchall()
# Iterate list of data returned from database
for row in dataset:
# Create an animal instance from the current row.
# Note that the database fields are specified in
# exact order of the parameters defined in the
# Animal class above.
animal = Animal(row['id'], row['name'], row['breed'],
row['status'], row['location_id'],
row['customer_id'])
animals.append(animal.__dict__)
# Use `json` package to properly serialize list as JSON
return json.dumps(animals)
def create_animal(animal):
# Get the id value of the last animal in the list
@@ -85,4 +85,31 @@ def update_animal(id, new_animal):
break
def get_single_animal(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
a.id,
a.name,
a.breed,
a.status,
a.location_id,
a.customer_id
FROM animal a
WHERE a.id = ?
""", ( id, ))
# Load the single result into memory
data = db_cursor.fetchone()
# Create an animal instance from the current row
animal = Animal(data['id'], data['name'], data['breed'],
data['status'], data['location_id'],
data['customer_id'])
return json.dumps(animal.__dict__)
+66 -46
View File
@@ -1,55 +1,75 @@
CUSTOMERS = [
{
"id": 1,
"name": "Ryan Tanay",
"email": "ryan@tanay.com",
},
{
"id": 2,
"name": "Emma Beaton",
"email": "emma@beaton.com",
},
{
"id": 3,
"name": "Dani Adkins",
"email": "dani.adkins.com",
},
{
"id": 4,
"name": "Adam Oswalt",
"email": "adam@oswalt.com",
},
{
"id": 5,
"name": "Fletcher Bangs",
"email": "flangs@bangs.com",
},
{
"id": 6,
"name": "Angela Lee",
"email": "lee@lee.com",
},
{
"name": "mike mike",
"email": "m@m.com",
"id": 7
},
]
import sqlite3
import json
from models.customer import Customer
def get_all_customers():
return CUSTOMERS
# Open a connection to the database
with sqlite3.connect("./kennel.sqlite3") as conn:
def get_single_customer():
requested_customer = None
# Just use these. It's a Black Box.
conn.row_factory = sqlite3.Row
db_cursor = conn.cursor()
# Write the SQL query to get the information you want
db_cursor.execute("""
SELECT
c.id,
c.name,
c.address,
c.email,
c.password
FROM customer c
""")
# Initialize an empty list to hold all customer representations
customers = []
# Convert rows of data into a Python list
dataset = db_cursor.fetchall()
# Iterate list of data returned from database
for row in dataset:
# Create an customer instance from the current row.
# Note that the database fields are specified in
# exact order of the parameters defined in the
# Customer class above.
customer = Customer(row['id'], row['name'], row['address'],
row['email'], row['password'],
)
customers.append(customer.__dict__)
# Use `json` package to properly serialize list as JSON
return json.dumps(customers)
for customer in CUSTOMERS:
if customer["id"] == id:
requested_customer = customer
def get_single_customer(id):
with sqlite3.connect("./kennel.sqlite3") as conn:
conn.row_factory = sqlite3.Row
db_cursor = conn.cursor()
return requested_customer
# 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, ))
# Load the single result into memory
data = db_cursor.fetchone()
customer = Customer(data['id'], data['name'], data['address'],
data['email'], data['password']
)
return json.dumps(customer.__dict__)
def create_customer(customer):
# Get the id value of the last location in the list
+61 -79
View File
@@ -1,88 +1,70 @@
EMPLOYEES = [
{
"id": 1,
"name": "Jessica Younker",
"email": "jessica@younker.com"
},
{
"id": 2,
"name": "Jordan Nelson",
"email": "jordan@nelson.com"
},
{
"id": 3,
"name": "Zoe LeBlanc",
"email": "zoe@leblanc.com"
},
{
"name": "Meg Ducharme",
"email": "meg@ducharme.com",
"id": 4
},
{
"name": "Hannah Hall",
"email": "hannah@hall.com",
"id": 5
},
{
"name": "Emily Lemmon",
"email": "emily@lemmon.com",
"id": 6
},
{
"name": "Jordan Castelloe",
"email": "jordan@castelloe.com",
"id": 7
},
{
"name": "Leah Gwin",
"email": "leah@gwin.com",
"id": 8
},
{
"name": "Caitlin Stein",
"email": "caitlin@stein.com",
"id": 9
},
{
"name": "Greg Korte",
"email": "greg@korte.com",
"id": 10
},
{
"name": "Charisse Lambert",
"email": "charisse@lambert.com",
"id": 11
},
{
"name": "Madi Peper",
"email": "madi@peper.com",
"id": 12
},
{
"name": "Jenna Solis",
"email": "jenna@solis.com",
"id": 14
},
]
import sqlite3
import json
from models.employee import Employee
def get_all_employees():
return EMPLOYEES
# Open a connection to the database
with sqlite3.connect("./kennel.sqlite3") as conn:
def get_single_employee():
requested_employee = None
# Just use these. It's a Black Box.
conn.row_factory = sqlite3.Row
db_cursor = conn.cursor()
# Write the SQL query to get the information you want
db_cursor.execute("""
SELECT
e.id,
e.name,
e.address,
e.location_id
FROM employee e
""")
for employee in EMPLOYEES:
if EMPLOYEES["id"] == id:
requested_EMPLOYEE = employee
# Initialize an empty list to hold all employee representations
employees = []
return requested_employee
# Convert rows of data into a Python list
dataset = db_cursor.fetchall()
# Iterate list of data returned from database
for row in dataset:
# Create an employee instance from the current row.
# Note that the database fields are specified in
# exact order of the parameters defined in the
# Animal class above.
employee = Employee(row['id'], row['name'], row['address'],row['location_id'])
employees.append(employee.__dict__)
# Use `json` package to properly serialize list as JSON
return json.dumps(employees)
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, ))
# Load the single result into memory
data = db_cursor.fetchone()
employee = Employee(data['id'], data['name'], data['address'],
data['location_id']
)
return json.dumps(employee.__dict__)
def create_employee(employee):
# Get the id value of the last location in the list