Skip to content
Snippets Groups Projects
Commit 14ae1361 authored by fhurtado14's avatar fhurtado14
Browse files

Merge branch 'feature/delete-person-by-id' into 'main'

Able to delete a person and their associated data by their id.

See merge request !4
parents 7d8fb87c b08acef3
No related branches found
No related tags found
1 merge request!4Able to delete a person and their associated data by their id.
......@@ -79,7 +79,7 @@ CREATE TABLE IF NOT EXISTS peopleDegree (
degreeCollege VARCHAR(50),
degreeYear VARCHAR(16),
degreeDescription VARCHAR(255),
FOREIGN KEY (peopleId) REFERENCES people(peopleId),
FOREIGN KEY (peopleId) REFERENCES people(peopleId) ON DELETE CASCADE,
FOREIGN KEY (degreeTypeId) REFERENCES degreeTypeLookup(degreeTypeId)
);
......@@ -92,7 +92,7 @@ CREATE TABLE IF NOT EXISTS peopleXAddress (
peopleId INT PRIMARY KEY,
addressId INT UNIQUE,
preferredAddress VARCHAR(255) NOT NULL,
FOREIGN KEY (peopleId) REFERENCES people(peopleId),
FOREIGN KEY (peopleId) REFERENCES people(peopleId) ON DELETE CASCADE,
FOREIGN KEY (addressId) REFERENCES address(addressId)
);
......@@ -102,7 +102,7 @@ CREATE TABLE IF NOT EXISTS peopleContact (
contactNumber VARCHAR(255) NOT NULL,
contactType VARCHAR(50) NOT NULL,
preferredContact BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (peopleId) REFERENCES people(peopleId)
FOREIGN KEY (peopleId) REFERENCES people(peopleId) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS involvementLookup (
......@@ -115,7 +115,7 @@ CREATE TABLE IF NOT EXISTS peopleXInvolvement (
peopleId INT,
involvementId INT,
PRIMARY KEY (peopleId, involvementId),
FOREIGN KEY (peopleId) REFERENCES people(peopleId),
FOREIGN KEY (peopleId) REFERENCES people(peopleId) ON DELETE CASCADE,
FOREIGN KEY (involvementId) REFERENCES involvementLookup(involvementId)
);
......
......@@ -86,6 +86,32 @@ async function searchPeopleByName(firstName, lastName) {
async function getPersonById() {}
async function deletePersonById() {}
/*
Function to delete a person with the peopleId given.
Since the SQL schema has ON DELETE CASCADE, all associated data
(address, contacts, etc). will also be deleted. Function returns
true if the delete was successful and false if not.
*/
async function deletePersonById(peopleId) {
console.log("deleting person by id....");
try {
// execute the query to delete by primary key
const query = `DELETE FROM people WHERE peopleId = ?`;
const [results] = await pool.query(query, [peopleId]);
// check if anything was deleted
if (results.affectedRows === 0) {
console.log("no rows deleted");
return false; // no rows deleted
}
console.log("row deleted");
return true;
} catch (error) {
console.error(error);
return false;
}
}
module.exports = { addPerson, searchPeopleByName };
module.exports = { addPerson, searchPeopleByName, deletePersonById };
const { check, body, validationResult } = require("express-validator");
const express = require("express");
const peopleRouter = express.Router();
const { createPerson, findByName } = require("../service/peopleService");
const {
createPerson,
findByName,
deleteById,
} = require("../service/peopleService");
/*
Function to check that the POST request body in
create person contains the correct data types
......@@ -164,4 +168,36 @@ peopleRouter.post("/searchByName", async (req, res) => {
}
});
// DELETE endpoint to delete a person by their ID
peopleRouter.delete("/:id", async (req, res) => {
const personId = parseInt(req.params.id, 10); // Convert to an integer
// Check if personId is a valid integer
if (!Number.isInteger(personId)) {
return res
.status(400)
.json({ message: "Invalid ID. ID must be an integer." });
}
try {
// Call the function to delete the person
const success = await deleteById(personId);
if (!success) {
// If no person is deleted, return a 404 status
return res
.status(404)
.json({ message: "Person not found or could not be deleted." });
}
// TODO: log database change here
// Return a success message
res.json({ message: "Person successfully deleted." });
} catch (error) {
console.error("Error in DELETE /person/:id", error);
res.status(500).json({ message: "Internal server error." });
}
});
module.exports = peopleRouter;
......@@ -4,6 +4,7 @@ const { addDegree } = require("../repository/degreeRepository");
const {
addPerson,
searchPeopleByName,
deletePersonById,
} = require("../repository/peopleRepository");
/*
......@@ -56,4 +57,17 @@ async function findByName(firstName, lastName) {
return peopleFound;
}
module.exports = { createPerson, findByName };
/*
Function to delete person by their id.
*/
async function deleteById(peopleId) {
const personDeleted = await deletePersonById(peopleId);
if (personDeleted) {
return true;
}
return false;
}
module.exports = { createPerson, findByName, deleteById };
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