Skip to content

Commit

Permalink
[Fix] 회원 친구목록 조회 시 잘못된 정보 불러오는 문제
Browse files Browse the repository at this point in the history
* [Refactor] setter update{필드명} 으로 변경, 위치 조정

* [Bug] 닉네임 변경 없는 유저 수정 시 중복 발생 해결
  • Loading branch information
eunki96 authored Dec 24, 2023
1 parent 1fc3ef2 commit c4c55b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
49 changes: 23 additions & 26 deletions favor/src/main/java/com/favor/favor/user/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.favor.favor.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.favor.favor.anniversary.Anniversary;
import com.favor.favor.anniversary.AnniversaryResponseDto;
import com.favor.favor.common.enums.Favor;
Expand Down Expand Up @@ -34,32 +33,13 @@ public class User extends TimeStamped implements UserDetails {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userNo;
private String email;

private String password;
public void setPassword(String password){ this.password = password; }

private String userId;
public void setUserId(String userId){
this.userId = userId;
}

private String name;
public void setName(String name) {
this.name = name;
}


@Builder.Default
@ElementCollection
private List<Integer> favorList = new ArrayList<>();
@Transactional
public void setFavorList(List<Favor> favorList) {
ArrayList<Integer> favorTypeList = new ArrayList<>();
for(Favor favor : favorList){
favorTypeList.add(favor.getType());
}
this.favorList = favorTypeList;
}

@Builder.Default
@OneToMany(mappedBy = "user", orphanRemoval = true)
Expand All @@ -79,18 +59,35 @@ public void setFavorList(List<Favor> favorList) {

@OneToOne(cascade = CascadeType.ALL)
private UserPhoto userProfilePhoto;
public void setUserProfilePhoto(UserPhoto userPhoto) {
this.userProfilePhoto = userPhoto;
}

@OneToOne(cascade = CascadeType.ALL)
private UserPhoto userBackgroundPhoto;
public void setUserBackgroundPhoto(UserPhoto userPhoto) {
this.userBackgroundPhoto = userPhoto;
}

private Role role;


public void updatePassword(String password){ this.password = password; }
public void updateUserId(String userId){
this.userId = userId;
}
public void updateName(String name) {
this.name = name;
}
@Transactional
public void updateFavorList(List<Favor> favorList) {
ArrayList<Integer> favorTypeList = new ArrayList<>();
for(Favor favor : favorList){
favorTypeList.add(favor.getType());
}
this.favorList = favorTypeList;
}
public void updateUserProfilePhoto(UserPhoto userPhoto) {
this.userProfilePhoto = userPhoto;
}
public void updateUserBackgroundPhoto(UserPhoto userPhoto) {
this.userBackgroundPhoto = userPhoto;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {return null;}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;
import java.util.Optional;
import java.util.UUID;

import static com.favor.favor.exception.ExceptionCode.SERVER_ERROR;
Expand Down Expand Up @@ -45,7 +44,7 @@ public User updateUserProfilePhoto(Long userNo, MultipartFile file) {
throw new CustomException(e, SERVER_ERROR);
}

user.setUserProfilePhoto(userPhoto);
user.updateUserProfilePhoto(userPhoto);
return userRepository.save(user);
}

Expand All @@ -60,7 +59,7 @@ public void deleteUserProfilePhoto(Long userNo) {

User user = userService.findUserByUserNo(userNo);
String fileName = extractProfilePhotoFileName(user);
user.setUserProfilePhoto(null);
user.updateUserProfilePhoto(null);
photoService.deleteFileFromS3(fileName);
}

Expand All @@ -86,7 +85,7 @@ public User updateUserBackgroundPhoto(Long userNo, MultipartFile file) {
throw new CustomException(e, SERVER_ERROR);
}

user.setUserBackgroundPhoto(userPhoto);
user.updateUserBackgroundPhoto(userPhoto);
return userRepository.save(user);
}

Expand All @@ -101,7 +100,7 @@ public void deleteUserBackgroundPhoto(Long userNo) {

User user = userService.findUserByUserNo(userNo);
String fileName = extractBackgroundPhotoFileName(user);
user.setUserBackgroundPhoto(null);
user.updateUserBackgroundPhoto(null);
photoService.deleteFileFromS3(fileName);
}

Expand Down
25 changes: 16 additions & 9 deletions favor/src/main/java/com/favor/favor/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.favor.favor.exception.ExceptionCode.*;
Expand Down Expand Up @@ -61,8 +60,8 @@ public UserResponseDto signUp(SignDto signDto) {
public UserResponseDto createProfile(ProfileDto profileDto, Long userNo) {
User user = findUserByUserNo(userNo);

user.setName(profileDto.getName());
user.setUserId(profileDto.getUserId());
user.updateName(profileDto.getName());
user.updateUserId(profileDto.getUserId());
save(user);

return user.toDto(user, readReminderList(user.getUserNo()), readFriendList(user.getUserNo()), readFavorList(user.getUserNo()), readAnniversaryList(user.getUserNo()), getGiftInfo(user.getUserNo()));
Expand Down Expand Up @@ -99,14 +98,22 @@ public void isRightPassword(String password, User user){

@Transactional
public UserResponseDto updateUser(Long userNo, UserUpdateRequestDto userUpdateRequestDto){

//user 존재여부 확인
isExistingUserNo(userNo);
isExistingUserId(userUpdateRequestDto.getUserId());

//user 의 기존 닉네임 확인
User user = findUserByUserNo(userNo);
user.setName(userUpdateRequestDto.getName());
user.setUserId(userUpdateRequestDto.getUserId());
user.setFavorList(userUpdateRequestDto.getFavorList());
String beforeUserId = user.getUserId();
String newUserId = userUpdateRequestDto.getUserId();

//닉네임 변경 시 중복 체크
if(!beforeUserId.equals(newUserId)){
isExistingUserId(userUpdateRequestDto.getUserId());
}

user.updateName(userUpdateRequestDto.getName());
user.updateUserId(userUpdateRequestDto.getUserId());
user.updateFavorList(userUpdateRequestDto.getFavorList());
save(user);

return user.toDto(user, readReminderList(user.getUserNo()), readFriendList(user.getUserNo()), readFavorList(user.getUserNo()), readAnniversaryList(user.getUserNo()), getGiftInfo(user.getUserNo()));
Expand All @@ -121,7 +128,7 @@ public void deleteUser(Long userNo) {
@Transactional
public UserResponseDto updatePassword(String email, String password){
User user = findUserByEmail(email);
user.setPassword(passwordEncoder.encode(password));
user.updatePassword(passwordEncoder.encode(password));
save(user);

return user.toDto(user, readReminderList(user.getUserNo()), readFriendList(user.getUserNo()), readFavorList(user.getUserNo()), readAnniversaryList(user.getUserNo()), getGiftInfo(user.getUserNo()));
Expand Down

0 comments on commit c4c55b4

Please sign in to comment.