Skip to content

Commit

Permalink
유저 프로필 데이터 업데이트 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
T-ferret committed Sep 28, 2024
1 parent 093c4ee commit 00ac22b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/main/java/com/goormdari/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,34 @@ public User(String nickname, String username, String password, String role, Stri
this.role = role;
this.profileUrl = profileUrl;
}

public void updateNickname(String nickname) {
if (nickname != null && !nickname.isEmpty()) {
this.nickname = nickname;
}
}

public void updateProfileUrl(String profileUrl) {
if (profileUrl != null && !profileUrl.isEmpty()) {
this.profileUrl = profileUrl;
}
}

public void updateUsername(String username) {
if (username != null && !username.isEmpty()) {
this.username = username;
}
}

public void updatePassword(String encodedPassword) {
if (encodedPassword != null && !encodedPassword.isEmpty()) {
this.password = encodedPassword;
}
}

public void updateEmail(String email) {
if (email != null && !email.isEmpty()) {
this.email = email;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.goormdari.domain.user.domain.dto.request;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class UpdateUserRequest {
private String nickname;
private String username;
private String password;
private String profileUrl;
private String email;
private String currentPassword;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.goormdari.domain.user.domain.exception;

public class InvalidPasswordException extends RuntimeException {
public InvalidPasswordException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.goormdari.domain.user.domain.service;

import com.amazonaws.services.kms.model.NotFoundException;
import com.goormdari.domain.user.domain.exception.InvalidPasswordException;
import com.goormdari.domain.user.domain.dto.request.UpdateUserRequest;
import com.goormdari.domain.user.domain.dto.response.UserInfoResponse;
import com.goormdari.domain.user.domain.dto.response.findCurrentStepResponse;
import com.goormdari.domain.user.domain.User;
import com.goormdari.domain.user.domain.DefaultProfileUrl;
import com.goormdari.domain.user.domain.dto.request.AddUserRequest;
import com.goormdari.domain.user.domain.dto.request.LoginRequest;
import com.goormdari.domain.user.domain.dto.response.JwtResponse;
import com.goormdari.domain.user.domain.dto.response.findCurrentStepResponse;
import com.goormdari.domain.user.domain.repository.UserRepository;
import com.goormdari.global.config.security.jwt.JWTUtil;

Expand Down Expand Up @@ -101,4 +102,50 @@ public UserInfoResponse getUserInfo(Long userId) {
.build();

}

public boolean verifyCurrentPassword(User user, String rawPassword) {
return passwordEncoder.matches(rawPassword, user.getPassword());
}

@Transactional
public UserInfoResponse updateUserProfile(Long userId, UpdateUserRequest updateUserRequest) {
User user = userRepository.findById(userId).orElseThrow(()->new NotFoundException("User Not Found: " + userId));

// 닉네임 업데이트
if (updateUserRequest.getNickname() != null && !updateUserRequest.getNickname().isEmpty()) {
user.updateNickname(updateUserRequest.getNickname());
}

// 유저네임 업데이트 (중복 체크 필요)
if (updateUserRequest.getUsername() != null && !updateUserRequest.getUsername().isEmpty()) {
if (userRepository.findByUsername(updateUserRequest.getUsername()).isPresent() &&
!user.getUsername().equals(updateUserRequest.getUsername())) {
throw new IllegalArgumentException("Username is already exists.");
}
user.updateUsername(updateUserRequest.getUsername());
}

// 비밀번호 업데이트 (인코딩 필요)
if (updateUserRequest.getPassword() != null && !updateUserRequest.getPassword().isEmpty()) {
// 현재 비밀번호 검증
if (updateUserRequest.getCurrentPassword() == null || !verifyCurrentPassword(user, updateUserRequest.getCurrentPassword())) {
throw new InvalidPasswordException("Current password is incorrect.");
}
user.updatePassword(passwordEncoder.encode(updateUserRequest.getPassword()));
}

// 프로필 URL 업데이트
if (updateUserRequest.getProfileUrl() != null && !updateUserRequest.getProfileUrl().isEmpty()) {
user.updateProfileUrl(updateUserRequest.getProfileUrl());
}

// 프로필 이메일 업데이트
if (updateUserRequest.getEmail() != null && !updateUserRequest.getEmail().isEmpty()) {
user.updateEmail(updateUserRequest.getEmail());
}

userRepository.save(user);

return getUserInfo(userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goormdari.domain.user.presentation;

import com.goormdari.domain.calendar.exception.InvalidTokenException;
import com.goormdari.domain.user.domain.dto.request.UpdateUserRequest;
import com.goormdari.domain.user.domain.dto.response.UserInfoResponse;
import com.goormdari.domain.user.domain.service.UserService;
import com.goormdari.domain.user.domain.dto.response.findCurrentStepResponse;
Expand All @@ -14,6 +15,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -64,4 +66,21 @@ public UserInfoResponse getCurrentUserInfo(@Parameter(description = "Accesstoken
return userService.getUserInfo(userId);
}

@Operation(summary = "현재 유저 프로필 업데이트", description = "유저의 nickname, username, password(지난 비밀번호 검증 과정 존재) email, profileImageUrl 업데이트 기능(null 값으로 전송 시, 업데이트 X)")
@PostMapping
public UserInfoResponse updateCurrentUserInfo(@Parameter(description = "Accesstoken을 입력해주세요.", required = true) @RequestHeader("Authorization") String token,
@Valid @RequestBody UpdateUserRequest updateUserRequest) {
if (token == null) {
throw new InvalidTokenException();
}

String jwt = token.startsWith("Bearer ") ? token.substring(7) : token;
if (!jwtUtil.validateToken(jwt)) {
throw new IllegalArgumentException("Invalid token");
}
Long userId = jwtUtil.extractId(jwt);

return userService.updateUserProfile(userId, updateUserRequest);
}

}

0 comments on commit 00ac22b

Please sign in to comment.