Skip to content

Commit

Permalink
Merge pull request #75 from Solucitation/fix/codebase2
Browse files Browse the repository at this point in the history
Fix/codebase2
  • Loading branch information
khee2 authored Aug 1, 2024
2 parents 1b659e6 + 1220d96 commit d2bc164
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ public void setLatitude(Float latitude) {
public void setLongitude(Float longitude) {
this.longitude = longitude;
}

public void assignMember(Member member) {
this.member = member;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ public FavFriend saveFavoriteFriend(String address, String name, Float latitude,
});

FavFriend favFriend = FavFriend.builder()
.member(member)
.address(address)
.name(name)
.latitude(latitude)
.longitude(longitude)
.build();

member.addFavFriend(favFriend); // 양방향 연관 관계 설정
return favoriteFriendRepository.save(favFriend);
}

Expand All @@ -54,6 +53,7 @@ public void deleteFavoriteFriendByName(Long favFriendId, String email) {
FavFriend favFriend = favoriteFriendRepository.findByFavFriendIdAndMemberId(favFriendId, member.getId())
.orElseThrow(() -> new RuntimeException("존재하지 않는 친구입니다."));

member.removeFavFriend(favFriend); // 양방향 연관 관계 해제
favoriteFriendRepository.delete(favFriend);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public void setLongitude(Float longitude) {
this.longitude = longitude;
}

public void assignMember(Member member) {
this.member = member;
}

public enum AddrType {
HOME, WORK
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public FavPlace saveFavoritePlace(String addr, Float latitude, Float longitude,
});

FavPlace favPlace = FavPlace.builder()
.member(member)
.addr(addr)
.latitude(latitude)
.longitude(longitude)
.addrType(addrType)
.build();

member.addFavPlace(favPlace); // 양방향 연관 관계 설정
return favPlaceRepository.save(favPlace);
}

Expand All @@ -49,7 +49,7 @@ public void deleteFavoritePlace(Long favPlaceId, String email) {
if (!favPlace.getMember().equals(member)) {
throw new RuntimeException("접근 권한이 없습니다.");
}

member.removeFavPlace(favPlace); // 양방향 연관 관계 해제
favPlaceRepository.delete(favPlace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public ResponseEntity<?> updateProfile(
return ResponseEntity.badRequest().body(Map.of("error", "profile_update_empty_dto", "message", "프로필 수정 요청 dto가 비었습니다."));
}
ProfileUpdateRequestDto profileUpdateRequestDto = objectMapper.readValue(profileUpdateRequestDtoJson, ProfileUpdateRequestDto.class);
log.info("profileUpdateRequestDto = " + profileUpdateRequestDto);

Set<ConstraintViolation<ProfileUpdateRequestDto>> violations = validator.validate(profileUpdateRequestDto);
if (!violations.isEmpty()) {
List<ValidationErrorResponse.FieldError> fieldErrors = violations.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.solucitation.midpoint_backend.domain.member.entity;

import com.solucitation.midpoint_backend.domain.FavFriend.entity.FavFriend;
import com.solucitation.midpoint_backend.domain.FavPlace.entity.FavPlace;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;

import java.util.HashSet;
import java.util.Set;

@Entity
Expand Down Expand Up @@ -38,13 +40,48 @@ public class Member {
private String nickname;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<FavPlace> favPlaces;
private Set<FavPlace> favPlaces = new HashSet<>(); // 초기화

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<FavFriend> favFriends = new HashSet<>(); // 초기화

public Member(String password, String name, String email, String nickname, String loginId) {
this.email= email;
this.email = email;
this.name = name;
this.pwd = password;
this.nickname = nickname;
this.loginId = loginId;
}

public void addFavPlace(FavPlace favPlace) {
favPlaces.add(favPlace);
favPlace.assignMember(this);
}

public void removeFavPlace(FavPlace favPlace) {
favPlaces.remove(favPlace);
favPlace.assignMember(null);
}

public void addFavFriend(FavFriend favFriend) {
favFriends.add(favFriend);
favFriend.assignMember(this);
}

public void removeFavFriend(FavFriend favFriend) {
favFriends.remove(favFriend);
favFriend.assignMember(null);
}

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

public void assignNickname(String nickname) {
this.nickname = nickname;
}

public void assignPwd(String encodedPassword) {
this.pwd = pwd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,11 @@ public void resetPassword(String email, String newPassword) {
// 비밀번호 암호화
String encodedPassword = passwordEncoder.encode(newPassword);

// 변경된 비밀번호로 새로운 Member 객체 생성
Member updatedMember = Member.builder()
.id(member.getId())
.name(member.getName())
.email(member.getEmail())
.nickname(member.getNickname())
.pwd(encodedPassword)
.build();
// pwd 필드를 암호화된 새로운 비밀번호로 할당
member.assignPwd(encodedPassword);

// 변경된 비밀번호 저장
memberRepository.save(updatedMember);
memberRepository.save(member);
}


Expand Down Expand Up @@ -317,13 +311,27 @@ public void updateMember(String currentEmail, ProfileUpdateRequestDto profileUpd
Member member = memberRepository.findByEmail(currentEmail)
.orElseThrow(() -> new IllegalArgumentException("해당 이메일의 회원이 존재하지 않습니다."));

// 닉네임이 이미 사용 중인지 확인
if (isNicknameAlreadyInUse(profileUpdateRequestDto.getNickname())) {
throw new NicknameAlreadyInUseException("이미 사용중인 닉네임입니다.");
String newNickname = profileUpdateRequestDto.getNickname();

// 새로운 닉네임이 현재 닉네임과 다른 경우에만 중복 확인
if (!newNickname.equals(member.getNickname())) {
Optional<Member> byNickname = memberRepository.findByNickname(newNickname);

// 중복된 닉네임이 존재하고, 그것이 다른 사람의 닉네임인 경우 에러 처리
if (byNickname.isPresent() && !byNickname.get().getId().equals(member.getId())) {
throw new NicknameAlreadyInUseException("이미 사용중인 닉네임입니다.");
}
// 새로운 닉네임으로 업데이트
member.assignNickname(newNickname);
}

// 이름이 기존과 동일하지 않으면 업데이트
if (!profileUpdateRequestDto.getName().equals(member.getName())) {
member.assignName(profileUpdateRequestDto.getName());
}

// 회원 정보 업데이트
updateMemberDetails(member, profileUpdateRequestDto);
updateMemberDetails(member);

String profileImageUrl = null;
boolean isDefaultImage = false;
Expand Down Expand Up @@ -374,19 +382,10 @@ private void handleExistingImageDeletion(Member member) {
* member 업데이트
*
* @param member
* @param profileUpdateRequestDto
*/
@Transactional
public void updateMemberDetails(Member member, ProfileUpdateRequestDto profileUpdateRequestDto) {
Member updatedMember = Member.builder()
.id(member.getId())
.name(profileUpdateRequestDto.getName())
.loginId(member.getLoginId())
.nickname(member.getNickname())
.email(member.getEmail())
.pwd(member.getPwd())
.build();
memberRepository.save(updatedMember);
public void updateMemberDetails(Member member) {
memberRepository.save(member);
}

/**
Expand Down

0 comments on commit d2bc164

Please sign in to comment.