diff --git a/backend/config/database/schema.sql b/backend/config/database/schema.sql index ced00bb96f27281da7f54017d8d46334ee6a5672..be774a469ce2b6829d16f9adb58213caccbeb4cd 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 0000000000000000000000000000000000000000..d2deaef6b4638dfcddfa2dc11fa134b9d66565d4 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/repository/degreeRepository.js b/backend/repository/degreeRepository.js new file mode 100644 index 0000000000000000000000000000000000000000..d36e65f0cee7a1c9a114f1fcf92abc46d858f488 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/repository/peopleRepository.js b/backend/repository/peopleRepository.js new file mode 100644 index 0000000000000000000000000000000000000000..680b8fb3ae862c46fa5440d81323766a83d43480 --- /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 a647ac941f5bf73b4197cd502e6e6d50d0a2fdfa..3e707a6bddd3f55bb42afcdbf7c1afbda991cf8c 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 0000000000000000000000000000000000000000..0c610db40e1e53609038f1ba4ca8bd493474073f --- /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 }; diff --git a/frontend/README.md b/frontend/README.md index 9a491f054731200ec2f04f40825312048b567830..f262d0961ea3dfbbfcd30856b17d29a60baa2e8e 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -15,8 +15,7 @@ } degrees: [ { - subject: string (not null), - degreeType: string (accepted values: "AS", "BS", "MS", "PhD") (need to add more of these) --> make a dropdown menu to choose from in front end for this + degreeType: string (accepted values: "AS", "BS", "BA", "MS", "PhD", ....) (need to add more of these) --> make a dropdown menu to choose from in front end for this degreeYear: int (not null), degreeDescription: string (max 255 chars) degreeDepartment: string,