Skip to content
Snippets Groups Projects

Populate state table

Merged kainguyen requested to merge Populate-State-Table into main
1 file
+ 59
0
Compare changes
  • Side-by-side
  • Inline
+ 59
0
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
Loading