Skip to content
Snippets Groups Projects
Commit 7d8fb87c authored by fhurtado14's avatar fhurtado14
Browse files

Merge branch 'find-by-name' into 'main'

functionality for search people by first and last name

See merge request !3
parents b3d8b039 a851a2c9
No related branches found
No related tags found
1 merge request!3functionality for search people by first and last name
......@@ -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 };
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;
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 };
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