Archived
Add edition of user name or user email
This commit is contained in:
@@ -87,4 +87,10 @@ public class AccountController {
|
||||
public UserDTO signin(@RequestBody final UserDTO pUser, final HttpServletResponse pResponse) throws IOException {
|
||||
return accountService.signin(pUser, pResponse);
|
||||
}
|
||||
|
||||
@PutMapping("/")
|
||||
public void update(@RequestBody final UserDTO pUser, final HttpServletRequest pRequest,
|
||||
final HttpServletResponse pResponse) throws IOException {
|
||||
accountService.updateUser(pUser, pRequest, pResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user