Hello, my question comes from:
https://www.codecademy.com/paths/web-development/tracks/building-a-persistent-api/modules/persistent-api-cumulative-projects/projects/x-press-publishing
And my question is about step 53 which is deleting a Series row based on param :seriesId.
Here is the code supplied by codecademy
seriesRouter.delete('/:seriesId', (req, res, next) => {
const issueSql = 'SELECT * FROM Issue WHERE Issue.series_id = $seriesId';
const issueValues = {$seriesId: req.params.seriesId};
SeriesDB.get(issueSql, issueValues, (err, row) => {
if (err) {
next(err);
} else if (row) {
res.sendStatus(400);
} else {
const deleteSql = `DELETE FROM Series WHERE Series.id = $seriesId`;
const seriesValues = {$seriesId: req.params.seriesId};
SeriesDB.run(`DELETE FROM Series WHERE Series.id = ${req.params.seriesId}`, (err) => {
if (err) {
console.log("Could not delete from Series");
next(err);
} else {
res.sendStatus(204);
}
})
}
});
})
Can someone help explain why we need to access the Issue Table, rather than just deleting the Series?
Is it because Issues is “nested” within the Series and Artists table?
That is to say: there should be no existing issues that have a artist_id / series_id column that matches with any IDs of the Artist and Series table
I had to watch the video for this last part as I was unsure why my code would not pass MOCHA tests. This reminds me alot of C++ where we’d have to check nested children values first to then delete the parent value.
Also if you’d like to laugh at a newbie here is my original code dont blow a lung laughing at me
seriesRouter.delete('/:seriesId', (req, res, next) => {
let serie = req.body.series;
let name = serie.name;
let description = serie.description;
//console.log(req.params.seriesId);
if (!name || !description) {
console.log("The updating body does not have all required fields. Failed Series DELETE.");
return res.sendStatus(400);
} else {
SeriesDB.run(`DELETE FROM Series WHERE Series.id = ${seriesId}`, (err) => {
if (err) {
console.log("Could not delete from Series");
res.sendStatus(204);
next(err);
}
})
}
})