X-Press Publishing Step 53: DELETE Series SPOILERS

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 :slight_smile: dont blow a lung laughing at me :slight_smile:

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);
            }
        })
    }
})

You pretty much answered your own question here. If you delete the Series when there are still Issues associated with it, then those records will be orphaned. There will be Issues that are still referencing a series that no longer exists.

Here is where the requirements were in the project’s README.md

  • DELETE
    • Deletes the series with the supplied series ID from the database if that series has no related issues. Returns a 204 response.
    • If the series with the supplied series ID has related issues, returns a 400 response.
    • If a series with the supplied series ID doesn’t exist, returns a 404 response

It isn’t a problem with the Artists because you’ll notice in the requirements that we don’t actually delete them in the DELETE route. We just mark them as unemployed by setting the column is_currently_employed to 0. The record is never deleted, so we can “DELETE” the artist even if they have Series associated with them still.

1 Like