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