Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
corps-directory
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fhurtado14
corps-directory
Commits
a851a2c9
Commit
a851a2c9
authored
5 months ago
by
Federico Hurtado
Browse files
Options
Downloads
Patches
Plain Diff
functionality for search people by first and last name
parent
b3d8b039
No related branches found
Branches containing commit
No related tags found
1 merge request
!3
functionality for search people by first and last name
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
backend/repository/peopleRepository.js
+34
-1
34 additions, 1 deletion
backend/repository/peopleRepository.js
backend/routes/peopleRoutes.js
+34
-1
34 additions, 1 deletion
backend/routes/peopleRoutes.js
backend/service/peopleService.js
+19
-3
19 additions, 3 deletions
backend/service/peopleService.js
with
87 additions
and
5 deletions
backend/repository/peopleRepository.js
+
34
−
1
View file @
a851a2c9
...
...
@@ -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
};
This diff is collapsed.
Click to expand it.
backend/routes/peopleRoutes.js
+
34
−
1
View file @
a851a2c9
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
;
This diff is collapsed.
Click to expand it.
backend/service/peopleService.js
+
19
−
3
View file @
a851a2c9
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
};
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment