From e0fdaeb959595d0a3cf4f4a6889bb39b8ee187b3 Mon Sep 17 00:00:00 2001 From: kainguyen <kainguyen@vt.edu> Date: Mon, 28 Oct 2024 23:27:42 +0000 Subject: [PATCH] Populate state table --- backend/config/database/database.config.js | 2 + .../config/database/databaseInitializer.js | 68 ++++++++++++++++++ backend/config/database/schema.sql | 3 - backend/repository/stateRepository.js | 69 +++++++++++++++++++ 4 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 backend/repository/stateRepository.js diff --git a/backend/config/database/database.config.js b/backend/config/database/database.config.js index 44c214b..38a315f 100644 --- a/backend/config/database/database.config.js +++ b/backend/config/database/database.config.js @@ -52,6 +52,8 @@ const executeSQLFile = (filePath) => { } }); } + + console.log("Just added: ", statement); }); // release connection after all statements diff --git a/backend/config/database/databaseInitializer.js b/backend/config/database/databaseInitializer.js index 24d3968..f3db959 100644 --- a/backend/config/database/databaseInitializer.js +++ b/backend/config/database/databaseInitializer.js @@ -2,6 +2,10 @@ const { addUser, findByUsername } = require("../../repository/userRepository"); const { addRoleIfNotExists, } = require("../../repository/rolesPermissionRepository"); +const { + addStateIfNotExists, + checkTableExistence, +} = require("../../repository/stateRepository"); /* Class responsible for populating the database @@ -66,10 +70,74 @@ class DatabaseInitializer { } } + /* + Creates all the states + */ + async initializeStates() { + checkTableExistence(); + + const states = [ + "AL", + "AK", + "AZ", + "AR", + "CA", + "CO", + "CT", + "DE", + "FL", + "GA", + "HI", + "ID", + "IL", + "IN", + "IA", + "KS", + "KY", + "LA", + "ME", + "MD", + "MA", + "MI", + "MN", + "MS", + "MO", + "MT", + "NE", + "NV", + "NH", + "NJ", + "NM", + "NY", + "NC", + "ND", + "OH", + "OK", + "OR", + "PA", + "RI", + "SC", + "SD", + "TN", + "TX", + "UT", + "VT", + "VA", + "WA", + "WV", + "WI", + "WY", + ]; + for (const state of states) { + await addStateIfNotExists(state); + } + } + async initializeAll() { await this.testDBConnection(); await this.initializeRoles(); await this.initializeTestUsers(); + await this.initializeStates(); console.log("database init completed."); } diff --git a/backend/config/database/schema.sql b/backend/config/database/schema.sql index 436c633..3c1fecc 100644 --- a/backend/config/database/schema.sql +++ b/backend/config/database/schema.sql @@ -119,9 +119,6 @@ CREATE TABLE IF NOT EXISTS peopleXInvolvement ( FOREIGN KEY (involvementId) REFERENCES involvementLookup(involvementId) ); -INSERT INTO stateLookup (stateId, name) -VALUES (1, 'Virginia'); - INSERT INTO countryLookup (countryId, name) VALUES (1, 'United States'); diff --git a/backend/repository/stateRepository.js b/backend/repository/stateRepository.js new file mode 100644 index 0000000..f30c679 --- /dev/null +++ b/backend/repository/stateRepository.js @@ -0,0 +1,69 @@ +const { pool } = require("../config/database/database.config"); + +/* +Class responsible for making queries to the +stateLookup database. +*/ + +/* +Function to add a state to the database + +state must be a string and +*/ +async function addStateIfNotExists(state) { + console.log("adding state: ", state); + + // don't add state if it exists + const existingState = await getState(state); + if (existingState) { + console.log("State already exists: ", state); + return; + } + + // insert SQL query + const query = `INSERT INTO stateLookup (name) VALUES (?)`; + + // attempt to insert state into table + try { + await pool.query(query, [state]); + console.log(`Successfully added state: ${state}`); + } catch (error) { + console.error(`Error adding state: ${state}`, error); + throw error; + } +} + +async function getState(name) { + // SQL query to get a certain state + const query = "SELECT * FROM stateLookup WHERE name = ?"; + + try { + console.log("Getting state: ", name); + const [rows] = await pool.query(query, [name]); + if (rows.length > 0) { + console.log("Found state: ", name); + return rows[0]; + } else { + console.log("State not found: ", name); + return null; + } + } catch (error) { + console.error(`Error fetching state with ID ${name}:`, error); + throw error; + } +} + +async function checkTableExistence() { + try { + const [rows] = await pool.query("SHOW TABLES LIKE 'stateLookup'"); + if (rows.length > 0) { + console.log("Table `stateLookup` exists"); + } else { + console.log("Table `stateLookup` does not exist"); + } + } catch (error) { + console.error("Error checking table existence:", error); + } +} + +module.exports = { checkTableExistence, addStateIfNotExists, getState }; -- GitLab