Skip to content
Snippets Groups Projects
Commit dbf9161c authored by kainguyen's avatar kainguyen
Browse files

finished stateRepository

parent 14ae1361
No related branches found
No related tags found
1 merge request!5Populate state table
This commit is part of merge request !5. Comments created here will be created in the context of that merge request.
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment