Skip to content

Instantly share code, notes, and snippets.

@vedranjukic
Last active December 23, 2017 18:24
Show Gist options
  • Save vedranjukic/61be79ac15bf79440e91672a0494c2f4 to your computer and use it in GitHub Desktop.
Save vedranjukic/61be79ac15bf79440e91672a0494c2f4 to your computer and use it in GitHub Desktop.
Coupled infrastructure and core logic example
//
// 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