Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
build/
logs/
node_modules/
__pycache__/

# Specific Files
config.json
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
"type": "module",
"scripts": {
"start:dev": "nodemon --ignore src/data/notifRequests.json src/index.js",
"start": "node src/index.js"
"start": "node src/index.js",
"migrate": "node src/data/scripts/run-migrations.js",
"populate:db": "npm run migrate && python3 src/data/scripts/populate_db.py"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"better-sqlite3": "^12.4.1",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"firebase-admin": "^13.1.0",
Expand Down
Binary file modified src/.DS_Store
Binary file not shown.
40 changes: 38 additions & 2 deletions src/data/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,54 @@ def insert_library(location, address, latitude, longitude):
conn.close()


def insert_printer(location, description, latitude, longitude):
def insert_printer(location, description, labels, latitude, longitude):
"""Insert a printer into the database."""
conn = get_db_connection()
cursor = conn.cursor()

# We remove the "OR IGNORE" because we acknoledge that several printers may have the same location and description (i.e., same building and room), so we rely on the unique printer_id to identify the printer
cursor.execute(
"""
INSERT OR IGNORE INTO printers (location, description, latitude, longitude)
INSERT INTO printers (location, description, latitude, longitude)
VALUES (?, ?, ?, ?)
""",
(location, description, latitude, longitude),
)

# To get the printer_id, we do NOT rely on the location/description/coordinates, but rather on the printer_id that was just inserted (lastrowid), as several printers may have the same location and description (i.e., same building and room)
printer_id = cursor.lastrowid

# Insert labels into the labels table and get their IDs
label_ids = []
for label in labels:
cursor.execute(
"""
INSERT OR IGNORE INTO labels (label)
VALUES (?)
""",
(label,),
)
cursor.execute(
"""
SELECT id FROM labels WHERE label = ?
""",
(label,),
)
result = cursor.fetchone()
if result is None:
raise ValueError(f"Failed to find label: {label}")
label_id = result[0]
label_ids.append(label_id)

# Insert into junction table
for label_id in label_ids:
cursor.execute(
"""
INSERT OR IGNORE INTO printer_labels (printer_id, label_id)
VALUES (?, ?)
""",
(printer_id, label_id),
)

conn.commit()
conn.close()
2 changes: 1 addition & 1 deletion src/data/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def create_tables():
"""
CREATE TABLE IF NOT EXISTS printers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
location TEXT UNIQUE,
location TEXT,
description TEXT,
latitude REAL,
longitude REAL
Expand Down
6 changes: 6 additions & 0 deletions src/data/migrations/2025117_1854_create_labels.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS labels (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE NOT NULL
);
9 changes: 9 additions & 0 deletions src/data/migrations/2025117_1859_create_printer_labels.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS printer_labels (
printer_id INTEGER NOT NULL,
label_id INTEGER NOT NULL,
PRIMARY KEY (printer_id, label_id),
FOREIGN KEY (printer_id) REFERENCES printers(id) ON DELETE CASCADE,
FOREIGN KEY (label_id) REFERENCES labels(id) ON DELETE CASCADE
);
Loading