Skip to content

Instantly share code, notes, and snippets.

@vedranjukic
Last active December 23, 2017 18:47
Show Gist options
  • Save vedranjukic/59d2d6b5e45eb7ee7d2517ffbb18a614 to your computer and use it in GitHub Desktop.
Save vedranjukic/59d2d6b5e45eb7ee7d2517ffbb18a614 to your computer and use it in GitHub Desktop.
Implemented UserRepository interface
//
// UserRepository interface implemented instead of
// directly interfacing mongo db client
//
async function updateUser ({userRepository, updateParams}) {
const { userId, userName, userEmail } = updateParams
// validate params
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 user = await userRepository.getById(userId)
// no need to validate does user exist
// because userRepository will throw UserNotFoundException
// if there is no user to return
const updatedUser = {
...user,
userName,
userEmail
}
await userRepository.save(updatedUser)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment