Add edition of user name or user email

This commit is contained in:
Florian
2018-06-07 21:05:36 +02:00
parent b46dcb1bda
commit 8a10fa0451
7 changed files with 213 additions and 6 deletions
@@ -140,4 +140,32 @@ public class AccountService {
return new UserDTO(user);
}
public void updateUser(final UserDTO pUser, final HttpServletRequest pRequest,
final HttpServletResponse pResponse) throws IOException {
final Optional<User> connectedUserOpt = tokenService.getAuthenticatedUserByToken(pRequest);
if(connectedUserOpt.isPresent()) {
final User connectedUser = connectedUserOpt.get();
final Optional<User> userFromDb = userRepository.findByEmail(pUser.getEmail());
/*
* If a user is returned by the repository, that's the email adress is used, but
* if it is not the same as the connected user, that's the email adress
* corresponds to another user. So a 409 error is sent. Otherwise, if no user is
* returned by the repository, that's the email adress is free to be used. So,
* user can change him email adress.
*/
if(userFromDb.isPresent() && !connectedUser.getEmail().equals(userFromDb.get().getEmail())) {
pResponse.sendError(HttpServletResponse.SC_CONFLICT);
} else {
connectedUser.setName(pUser.getName());
connectedUser.setEmail(pUser.getEmail());
userRepository.save(connectedUser);
}
} else {
pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}
}