diff --git a/backend/config/database/database.config.js b/backend/config/database/database.config.js index 44c214b31340d6ca027f6acccd70fdc13de10938..38a315f03d59b6edc1ea71105c5d645ac8e2cf21 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 24d396804070d80fc40fc3bc9913b41cebbec9ee..f3db95995c92b6ff88c645270bdfa4224a1e6059 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 436c633f923c5f804e955b98929f53dfa54bca60..3c1feccdc55660dc6e25738728cc0e6a2e9be70c 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 0000000000000000000000000000000000000000..f30c679ceb1c905aade0deedeeee0a6a9ab71f18 --- /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 };