From c4860d1fbbd9e73f9b28f38da4cde423ff59ea6c Mon Sep 17 00:00:00 2001 From: Federico Hurtado <fed_home@Federicos-Mac-mini.local> Date: Thu, 24 Oct 2024 13:26:46 -0400 Subject: [PATCH] Working repository classes with most functionality complete. --- backend/repository/addressRepository.js | 65 +++++++- backend/repository/contactRepository.js | 97 +++++++++++- backend/repository/degreeRepository.js | 201 +++++++++++++++++++++++- backend/repository/peopleRepository.js | 132 +++++++++++++++- 4 files changed, 486 insertions(+), 9 deletions(-) diff --git a/backend/repository/addressRepository.js b/backend/repository/addressRepository.js index 06da1ce..6173798 100644 --- a/backend/repository/addressRepository.js +++ b/backend/repository/addressRepository.js @@ -62,6 +62,69 @@ async function addAddress(address, peopleId) { } } +async function updateAddress() { + console.log("Update address not implemented!"); +} + +/* +Function to find all address information associated with a person. +Returns an array of address objects or an empty array if no addresses exist. +*/ +async function getAddressesForPerson(peopleId) { + console.log("Finding addresses for person with ID: ", peopleId); + + try { + const query = ` + SELECT + a.addressId, + a.address1, + a.address2, + a.city, + sl.name AS state, + cl.name AS country, + a.zipCode, + pa.preferredAddress + FROM + peopleXAddress pa + LEFT JOIN + address a ON pa.addressId = a.addressId + LEFT JOIN + stateLookup sl ON a.stateId = sl.stateId + LEFT JOIN + countryLookup cl ON a.countryId = cl.countryId + WHERE + pa.peopleId = ?; + `; + + // Execute the query + const [results] = await pool.query(query, [peopleId]); + + if (results.length === 0) { + console.log("No addresses found for person with ID:", peopleId); + return []; + } + + // Map the results to structured address objects + const addresses = results.map((row) => ({ + addressId: row.addressId, + address1: row.address1, + address2: row.address2, + city: row.city, + state: row.state, + country: row.country, + zipCode: row.zipCode, + preferredAddress: row.preferredAddress, + })); + + console.log("Found addresses: ", addresses); + + return addresses; + } catch (error) { + console.error("Error fetching addresses for person: ", error); + return []; + } +} + // Private helper function to look up the stateId async function _lookupStateId(state) { // for now return 1 while tables are not populated @@ -106,4 +169,4 @@ async function _lookupCountryId(country) { // } } -module.exports = { addAddress }; +module.exports = { addAddress, getAddressesForPerson }; diff --git a/backend/repository/contactRepository.js b/backend/repository/contactRepository.js index 0a091e9..404d9c1 100644 --- a/backend/repository/contactRepository.js +++ b/backend/repository/contactRepository.js @@ -2,7 +2,8 @@ const { pool } = require("../config/database/database.config"); /* Function to add a row into the peopleContact -table with the contact info given +table with the contact info given. Returns the id of the new contact. +If no contact is added, null is returned */ async function addContact(contact, peopleId) { // extract the parts of the contact @@ -25,11 +26,101 @@ async function addContact(contact, peopleId) { preferredContact, ]); - return results; + return results.insertId; } catch (error) { console.log(error); return null; } } -module.exports = { addContact }; +/* +Function to update the data for a specific contact. The +function will return true if the update was successful and false +otherwise. +*/ +async function updateContact(peopleContactId, peopleId, contactInfo) { + // extract the parts of the contact + let { contactNumber, contactType, preferredContact } = contactInfo; + + console.log("updating contact...."); + + try { + const query = ` + UPDATE peopleContact + SET + peopleId = ?, + contactNumber = ?, + contactType = ?, + preferredContact = ? + WHERE peopleContactId = ? + `; + + const [results] = await pool.query(query, [ + peopleId, + contactNumber, + contactType, + preferredContact, + peopleContactId, + ]); + + // Check if any rows were affected + if (results.affectedRows === 0) { + console.error("Contact not found. no rows updated"); + return false; + } + + // return true to signify success + return true; + } catch (error) { + console.log("Error updating contact: ", error); + return false; + } +} + +/* +Function to find all contact information associated with a person. +Returns an array of contact objects or an empty array if no contacts exist. +*/ +async function getContactsForPerson(peopleId) { + console.log("Finding contacts for person with ID: ", peopleId); + + try { + // get all contacts with the wanted peopleId + const query = ` + SELECT + peopleContactId, + contactNumber, + contactType, + preferredContact + FROM + peopleContact + WHERE + peopleId = ?; + `; + + // Execute the query + const [results] = await pool.query(query, [peopleId]); + + if (results.length === 0) { + console.log("No contacts found for person with ID:", peopleId); + return []; // empty array, no contacts found + } + + // Map the results to structured contact objects + const contacts = results.map((row) => ({ + peopleContactId: row.peopleContactId, + contactNumber: row.contactNumber, + contactType: row.contactType, + preferredContact: row.preferredContact, + })); + + console.log("Found contacts: ", contacts); + + return contacts; + } catch (error) { + console.error("Error fetching contacts for person: ", error); + return []; // empty array, no contacts found + } +} + +module.exports = { addContact, updateContact, getContactsForPerson }; diff --git a/backend/repository/degreeRepository.js b/backend/repository/degreeRepository.js index 9e7835f..09aacfc 100644 --- a/backend/repository/degreeRepository.js +++ b/backend/repository/degreeRepository.js @@ -1,7 +1,8 @@ const { pool } = require("../config/database/database.config"); /* -Add degree information into the database. +Add degree information into the database. The method will return +the id of the newly inserted degree, or null if nothing was added. */ async function addDegree(degree, peopleId) { // get all the parts of the degree @@ -25,6 +26,14 @@ async function addDegree(degree, peopleId) { VALUES (?,?,?,?,?,?) `; + console.log("Adding degree with the following details:"); + console.log("People ID:", peopleId); + console.log("Degree Type:", degreeType); + console.log("Degree Department:", degreeDepartment); + console.log("Degree College:", degreeCollege); + console.log("Degree Year:", degreeYear); + console.log("Degree Description:", degreeDescription); + // insert data into db const [results] = await pool.query(query, [ peopleId, @@ -35,7 +44,14 @@ async function addDegree(degree, peopleId) { degreeDescription, ]); - return results; + console.log("Inserted degree: ", results.insertId); + + const checkQuery = `SELECT * FROM peopleDegree WHERE peopleDegreeId = ?`; + const [checkResults] = await pool.query(checkQuery, [results.insertId]); + + console.log(checkResults[0]); + + return results.insertId; } catch (error) { // return null if there is an error console.error(error); @@ -43,6 +59,96 @@ async function addDegree(degree, peopleId) { } } +/* +Function to update the information about a specific degree +in the database. The function will return true if a row +was successfully updated and false otherwise. +*/ +async function updateDegree(peopleDegreeId, peopleId, degreeInfo) { + console.log("updating degree...."); + + // get all the parts of the degree + let { + degreeType, + degreeDepartment, + degreeCollege, + degreeYear, + degreeDescription, + } = degreeInfo; + + try { + // Use the helper function to determine the degreeTypeId + let degreeTypeId = await _lookupDegreeTypeId(degreeType); + + // SQL query to update + const query = ` + UPDATE peopleDegree + SET + peopleId = ? + degreeTypeId = ?, + degreeDepartment = ?, + degreeCollege = ?, + degreeYear = ?, + degreeDescription = ? + WHERE peopleDegreeId = ? + `; + + const [results] = await pool.query(query, [ + peopleId, + degreeTypeId, + degreeDepartment, + degreeCollege, + degreeYear, + degreeDescription, + peopleDegreeId, + ]); + + // Check if any rows were affected + if (results.affectedRows === 0) { + console.error("Degree not found. no rows updated"); + return false; + } + + console.log("degree info updated: ", peopleDegreeId); + + // return true to signify success + return true; + } catch (error) { + console.error("error updating degree: ", error); + return false; + } +} + +/* +Function to delete a degree by its ID. +Returns true if the degree was successfully deleted, false otherwise. +*/ +async function deleteDegreeById(peopleDegreeId) { + console.log("Deleting degree with ID: ", peopleDegreeId); + + try { + const query = ` + DELETE FROM peopleDegree + WHERE peopleDegreeId = ? + `; + + const [results] = await pool.query(query, [peopleDegreeId]); + + // Check if any rows were affected + if (results.affectedRows === 0) { + console.error("Degree not found. No rows deleted."); + return false; + } + + console.log("Degree successfully deleted: ", peopleDegreeId); + + return true; + } catch (error) { + console.error("Error deleting degree: ", error); + return false; + } +} + // Private helper function to look up the degreeTypeId async function _lookupDegreeTypeId(degreeType) { try { @@ -67,4 +173,93 @@ async function _lookupDegreeTypeId(degreeType) { } } -module.exports = { addDegree, _lookupDegreeTypeId }; +/* +Function to find all degree IDs associated with a person. +Returns an array of peopleDegreeIds or an empty array if no degrees exist. +*/ +async function getDegreeIdsForPerson(peopleId) { + console.log("Finding degree IDs for person with ID: ", peopleId); + + try { + // get the degrees for a given peopleId + const query = ` + SELECT peopleDegreeId + FROM peopleDegree + WHERE peopleId = ? + `; + + const [results] = await pool.query(query, [peopleId]); + + // Extract the degree IDs + const degreeIds = results.map((row) => row.peopleDegreeId); + + console.log("Found degree IDs: ", degreeIds); + + return degreeIds; + } catch (error) { + console.error("Error finding degree IDs: ", error); + return []; + } +} + +/* +Function to get all degree information associated with a person. +Returns an array of degree objects or an empty array if no degrees exist. +*/ +async function getDegreesForPerson(peopleId) { + console.log("Finding degrees for person with ID: ", peopleId); + + try { + // get the full degree information for degreees for person with peopleId + const query = ` + SELECT + d.peopleDegreeId, + d.degreeDepartment, + d.degreeCollege, + d.degreeYear, + d.degreeDescription, + dt.degreeType + FROM + peopleDegree d + LEFT JOIN + degreeTypeLookup dt ON d.degreeTypeId = dt.degreeTypeId + WHERE + d.peopleId = ?; + `; + + const [results] = await pool.query(query, [peopleId]); + + // Print out each row returned by the query + console.log("Raw results from query:", results); + + if (results.length === 0) { + console.log("No degrees found for person with ID:", peopleId); + return []; + } + + const degrees = results.map((row) => ({ + peopleDegreeId: row.peopleDegreeId, + degreeType: row.degreeType, + degreeDepartment: row.degreeDepartment, + degreeCollege: row.degreeCollege, + degreeYear: row.degreeYear, + degreeDescription: row.degreeDescription, + })); + + console.log("Found degrees: ", degrees); + + return degrees; + } catch (error) { + console.error("Error fetching degrees for person: ", error); + return []; + } +} + +module.exports = { + addDegree, + updateDegree, + deleteDegreeById, + getDegreeIdsForPerson, + getDegreesForPerson, + _lookupDegreeTypeId, +}; diff --git a/backend/repository/peopleRepository.js b/backend/repository/peopleRepository.js index df0c1a3..5805df0 100644 --- a/backend/repository/peopleRepository.js +++ b/backend/repository/peopleRepository.js @@ -84,7 +84,54 @@ async function searchPeopleByName(firstName, lastName) { } } -async function getPersonById() {} +/* +Function to get the data associated with a specific peopleId. +If none is found, the function returns null. +*/ +async function getPersonById(peopleId) { + console.log("searching for person..."); + + try { + // big query to get all of the data + const query = ` + SELECT + peopleId, + firstName, + lastName, + middleName, + maidenName, + suffix, + nickName, + techAlumniChapter, + classYear, + gradYear, + gradSemester, + gender + FROM + people + WHERE + peopleId = ?; + `; + + // Execute the query + const [rows] = await pool.query(query, [peopleId]); + + if (rows.length === 0) { + console.log("No person found"); + null; + } + + // structure the data + console.log("data recieved... structuring now"); + + // create the structure of a person + const person = rows[0]; + return person; + } catch (error) { + console.log("Error getting person's full details: ", error); + return null; + } +} /* Function to delete a person with the peopleId given. @@ -114,4 +161,85 @@ async function deletePersonById(peopleId) { } } -module.exports = { addPerson, searchPeopleByName, deletePersonById }; +/* +Function to update the people table's information for +the person with the given id. +*/ +async function updatePerson(personId, personInfo) { + // extract all the values from person + let { + firstName, + lastName, + middleName, + maidenName, + suffix, + nickName, + techAlumniChapter, + classYear, + gradYear, + gradSemester, + gender, + } = personInfo; + + // find the integer that represents the alumni chapter (for now do 2) + techAlumniChapter = 2; + + try { + console.log("Updating person: ", firstName); + + // SQL UPDATE query + const query = ` + UPDATE people + SET + firstName = ?, + lastName = ?, + middleName = ?, + maidenName = ?, + suffix = ?, + nickName = ?, + techAlumniChapter = ?, + classYear = ?, + gradYear = ?, + gradSemester = ?, + gender = ? + WHERE peopleId = ? + `; + + const [results] = await pool.query(query, [ + firstName, + lastName, + middleName, + maidenName, + suffix, + nickName, + techAlumniChapter, + classYear, + gradYear, + gradSemester, + gender, + personId, + ]); + + // Check if any rows were affected + if (results.affectedRows === 0) { + console.error("Person not found. no rows updated"); + return false; + } + + console.log("person info updated: ", personId); + + // return true to signify success + return true; + } catch (error) { + console.error(error); + return false; + } +} + +module.exports = { + addPerson, + searchPeopleByName, + deletePersonById, + updatePerson, + getPersonById, +}; -- GitLab