From 4c7e3533b281446db0b9310fb450638fcd02a203 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 10:01:00 -0600 Subject: [PATCH 1/6] saving changes to connect SQLite database --- kennel.sql | 58 +++++++++++++++++++++ kennel.sqlite3 | Bin 0 -> 24576 bytes views/animal_requests.py | 109 ++++++++++++++++++++++++--------------- 3 files changed, 126 insertions(+), 41 deletions(-) diff --git a/kennel.sql b/kennel.sql index e69de29..1cd76ac 100644 --- a/kennel.sql +++ b/kennel.sql @@ -0,0 +1,58 @@ +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); \ No newline at end of file diff --git a/kennel.sqlite3 b/kennel.sqlite3 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c087f04f29a75d7c3972af4533a9a4b34a466ab3 100644 GIT binary patch literal 24576 zcmeI)&u-&H7y$6GV<$?sU@8`sP=!vZs;$-S(myFIAf)`MyX`hfOOlH1X){fxjp^9# zj-Br2z@du}FM)Uqt_X<>>WLE~Ci5`NcU*W-_(U zN_N2MktZD%&^7V~Q52G+ln`UnCc+UPNxl{b01BYMKOk`4(GyddjPli|0Xr1ja!!TU<(K<-v1;Y(7Om$C zC5twqaDy(Sk`24nNN!i^);+6AE4y`C*)NsoUUj>iuO84H>wxC>>$}?(sG@9D>bIeU z%N*WF)~yGSc~Brc%~~zVegAJF!fUd?_S}bXEjoHFD7v{@wYKk7!mBMbu4XoA)!MYG zR;6gwXrnZ!rm+y-w3$koPakNBsdQTT`bMwadDnKB=)dd3ZSJ4EY%v|XjTdM#rDJXO z0~U1r#(3aRat@6hE70utf#+~}9mP=dqnjKhx?0IFQE^cHfM~Sfi`$y#C$at`W%g*J zilejk4H?7BF2m@OLDMNgUUKlWt*#)hg- znR{w{YH?9H=(W{ra!`M*EM}iCCcjGpcG|%5rdVWt6y*i_3H`=R1 zwLE*Y|BbI?$_)GFs_{KBeuX#OPyhu`00mG01yBG5Pyhu`00mG01zsJ28C4^T^A+Z| z&uk$$t$CfGy|TPQ_qglQ{2501BW03ZTG$P~c-N zPUe@EGL~bDE-i7#@!afsmXkW1NxG4w7EV>!`_-D!GG^nZf8u{fDqTFl&sVR#0k z^1}AlU{t=!TejrQARL+hA`IpHFm$zKUWKcEvXm*vE^}$c7Cv{^mzU|b8*u3|K`kjg zNekkvbr@#JxxFk`tH>&GGHaTdvPWySIOCG7!nKQ%vw*i~EpWV?-#>q~RM`L5jbmc` zX8dS;XPiS4Hxxht6hHwKKmim$0Te(16hHwKK!KMNcw0}9x6)IJvcXRM9rz7^i-Pxr zOI4lL5@Z?{u1IcpUQ6&naIbd|qcI51Oe@MJ6YPkKdf1zHxEs`LSY5#9nwnM<+H}j~M*{wLzX~JzwN4Wj z67<7q1Zqap$=t2v7K1qfkc$bix}2pjQ$Og2a|LKjC-bIK=EroS(`+;8@GQjl-4oY) z?9yVOzN_lQoJ;OV+YdO*8}KBgR^TdSX1g%vPZgcazMm|!mJMqIoN~Fgp8e=9L|e8G JYXHDC{sI$0Cx-w4 literal 0 HcmV?d00001 diff --git a/views/animal_requests.py b/views/animal_requests.py index a80c985..776dc28 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -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 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 - \ No newline at end of file +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__) \ No newline at end of file -- 2.47.3 From 5ff224c2d2e87e97df04c2ef141f4b6474358823 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 10:32:20 -0600 Subject: [PATCH 2/6] 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 -- 2.47.3 From 15554e6ef329d0ba94b7a9f09dc29eb3a3e8cdcf Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 10:41:46 -0600 Subject: [PATCH 3/6] completed adding get_all method to get all resources from SQLite database --- views/customer_requests.py | 17 +++++++++------- views/employees_requests.py | 40 ++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/views/customer_requests.py b/views/customer_requests.py index 9ca6085..19c6dd3 100644 --- a/views/customer_requests.py +++ b/views/customer_requests.py @@ -1,3 +1,6 @@ +import sqlite3 +import json + def get_all_customers(): # Open a connection to the database with sqlite3.connect("./kennel.sqlite3") as conn: @@ -14,11 +17,11 @@ def get_all_customers(): c.address, c.email, c.password, - FROM animal a + FROM customer c """) - # Initialize an empty list to hold all animal representations - animals = [] + # Initialize an empty list to hold all customer representations + customers = [] # Convert rows of data into a Python list dataset = db_cursor.fetchall() @@ -26,18 +29,18 @@ def get_all_customers(): # Iterate list of data returned from database for row in dataset: - # Create an animal instance from the current row. + # 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. - animal = Animal(row['id'], row['name'], row['breed'], + customer = Customer(row['id'], row['name'], row['breed'], row['status'], row['location_id'], row['customer_id']) - animals.append(animal.__dict__) + customers.append(customer.__dict__) # Use `json` package to properly serialize list as JSON - return json.dumps(animals) + return json.dumps(customers) def get_single_customer(): diff --git a/views/employees_requests.py b/views/employees_requests.py index 368feee..fa65d33 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -2,7 +2,45 @@ import sqlite3 import json def get_all_employees(): - return EMPLOYEES + # 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 + e.id, + e.name, + e.address, + e.email, + e.password, + FROM employee e + """) + + # Initialize an empty list to hold all employee representations + employees = [] + + # 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['breed'], + row['status'], row['location_id'], + row['employee_id']) + + employees.append(employee.__dict__) + + # Use `json` package to properly serialize list as JSON + return json.dumps(employees) def get_single_employee(): requested_employee = None -- 2.47.3 From d17834ecd099eb99b9f92077edaac41d790cdeab Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 13:53:43 -0600 Subject: [PATCH 4/6] correcting factory objects --- views/animal_requests.py | 2 +- views/customer_requests.py | 6 +++--- views/employees_requests.py | 8 +++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/views/animal_requests.py b/views/animal_requests.py index 776dc28..2e506b4 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -1,6 +1,6 @@ import sqlite3 import json -from models import Animal +from models.animal import Animal def get_all_animals(): # Open a connection to the database diff --git a/views/customer_requests.py b/views/customer_requests.py index 19c6dd3..a157259 100644 --- a/views/customer_requests.py +++ b/views/customer_requests.py @@ -33,9 +33,9 @@ def get_all_customers(): # Note that the database fields are specified in # exact order of the parameters defined in the # Animal class above. - customer = Customer(row['id'], row['name'], row['breed'], - row['status'], row['location_id'], - row['customer_id']) + customer = Customer(row['id'], row['name'], row['address'], + row['email'], row['password'], + ) customers.append(customer.__dict__) diff --git a/views/employees_requests.py b/views/employees_requests.py index fa65d33..e51a97a 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -1,5 +1,6 @@ import sqlite3 import json +from models.employee import Employee def get_all_employees(): # Open a connection to the database @@ -15,8 +16,7 @@ def get_all_employees(): e.id, e.name, e.address, - e.email, - e.password, + e.location_id FROM employee e """) @@ -33,9 +33,7 @@ def get_all_employees(): # 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['breed'], - row['status'], row['location_id'], - row['employee_id']) + employee = Employee(row['id'], row['name'], row['address'],row['location_id']) employees.append(employee.__dict__) -- 2.47.3 From a013d97057d4e2b7830b6cf32fbdcb74f3753896 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 14:14:58 -0600 Subject: [PATCH 5/6] saving changes to .vscode settings --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 29a20f8..e69de29 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +0,0 @@ -{ - "python.pythonPath": "/Users/jason.ross/.pyenv/versions/3.9.1/bin/python" -} \ No newline at end of file -- 2.47.3 From a3293d833a8b4160e024dded9fc42a5722c7f8a5 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 3 Feb 2022 15:48:48 -0600 Subject: [PATCH 6/6] 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 -- 2.47.3