diff --git a/backend/config/database/databaseInitializer.js b/backend/config/database/databaseInitializer.js index d52c90ff0eddd5dba0e00318478bb0cfcd70377a..f38388a33d8887dccd6b3097dc408ef9f40884d9 100644 --- a/backend/config/database/databaseInitializer.js +++ b/backend/config/database/databaseInitializer.js @@ -4,7 +4,7 @@ const { } = require("../../repository/rolesPermissionRepository"); const { addStateIfNotExists, - checkTableExistence, + checkStateTableExistence, } = require("../../repository/stateRepository"); const { addInvolvementTypeIfNotExists, @@ -12,6 +12,10 @@ const { checkInvolvementTypeTableExistence, checkInvolvementLookupTableExistence, } = require("../../repository/involvementRepository"); +const { + checkChapterTableExistence, + addChapterIfNotExists +} = require("../../repository/chapterRepository"); /* Class responsible for populating the database @@ -80,7 +84,7 @@ class DatabaseInitializer { Creates all the states */ async initializeStates() { - checkTableExistence(); + checkStateTableExistence(); const states = [ "AL", @@ -234,6 +238,139 @@ class DatabaseInitializer { } } + /* + Creates all the chapters + */ + async initializeChapters() { + checkChapterTableExistence(); + + const chapters = [ + "Alleghany Highlands #1", + "Augusta #29", + "Central Virginia #16", + "Charlottesville #7", + "Commonwealth #11", + "Danville #8", + "Eastern Shore #9", + "Fauquier #12", + "Franklin County/Smith Mountain Lake #3", + "Fredericksburg #13", + "Greensville/Southampton #10", + "Halifax #37", + "Loudoun County #15", + "Massanutten #18", + "D.C. Metro Area #6", + "New River Valley #19", + "Patrick Henry #17", + "Peanut #20", + "Peninsula #21", + "Piedmont #22", + "Prince William #23", + "Richmond #24", + "Roanoke Valley #25", + "Rockbridge #26", + "Shenandoah #27", + "Southside #36", + "Southwest Virginia #28", + "TideNeck #30", + "Tidewater #31", + "Tri-Cities #32", + "Williamsburg #33", + "Wythe-Bland #34", + "Belgium #239", + "China - Beijing #237", + "China - Shanghai #236", + "United Kingdom #250", + "Germany #235", + "Hong Kong #200", + "India - Hyderabad #232", + "India - Mumbai #210", + "Jordan #240", + "South Africa #238", + "Turkey #234", + "New Zealand #251", + "North Alabama #123", + "Central Alabama (Birmingham) #50", + "Kansas City #132", + "St. Louis #77", + "Greater Phoenix #124", + "New England #75", + "Northwest Arkansas #155", + "New Jersey #79", + "Los Angeles #52", + "Orange County #117", + "Sacramento #125", + "San Diego #119", + "San Francisco Bay Area #51", + "New York Capital District #82", + "New York City #83", + "Syracuse #85", + "Denver #53", + "Asheville", + "Charlotte #86", + "Coastal Carolina #87", + "Crystal Coast #151", + "Greenville #150", + "NC Triad #90", + "Triangle #88", + "Connecticut #54", + "First State #55", + "Cincinnati #92", + "Northeast Ohio #95", + "Central Florida #62", + "Jacksonville #59", + "Northwest Florida/Mobile #61", + "South Florida #57", + "Southwest Florida #142", + "Tallahassee #60", + "Tampa Bay #63", + "The Villages #231", + "Portland #96", + "Central Pennsylvania #121", + "Eastern Pennsylvania #48", + "Philadelphia #97", + "Pittsburgh #98", + "Atlanta #65", + "Central Savannah River Area #66", + "Classic City #145", + "Savannah #148", + "Charleston #102", + "Columbia #103", + "Grand Strand - Myrtle Beach #14", + "Palmetto #91", + "Hawaii #135", + "Chattanooga #105", + "East Tennessee #106", + "Knoxville #107", + "Memphis #108", + "Middle Tennessee #109", + "Idaho", + "Chicago #67", + "Austin #120", + "Dallas-Fort Worth #110", + "Houston #111", + "San Antonio #137", + "Indianapolis #130", + "Iowa #78", + "Utah #128", + "Kentuckiana #68", + "Greater Seattle #112", + "New Orleans/Baton Rouge #70", + "Vandalia #114", + "Annapolis #72", + "Baltimore #71", + "Western Maryland #73", + "Wisconsin #126", + "Southeastern Michigan #76", + "Minnesota #131", + ]; + + for (const chapter of chapters) { + await addChapterIfNotExists(chapter); + } + } + + async initializeAll() { await this.testDBConnection(); @@ -242,6 +379,7 @@ class DatabaseInitializer { await this.initializeStates(); await this.initializeInvolvementTypes(); await this.initializeInvolvementLookups(); + await this.initializeChapters(); console.log("database init completed."); } diff --git a/backend/repository/chapterRepository.js b/backend/repository/chapterRepository.js new file mode 100644 index 0000000000000000000000000000000000000000..11400c3f16e7d56440ef3d6b3c6ac2546637f2fb --- /dev/null +++ b/backend/repository/chapterRepository.js @@ -0,0 +1,75 @@ +const { pool } = require("../config/database/database.config"); + +/* +Class responsible for making queries to the +chapterLookup database. +*/ + +/* +Function that adds chapter to database if it doesn't aready exist + +chapter must be a string +*/ +async function addChapterIfNotExists(chapter) { + console.log("adding chapter: ", chapter); + + // don't add chapter if it exists + const existingChapter = await getChapter(chapter); + if (existingChapter) { + console.log("Chapter already exists: ", chapter); + return; + } + + // insert SQL query + const query = `INSERT INTO chapterLookup (name) VALUES (?)`; + + // attempt to insert chapter into table + try { + await pool.query(query, [chapter]); + console.log(`Successfully added chapter: ${chapter}`); + } catch (error) { + console.error(`Error adding chapter: ${chapter}`, error); + throw error; + } +} + +/* +Function to get chapter, used to check if it exists in the database or not +*/ +async function getChapter(name) { + // SQL queery to get a certain chapter + const query = "SELECT * FROM chapterLookup WHERE name = ?" + + try { + console.log("Getting chapter: ", name); + const [rows] = await pool.query(query, [name]); + if (rows.length > 0) { + console.log("Found chapter: ", name); + return rows[0]; + } + else { + console.log("Chapter not found: ", name); + return null; + } + } catch (error) { + console.error(`Error fetching chapter with ID ${name}:`, error); + } +} + +/* +Checks if table already exists +*/ +async function checkChapterTableExistence() { + try { + const [rows] = await pool.query("SHOW TABLES LIKE 'chapterLookup'"); + if (rows.length > 0) { + console.log("Table `chapterLookup` exists"); + } else { + console.log("Table `chapterLookup` does not exist"); + } + } catch (error) { + console.error("Error checking table existence:", error); + } +} + +module.exports = { checkChapterTableExistence, addChapterIfNotExists, getChapter }; \ No newline at end of file diff --git a/backend/repository/stateRepository.js b/backend/repository/stateRepository.js index f30c679ceb1c905aade0deedeeee0a6a9ab71f18..14e489d053bf2320fae6ef639ef9f320cc51e946 100644 --- a/backend/repository/stateRepository.js +++ b/backend/repository/stateRepository.js @@ -8,7 +8,7 @@ stateLookup database. /* Function to add a state to the database -state must be a string and +state must be a string */ async function addStateIfNotExists(state) { console.log("adding state: ", state); @@ -53,7 +53,7 @@ async function getState(name) { } } -async function checkTableExistence() { +async function checkStateTableExistence() { try { const [rows] = await pool.query("SHOW TABLES LIKE 'stateLookup'"); if (rows.length > 0) { @@ -66,4 +66,4 @@ async function checkTableExistence() { } } -module.exports = { checkTableExistence, addStateIfNotExists, getState }; +module.exports = { checkStateTableExistence, addStateIfNotExists, getState };