diff --git a/backend/repository/peopleRepository.js b/backend/repository/peopleRepository.js index 5191ddcaf92af031524019f98f246ea9823a9d81..e530f051d99bd399a0a7ff86c67dbc1a1a86f7db 100644 --- a/backend/repository/peopleRepository.js +++ b/backend/repository/peopleRepository.js @@ -51,8 +51,41 @@ async function addPerson(person) { } } +/* +Function to search for people by their first and last name, +if multiple people with that name exist, they will all be returned. +If no people are found, the function will return null. +*/ +async function searchPeopleByName(firstName, lastName) { + console.log("searching for people by name...."); + + try { + // SQL query to search for people by first and last name + const query = ` + SELECT * FROM people + WHERE firstName = ? AND lastName = ? + `; + + // Execute the query and pass the first and last name as parameters + const [results] = await pool.query(query, [firstName, lastName]); + + console.log("results found: ", results); + + // If no people are found, return null + if (results.length === 0) { + return null; + } + + // return the people found + return results; + } catch (error) { + console.error(error); + return null; + } +} + async function getPersonById() {} async function deletePersonById() {} -module.exports = { addPerson }; +module.exports = { addPerson, searchPeopleByName }; diff --git a/backend/routes/peopleRoutes.js b/backend/routes/peopleRoutes.js index fe7ff6ba64ae75ce24f8a2be476c79e823e96e5a..7cf5d78e5014f9eff2962d7b278ebce965e987fb 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"); +const { createPerson, findByName } = require("../service/peopleService"); /* Function to check that the POST request body in create person contains the correct data types @@ -131,4 +131,37 @@ peopleRouter.post("/create", validateCreatePerson, async (req, res) => { } }); +/* +Route to search for people by first and last name. Request body takes +firstName and lastName as strings. +*/ +peopleRouter.post("/searchByName", async (req, res) => { + // get the first and last name + const { firstName, lastName } = req.body; + + // Basic validation to ensure firstName and lastName are provided + if (!firstName || !lastName) { + return res + .status(400) + .json({ message: "First name and last name are required." }); + } + + try { + // run function to get person by name + const people = await findByName(firstName, lastName); + + // no person found + if (people == null) { + return res.status(404).json({ message: "person not found" }); + } + + // return the found people + return res.status(200).json({ people: people }); + } catch (error) { + // error + console.log("error"); + return res.status(500).json({ message: error }); + } +}); + module.exports = peopleRouter; diff --git a/backend/service/peopleService.js b/backend/service/peopleService.js index e196f5c1cba4583fde3cd9e8ec24d44cb338ece1..a5f813e358628bd376a1d7e8f2eccb32eb5a04eb 100644 --- a/backend/service/peopleService.js +++ b/backend/service/peopleService.js @@ -1,8 +1,14 @@ const { addAddress } = require("../repository/addressRepository"); const { addContact } = require("../repository/contactRepository"); const { addDegree } = require("../repository/degreeRepository"); -const { addPerson } = require("../repository/peopleRepository"); - +const { + addPerson, + searchPeopleByName, +} = require("../repository/peopleRepository"); + +/* +Function to handle the logic for creating a person. +*/ async function createPerson(person) { // extract the parts of a person const { personInfo, degrees, addresses, contacts, involvements } = person; @@ -40,4 +46,14 @@ async function createPerson(person) { return newPersonID; } -module.exports = { createPerson }; +/* +Function to call repository and get people with a +gicen first and last name. +*/ +async function findByName(firstName, lastName) { + // call repository function, null will be returned if no people are found + const peopleFound = await searchPeopleByName(firstName, lastName); + return peopleFound; +} + +module.exports = { createPerson, findByName };