saving refactors to get methods to use SQLite instead of hard-coded data

This commit is contained in:
2022-02-03 10:32:20 -06:00
parent 4c7e3533b2
commit 5ff224c2d2
3 changed files with 54 additions and 114 deletions
+12 -1
View File
@@ -55,4 +55,15 @@ 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);
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
+40 -41
View File
@@ -1,45 +1,44 @@
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
},
]
def get_all_customers():
return CUSTOMERS
# Open a connection to the database
with sqlite3.connect("./kennel.sqlite3") as conn:
# 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 animal a
""")
# 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 get_single_customer():
requested_customer = None
+2 -72
View File
@@ -1,75 +1,5 @@
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
def get_all_employees():
return EMPLOYEES