From 5ff224c2d2e87e97df04c2ef141f4b6474358823 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 10:32:20 -0600 Subject: [PATCH] saving refactors to get methods to use SQLite instead of hard-coded data --- kennel.sql | 13 +++++- views/customer_requests.py | 81 ++++++++++++++++++------------------- views/employees_requests.py | 74 +-------------------------------- 3 files changed, 54 insertions(+), 114 deletions(-) diff --git a/kennel.sql b/kennel.sql index 1cd76ac..6dee66f 100644 --- a/kennel.sql +++ b/kennel.sql @@ -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); \ No newline at end of file +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 \ No newline at end of file diff --git a/views/customer_requests.py b/views/customer_requests.py index 905010f..9ca6085 100644 --- a/views/customer_requests.py +++ b/views/customer_requests.py @@ -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 diff --git a/views/employees_requests.py b/views/employees_requests.py index 991afdc..368feee 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -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