use of com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException in project 2021-msk-food-delivery by netcracker-edu.
the class ClientServiceImpl method changeClientInfo.
@Override
public boolean changeClientInfo(Long id, UserChangeInfoDTO newUserInfo) {
Client client = getClientById(id);
String newFullName = newUserInfo.getFullName();
if (newFullName != null) {
User user = client.getUser();
user.setFullName(newFullName);
client.setUser(user);
}
String newPhoneNumber = newUserInfo.getPhoneNumber();
if (newPhoneNumber != null) {
Client clientWithNewNumber = clientRepo.findByPhoneNumber(newPhoneNumber);
if (clientWithNewNumber != null) {
throw new AlreadyExistsException(newPhoneNumber);
}
client.setPhoneNumber(newPhoneNumber);
}
clientRepo.save(client);
return true;
}
use of com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceImpl method changeEmail.
@Override
public boolean changeEmail(User user, EmailChangeDTO newEmailInfo) {
String newUserEmail = newEmailInfo.getEmail();
User userWithNewEmail = userRepo.findByEmail(newUserEmail);
// user with new email also exist throw exception!
if (userWithNewEmail != null) {
throw new AlreadyExistsException(newUserEmail);
}
String inputPassword = newEmailInfo.getPassword();
String userEncodedPassword = user.getPassword();
boolean isPasswordsSame = encoder.matches(inputPassword, userEncodedPassword);
if (!isPasswordsSame) {
throw new PasswordsMismatchException();
}
user.setEmail(newUserEmail);
userRepo.save(user);
return true;
}
use of com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceTest method changeEmailAlreadyExists.
@Test
public void changeEmailAlreadyExists() {
Long userId = 1L;
User penny = UserUtils.clientPennyTeller(userId);
Long anotherId = 2L;
User leonard = UserUtils.moderatorLeonardHofstadter(anotherId);
String newPennyEmail = leonard.getEmail();
when(userRepoMock.findByEmail(newPennyEmail)).thenReturn(leonard);
String password = "password";
EmailChangeDTO changeDTO = new EmailChangeDTO();
changeDTO.setEmail(newPennyEmail);
changeDTO.setPassword(password);
Exception exception = assertThrows(AlreadyExistsException.class, () -> {
userService.changeEmail(penny, changeDTO);
});
String resultMessage = exception.getMessage();
String perfectMessage = new AlreadyExistsException(newPennyEmail).getMessage();
assertEquals(perfectMessage, resultMessage);
verify(userRepoMock, times(1)).findByEmail(newPennyEmail);
verify(userRepoMock, never()).save(any(User.class));
}
Aggregations