refactor: modifyPassword method Optional 반환 -> Exception 처리
Optional 반환과 Exception 반환이 혼용되어 사용되고 있어, Exception 으로 통일
This commit is contained in:
@@ -8,7 +8,6 @@ import com.ticketing.server.user.application.response.UserDeleteResponse;
|
|||||||
import com.ticketing.server.user.application.response.UserModifyPasswordResponse;
|
import com.ticketing.server.user.application.response.UserModifyPasswordResponse;
|
||||||
import com.ticketing.server.user.domain.User;
|
import com.ticketing.server.user.domain.User;
|
||||||
import com.ticketing.server.user.service.UserServiceImpl;
|
import com.ticketing.server.user.service.UserServiceImpl;
|
||||||
import java.util.Optional;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -50,14 +49,8 @@ public class UserController {
|
|||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<User> user = userService.modifyPassword(request.toChangePassword(passwordEncoder));
|
User user = userService.modifyPassword(request.toChangePassword(passwordEncoder));
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).body(UserModifyPasswordResponse.of(user));
|
||||||
if (user.isEmpty()) {
|
|
||||||
log.error("존재하지 않는 이메일 입니다. :: {}", request.getEmail());
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.OK).body(UserModifyPasswordResponse.of(user.get()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,15 +51,15 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public Optional<User> modifyPassword(@Valid ChangePasswordDTO changePassword) {
|
public User modifyPassword(@Valid ChangePasswordDTO changePassword) {
|
||||||
Optional<User> optionalUser = userRepository.findByEmailAndIsDeletedFalse(changePassword.getEmail());
|
Optional<User> optionalUser = userRepository.findByEmailAndIsDeletedFalse(changePassword.getEmail());
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
log.error("존재하지 않는 이메일 입니다. :: {}", changePassword);
|
log.error("존재하지 않는 이메일 입니다. :: {}", changePassword);
|
||||||
return Optional.empty();
|
throw new NotFoundEmailException();
|
||||||
}
|
}
|
||||||
|
|
||||||
User user = optionalUser.get();
|
User user = optionalUser.get();
|
||||||
return Optional.of(user.modifyPassword(changePassword));
|
return user.modifyPassword(changePassword);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import com.ticketing.server.user.domain.User;
|
|||||||
import com.ticketing.server.user.service.dto.ChangePasswordDTO;
|
import com.ticketing.server.user.service.dto.ChangePasswordDTO;
|
||||||
import com.ticketing.server.user.service.dto.DeleteUserDTO;
|
import com.ticketing.server.user.service.dto.DeleteUserDTO;
|
||||||
import com.ticketing.server.user.service.dto.SignUpDTO;
|
import com.ticketing.server.user.service.dto.SignUpDTO;
|
||||||
import java.util.Optional;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
@@ -13,6 +12,6 @@ public interface UserService {
|
|||||||
|
|
||||||
User delete(@Valid DeleteUserDTO deleteUser);
|
User delete(@Valid DeleteUserDTO deleteUser);
|
||||||
|
|
||||||
Optional<User> modifyPassword(@Valid ChangePasswordDTO changePassword);
|
User modifyPassword(@Valid ChangePasswordDTO changePassword);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,10 +102,9 @@ class UserServiceImplTest {
|
|||||||
when(userRepository.findByEmailAndIsDeletedFalse("ticketing@gmail.com")).thenReturn(Optional.empty());
|
when(userRepository.findByEmailAndIsDeletedFalse("ticketing@gmail.com")).thenReturn(Optional.empty());
|
||||||
|
|
||||||
// when
|
// when
|
||||||
Optional<User> user = userService.modifyPassword(changePassword);
|
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(user).isEmpty();
|
assertThatThrownBy(() -> userService.modifyPassword(changePassword))
|
||||||
|
.isInstanceOf(NotFoundEmailException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -115,10 +114,10 @@ class UserServiceImplTest {
|
|||||||
when(userRepository.findByEmailAndIsDeletedFalse("ticketing@gmail.com")).thenReturn(Optional.of(user));
|
when(userRepository.findByEmailAndIsDeletedFalse("ticketing@gmail.com")).thenReturn(Optional.of(user));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
Optional<User> user = userService.modifyPassword(changePassword);
|
User user = userService.modifyPassword(changePassword);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(user).isPresent();
|
assertThat(user).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user