▸ 오늘의 코드카타
▸ double과 int
▸ 커스텀 어노테이션 Argument Resolver
▸ 내 프로필 조회 기능 구현
▸ 프로필 수정 기능 구현
▸ 비밀번호 수정 기능 구현
2024년 2월 26일 - [프로그래머스 - 자바(JAVA)] 30 : 의상 | 기능개발
double result = (int)Math.ceil((100-30)/30); System.out.println("결과: " + result); // 출력: 2.0
double result = (int)Math.ceil((100-30)/(double)30); System.out.println("결과: " + result); // 출력: 3.0
@Component
@RequiredArgsConstructor
public class UserInfoArgumentResolver implements HandlerMethodArgumentResolver {
private final JwtUtil jwtUtil;
private final UserRepository userRepository;
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(UserInfo.class)
&& User.class.isAssignableFrom(parameter.getParameterType());
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
String tokenValue = jwtUtil.getJwtFromRequest(request);
String token = jwtUtil.substringToken(tokenValue);
String userInfo = jwtUtil.getUserInfoFromToken(token);
return userRepository.userBy(userInfo);
}
}
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface UserInfo {
}
public ResponseEntity<ResponseDto<ProfileResponseDto>> getProfile(@UserInfo User user) {
@Transactional
public ProfileResponseDto updateProfile(ProfileRequsetDto requsetDto, User user) {
user.update(requsetDto);
userRepository.update(user);
return user.profileResponseDto();
}
@Transactional
public ProfileResponseDto updateProfile(ProfileRequsetDto requsetDto, User user) {
user.update(requsetDto);
userRepository.update(user);
return user.profileResponseDto();
}
@Getter
public class ProfileRequsetDto {
private String nickname;
private String photo;
}
public void update(ProfileRequsetDto requsetDto) {
this.nickname = requsetDto.getNickname();
this.photo = requsetDto.getPhoto();
}
public ProfileResponseDto profileResponseDto() {
return new ProfileResponseDto(nickname, email, photo);
}
@PostMapping("/api/users/change-password")
@Operation(summary = "비밀번호 변경 API")
public ResponseEntity<ResponseDto<ProfileResponseDto>> updatePassword(
@RequestBody @Valid ChangePasswordRequestDto requestDto, @UserInfo User user) {
return ResponseEntity.ok()
.body(ResponseDto.<ProfileResponseDto>builder()
.message("비밀번호 변경 성공")
.data(userService.updatePassword(requestDto, user))
.build());
}
@Getter
public class ChangePasswordRequestDto {
@NotBlank(message = "현재 비밀번호를 입력하세요.")
private String existingPassword;
@NotBlank(message = "변경하고자 하는 비밀번호를 입력하세요.")
private String newPassword;
}
@Transactional
public ProfileResponseDto updatePassword(ChangePasswordRequestDto requestDto, User user) {
String existingPassword = requestDto.getExistingPassword();
String newPassword = requestDto.getNewPassword();
user.validatePassword(existingPassword, passwordEncoder);
user.updatePassword(passwordEncoder.encode(newPassword));
userRepository.update(user);
return user.profileResponseDto();
}
public void updatePassword(String newPassword) {
this.password = newPassword;
}