Last active
December 23, 2017 18:47
-
-
Save vedranjukic/59d2d6b5e45eb7ee7d2517ffbb18a614 to your computer and use it in GitHub Desktop.
Implemented UserRepository interface
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
// | |
// 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