Refactor the Controller
Learn and practice refactoring our controller to remove repeated blocks of code.
We'll cover the following...
Refactor the recipes
controller
Currently, the get()
, update()
, and remove()
route handlers repeat the following chunk of logic to check that a recipe exists in the database and to throw an error if the recipe doesn’t exist:
Press + to interact
const recipe = await service.get(req.params.id);if (recipe === undefined) {const err = new Error("Recipe not found");err.statusCode = 404;throw err;}
To avoid duplicating the logic across controllers, we can move the validation logic ...