Skip to content

Commit 2e6a327

Browse files
authored
Merge pull request #220 from prgrms-web-devcourse-final-project/fix/EA3-180-member-profile-update
[EA3-180] Fix: 닉네임 변경사항이 존재할때만 수정하도록 변경
2 parents 09961aa + 39ef596 commit 2e6a327

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/main/java/grep/neogulcoder/domain/users/service/UserService.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import grep.neogulcoder.global.utils.upload.FileUsageType;
2424
import grep.neogulcoder.global.utils.upload.uploader.GcpFileUploader;
2525
import grep.neogulcoder.global.utils.upload.uploader.LocalFileUploader;
26-
import jakarta.transaction.Transactional;
2726
import java.io.IOException;
2827
import java.util.List;
2928
import lombok.RequiredArgsConstructor;
3029
import org.springframework.beans.factory.annotation.Autowired;
3130
import org.springframework.core.env.Environment;
3231
import org.springframework.security.crypto.password.PasswordEncoder;
3332
import org.springframework.stereotype.Service;
33+
import org.springframework.transaction.annotation.Transactional;
3434
import org.springframework.web.multipart.MultipartFile;
3535

3636
@Transactional
@@ -58,14 +58,14 @@ public class UserService {
5858
private Environment environment;
5959

6060

61+
@Transactional(readOnly = true)
6162
public User get(Long id) {
62-
User user = findUser(id);
63-
return user;
63+
return findUserById(id);
6464
}
6565

66+
@Transactional(readOnly = true)
6667
public User getByEmail(String email) {
67-
User user = findUser(email);
68-
return user;
68+
return findUserByEmail(email);
6969
}
7070

7171
public void signUp(SignUpRequest request) {
@@ -84,7 +84,7 @@ public void signUp(SignUpRequest request) {
8484
userRepository.save(
8585
User.UserInit(request.getEmail(), encodedPassword, request.getNickname()));
8686

87-
User user = findUser(request.getEmail());
87+
User user = findUserByEmail(request.getEmail());
8888
initializeUserData(user.getId());
8989

9090
verificationService.clearVerifiedStatus(request.getEmail());
@@ -93,10 +93,9 @@ public void signUp(SignUpRequest request) {
9393
public void updateProfile(Long userId, String nickname, MultipartFile profileImage)
9494
throws IOException {
9595

96-
User user = findUser(userId);
97-
if(isDuplicateNickname(nickname)){
98-
throw new NicknameDuplicatedException(UserErrorCode.IS_DUPLICATED_NICKNAME);
99-
}
96+
User user = findUserById(userId);
97+
98+
String validatedNickname = validateUpdateNickname(user, nickname);
10099

101100
String uploadedImageUrl;
102101
if (isProfileImgExists(profileImage)) {
@@ -108,12 +107,12 @@ public void updateProfile(Long userId, String nickname, MultipartFile profileIma
108107
uploadedImageUrl = user.getProfileImageUrl();
109108
}
110109

111-
user.updateProfile(nickname, uploadedImageUrl);
110+
user.updateProfile(validatedNickname, uploadedImageUrl);
112111
}
113112

114113
public void updatePassword(Long id, String password, String newPassword,
115114
String newPasswordCheck) {
116-
User user = findUser(id);
115+
User user = findUserById(id);
117116

118117
if (isNotMatchCurrentPassword(password, user.getPassword())) {
119118
throw new PasswordNotMatchException(UserErrorCode.PASSWORD_MISMATCH);
@@ -128,7 +127,7 @@ public void updatePassword(Long id, String password, String newPassword,
128127
}
129128

130129
public void deleteUser(Long userId, String password) {
131-
User user = findUser(userId);
130+
User user = findUserById(userId);
132131

133132
if (isNotMatchCurrentPassword(password, user.getPassword())) {
134133
throw new PasswordNotMatchException(UserErrorCode.PASSWORD_MISMATCH);
@@ -142,7 +141,7 @@ public void deleteUser(Long userId, String password) {
142141
}
143142

144143
public void deleteUser(Long userId) {
145-
User user = findUser(userId);
144+
User user = findUserById(userId);
146145

147146
prTemplateService.deleteByUserId(user.getId());
148147
linkService.deleteByUserId(userId);
@@ -157,6 +156,7 @@ public void initializeUserData(Long userId) {
157156
buddyEnergyService.createDefaultEnergy(userId);
158157
}
159158

159+
@Transactional(readOnly = true)
160160
public UserResponse getUserResponse(Long userId) {
161161
User user = get(userId);
162162
return UserResponse.toUserResponse(
@@ -168,6 +168,7 @@ public UserResponse getUserResponse(Long userId) {
168168
user.getRole());
169169
}
170170

171+
@Transactional(readOnly = true)
171172
public List<AllUserResponse> getAllUsers() {
172173
return userRepository.findAllByActivatedTrue().stream()
173174
.map(user -> AllUserResponse.builder()
@@ -180,13 +181,26 @@ public List<AllUserResponse> getAllUsers() {
180181
.toList();
181182
}
182183

183-
private User findUser(Long id) {
184+
private String validateUpdateNickname(User user, String nickname) {
185+
if (isChangedNickname(nickname)) {
186+
nickNameDuplicationCheck(nickname);
187+
} else {
188+
nickname = user.getNickname();
189+
}
190+
return nickname;
191+
}
192+
193+
private boolean isChangedNickname(String nickname){
194+
return nickname != null && !nickname.isBlank();
195+
}
196+
197+
private User findUserById(Long id) {
184198
return userRepository.findById(id)
185199
.orElseThrow(
186200
() -> new UserNotFoundException(UserErrorCode.USER_NOT_FOUND));
187201
}
188202

189-
private User findUser(String email) {
203+
private User findUserByEmail(String email) {
190204
return userRepository.findByEmail(email)
191205
.orElseThrow(
192206
() -> new UserNotFoundException(UserErrorCode.USER_NOT_FOUND));
@@ -202,6 +216,12 @@ private void duplicationCheck(String email, String nickname) {
202216
}
203217
}
204218

219+
private void nickNameDuplicationCheck(String nickname){
220+
if(isDuplicateNickname(nickname)){
221+
throw new NicknameDuplicatedException(UserErrorCode.IS_DUPLICATED_NICKNAME);
222+
}
223+
}
224+
205225
private boolean isDuplicateEmail(String email) {
206226
return userRepository.findByEmail(email).isPresent();
207227
}

0 commit comments

Comments
 (0)