Search in sources :

Example 1 with AlreadyExistsException

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;
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) AlreadyExistsException(com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException) Client(com.ncedu.fooddelivery.api.v1.entities.Client)

Example 2 with AlreadyExistsException

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;
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) AlreadyExistsException(com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException) PasswordsMismatchException(com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)

Example 3 with AlreadyExistsException

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));
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) AlreadyExistsException(com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException) EmailChangeDTO(com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO) AlreadyExistsException(com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException) PasswordsMismatchException(com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.ncedu.fooddelivery.api.v1.entities.User)3 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)3 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)2 EmailChangeDTO (com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO)1 Client (com.ncedu.fooddelivery.api.v1.entities.Client)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1