From 8792dcfc2ae14cc948ec615c1fa6c96d6c35270b Mon Sep 17 00:00:00 2001 From: Federico Hurtado <fed_home@Federicos-Mac-mini.local> Date: Tue, 22 Oct 2024 18:28:33 -0400 Subject: [PATCH] Beginning of adding people --- backend/config/database/schema.sql | 13 ++- backend/repository/addressRepository.js | 105 ++++++++++++++++++++ backend/repository/contactRepository.js | 0 backend/repository/degreeRepository.js | 64 ++++++++++++ backend/repository/involvementRepository.js | 0 backend/repository/peopleRepository.js | 53 ++++++++++ backend/routes/peopleRoutes.js | 28 ++++-- backend/service/peopleService.js | 37 +++++++ 8 files changed, 288 insertions(+), 12 deletions(-) create mode 100644 backend/repository/addressRepository.js create mode 100644 backend/repository/contactRepository.js create mode 100644 backend/repository/degreeRepository.js create mode 100644 backend/repository/involvementRepository.js create mode 100644 backend/repository/peopleRepository.js create mode 100644 backend/service/peopleService.js diff --git a/backend/config/database/schema.sql b/backend/config/database/schema.sql index ced00bb..be774a4 100644 --- a/backend/config/database/schema.sql +++ b/backend/config/database/schema.sql @@ -61,6 +61,7 @@ CREATE TABLE IF NOT EXISTS people ( firstName VARCHAR(255) NOT NULL, lastName VARCHAR(255) NOT NULL, middleName VARCHAR(255), + maidenName VARCHAR(255), suffix VARCHAR(32), nickName VARCHAR(255), techAlumniChapter INT, @@ -73,8 +74,9 @@ CREATE TABLE IF NOT EXISTS people ( CREATE TABLE IF NOT EXISTS peopleDegree ( peopleDegreeId INT PRIMARY KEY AUTO_INCREMENT, peopleId INT, - degreeSubject VARCHAR(255), degreeTypeId INT, + degreeDepartment VARCHAR(50), + degreeCollege VARCHAR(50), degreeYear VARCHAR(16), degreeDescription VARCHAR(255), FOREIGN KEY (peopleId) REFERENCES people(peopleId), @@ -116,3 +118,12 @@ CREATE TABLE IF NOT EXISTS peopleXInvolvement ( FOREIGN KEY (peopleId) REFERENCES people(peopleId), FOREIGN KEY (involvementId) REFERENCES involvementLookup(involvementId) ); + +INSERT INTO stateLookup (stateId, name) +VALUES (1, 'Virginia'); + +INSERT INTO countryLookup (countryId, name) +VALUES (1, 'United States'); + +-- I think there is already a people table that exists in our DB and +-- has data that does not include peopoleID \ No newline at end of file diff --git a/backend/repository/addressRepository.js b/backend/repository/addressRepository.js new file mode 100644 index 0000000..d2deaef --- /dev/null +++ b/backend/repository/addressRepository.js @@ -0,0 +1,105 @@ +const { pool } = require("../config/database/database.config"); + +async function addAddress(address, peopleId) { + // get all the parts of the address + let { address1, address2, city, state, country, zipCode, preferredAddress } = + address; + + console.log("adding address..."); + + try { + // Use helper functions to look up stateId and countryId + const stateId = await _lookupStateId(state); + const countryId = await _lookupCountryId(country); + + // query for adding the address + const query = ` + INSERT INTO address (address1, address2, city, stateId, countryId, zipCode) + VALUES (?,?,?,?,?,?) + `; + + // insert data into the address table + const [addressResult] = await pool.query(query, [ + address1, + address2, + city, + stateId, + countryId, + zipCode, + ]); + + // get the id of the newly created address in the DB + const addressId = addressResult.insertId; + + console.log( + "successfully inserted addressID " + + addressId + + " for person: " + + peopleId + ); + + // Now insert into the peopleXAddress table + const peopleAddressQuery = ` + INSERT INTO peopleXAddress (peopleId, addressId, preferredAddress) + VALUES (?, ?, ?) + `; + + const [peopleAddressResult] = await pool.query(peopleAddressQuery, [ + peopleId, + addressId, + preferredAddress, + ]); + + console.log("Added into peopleXaddress"); + + return peopleAddressResult; + } catch (error) { + console.error(error); + } +} + +// Private helper function to look up the stateId +async function _lookupStateId(state) { + // for now return 1 while tables are not populated + return 1; + + // try { + // const lookupQuery = ` + // SELECT stateId FROM stateLookup WHERE name = ? + // `; + // const [lookupResults] = await pool.query(lookupQuery, [state]); + + // if (lookupResults.length === 0) { + // console.log("State not found"); + // return null; + // } + + // return lookupResults[0].stateId; + // } catch (error) { + // console.error(`Error looking up state: ${state}`, error); + // throw error; + // } +} + +// Private helper function to look up the countryId +async function _lookupCountryId(country) { + // for now return 1 while tables are not populated + return 1; + + // try { + // const lookupQuery = ` + // SELECT countryId FROM countryLookup WHERE name = ? + // `; + // const [lookupResults] = await pool.query(lookupQuery, [country]); + // if (lookupResults.length === 0) { + // console.log("Country not found"); + // return null; + // } + // return lookupResults[0].countryId; + // } catch (error) { + // console.error(`Error looking up country: ${country}`, error); + // throw error; + // } +} + +module.exports = { addAddress }; diff --git a/backend/repository/contactRepository.js b/backend/repository/contactRepository.js new file mode 100644 index 0000000..e69de29 diff --git a/backend/repository/degreeRepository.js b/backend/repository/degreeRepository.js new file mode 100644 index 0000000..d36e65f --- /dev/null +++ b/backend/repository/degreeRepository.js @@ -0,0 +1,64 @@ +const { pool } = require("../config/database/database.config"); + +async function addDegree(degree, peopleId) { + // get all the parts of the degree + let { + degreeType, + degreeDepartment, + degreeCollege, + degreeYear, + degreeDescription, + } = degree; + + console.log("adding degree..."); + + try { + // Use the helper function to determine the degreeTypeId + degreeTypeId = await _lookupDegreeTypeId(degreeType); + + const query = ` + INSERT INTO peopleDegree (peopleId, degreeTypeId, degreeDepartment, degreeCollege, degreeYear, degreeDescription) + VALUES (?,?,?,?,?,?) + `; + + // insert data into db + const [results] = await pool.query(query, [ + peopleId, + degreeTypeId, + degreeDepartment, + degreeCollege, + degreeYear, + degreeDescription, + ]); + + return results; + } catch (error) { + console.error(error); + } +} + +// Private helper function to look up the degreeTypeId +async function _lookupDegreeTypeId(degreeType) { + try { + // return null for now + return null; + + // const lookupQuery = ` + // SELECT degreeTypeId FROM degreeTypeLookup WHERE degreeType = ? + // `; + // const [lookupResults] = await pool.query(lookupQuery, [degreeType]); + + // if (lookupResults.length === 0) { + // console.log("Degree type not found"); + // return null; + // // throw new Error(`Invalid degree type: ${degreeType}`); + // } + + // return lookupResults[0].degreeTypeId; + } catch (error) { + console.error(`Error looking up degree type: ${degreeType}`, error); + throw error; + } +} + +module.exports = { addDegree }; diff --git a/backend/repository/involvementRepository.js b/backend/repository/involvementRepository.js new file mode 100644 index 0000000..e69de29 diff --git a/backend/repository/peopleRepository.js b/backend/repository/peopleRepository.js new file mode 100644 index 0000000..680b8fb --- /dev/null +++ b/backend/repository/peopleRepository.js @@ -0,0 +1,53 @@ +const { pool } = require("../config/database/database.config"); + +async function addPerson(person) { + // extract all the values from person + let { + firstName, + lastName, + middleName, + maidenName, + suffix, + nickName, + techAlumniChapter, + classYear, + gradYear, + gradSemester, + gender, + } = person; + + // find the integer that represents the alumni chapter (for now do 2) + techAlumniChapter = 2; + + console.log("In here now"); + + try { + console.log("Adding person: ", firstName); + + const query = ` + INSERT INTO people (firstName, lastName, middleName, maidenName, suffix, nickName, techAlumniChapter, classYear, gradYear, gradSemester, gender) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + `; + + const [results] = await pool.query(query, [ + firstName, + lastName, + middleName, + maidenName, + suffix, + nickName, + techAlumniChapter, + classYear, + gradYear, + gradSemester, + gender, + ]); + + // return the new person's id + return results.insertId; + } catch (error) { + console.error(error); + } +} + +module.exports = { addPerson }; diff --git a/backend/routes/peopleRoutes.js b/backend/routes/peopleRoutes.js index a647ac9..3e707a6 100644 --- a/backend/routes/peopleRoutes.js +++ b/backend/routes/peopleRoutes.js @@ -1,7 +1,7 @@ const { check, body, validationResult } = require("express-validator"); const express = require("express"); const peopleRouter = express.Router(); - +const { createPerson } = require("../service/peopleService"); /* Function to check that the POST request body in create person contains the correct data types @@ -41,18 +41,15 @@ const validateCreatePerson = [ .isIn(["male", "female", "other"]) .withMessage("Invalid gender"), + check("personInfo.maidenName") + .optional() + .isString() + .withMessage("Maiden name should be a string"), + // Validate degrees array body("degrees").isArray().withMessage("Degrees should be an array"), - body("degrees.*.subject") - .notEmpty() - .withMessage("Degree subject is required") - .isString() - .withMessage("Degree subject must be a string"), - - body("degrees.*.degreeType") - .isIn(["AS", "BS", "MS", "PhD"]) - .withMessage("Invalid degree type"), + body("degrees.*.degreeType").isString().withMessage("Invalid degree type"), body("degrees.*.degreeYear") .notEmpty() @@ -112,7 +109,7 @@ const validateCreatePerson = [ .withMessage("Each involvement must be a string"), ]; -peopleRouter.post("/create", validateCreatePerson, (req, res) => { +peopleRouter.post("/create", validateCreatePerson, async (req, res) => { // ensure the request body has the correct data types const errors = validationResult(req); @@ -121,6 +118,15 @@ peopleRouter.post("/create", validateCreatePerson, (req, res) => { return res.status(400).json({ errors: errors.array() }); } + try { + const person = await createPerson(req.body); + console.log("In people routes after add: ", person); + + return res.status(201).json({ message: "person added successfully" }); + } catch (error) { + console.log("error"); + return res.status(500).json({ message: error }); + } // no logic for now, return 200 return res.status(200).json({ message: "No errors" }); }); diff --git a/backend/service/peopleService.js b/backend/service/peopleService.js new file mode 100644 index 0000000..0c610db --- /dev/null +++ b/backend/service/peopleService.js @@ -0,0 +1,37 @@ +const { addAddress } = require("../repository/addressRepository"); +const { addDegree } = require("../repository/degreeRepository"); +const { addPerson } = require("../repository/peopleRepository"); + +async function createPerson(person) { + // extract the parts of a person + const { personInfo, degrees, addresses, contacts, involvements } = person; + + console.log("Adding person...."); + // insert the personInfo into people table + const newPersonID = await addPerson(personInfo); + console.log("Added person ID: ", newPersonID); + + // insert the degree information into the database + for (const degree of degrees) { + const addedDegree = await addDegree(degree, newPersonID); + console.log("added degree: ", degree); + } + + // insert the address information into the database + for (const address of addresses) { + const addedAddress = await addAddress(address, newPersonID); + console.log("added address: ", address); + } + + // insert the contact information into the database + for (const contact of contacts) { + } + + // insert the involvment information into the database + for (const involvment of involvements) { + } + + return newPersonID; +} + +module.exports = { createPerson }; -- GitLab