diff --git a/backend/repository/stateRepository.js b/backend/repository/stateRepository.js new file mode 100644 index 0000000000000000000000000000000000000000..13892feb60fe2a200ae7e5c7576c0b120705d975 --- /dev/null +++ b/backend/repository/stateRepository.js @@ -0,0 +1,59 @@ +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(stateID); + 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(stateId) { + // SQL query to get a certain state + const query = "SELECT * FROM stateLookup WHERE stateId = ?"; + + try { + console.log("Getting role: ", stateId); + const [rows] = await pool.query(query, [roleId]); + if (rows.length > 0) { + console.log("Found state: ", stateId); + return rows[0]; + } + else { + console.log("State not found: ", stateId); + return null; + } + } + catch (error) { + console.error(`Error fetching state with ID ${stateId}:`, error); + throw error; + } +} + +module.exports = { addStateIfNotExists, getState }; \ No newline at end of file