Last active
December 23, 2017 18:24
-
-
Save vedranjukic/61be79ac15bf79440e91672a0494c2f4 to your computer and use it in GitHub Desktop.
Coupled infrastructure and core logic example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Express controller route example of | |
// coupled infrastructure with core logic | |
// | |
router.put('/user', async (req, res) => { | |
// db object is already added to req object when express is initialised | |
const db = req.db | |
const { userId, userName, userEmail } = req.body | |
if (!userId || !userName || !userEmail) { | |
throw new InvalidParamsException('Missing required params') | |
} | |
if (!isValidUserName(userName)) { | |
throw new InvalidParamsException('Invalid username') | |
} | |
if (!isValidEmail(userEmail)) { | |
throw new InvalidParamsException('Invalid email') | |
} | |
const users = db.get('users') | |
const user = await users.findOne({ | |
userId | |
}) | |
if (!user) { | |
throw new UserNotFoundException('User not found') | |
} | |
const updatedUser = { | |
...user, | |
userName, | |
userEmail | |
} | |
await users.update({ | |
userId | |
}, updatedUser) | |
res.send('User updated') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment