diff --git a/withmate/src/main/java/com/gdscewha/withmate/domain/matching/controller/MatchingController.java b/withmate/src/main/java/com/gdscewha/withmate/domain/matching/controller/MatchingController.java index 7adf49d..1b5735a 100644 --- a/withmate/src/main/java/com/gdscewha/withmate/domain/matching/controller/MatchingController.java +++ b/withmate/src/main/java/com/gdscewha/withmate/domain/matching/controller/MatchingController.java @@ -72,9 +72,7 @@ public ResponseEntity updateMatchingInfo(@RequestBody MatchingInputDto reqDto // 매칭 대기 취소: 내 매칭 삭제 @DeleteMapping("/match/cancel") public ResponseEntity cancelMatching() { - Matching matching = matchingService.deleteMatching(); - if (matching == null) - return ResponseEntity.ok().body("취소할 Matching이 없습니다."); - return ResponseEntity.ok().body("기존 matching을 취소했습니다."); + String cancelMessage = matchingService.deleteMyMatching(); + return ResponseEntity.ok().body(cancelMessage); } } diff --git a/withmate/src/main/java/com/gdscewha/withmate/domain/matching/dto/MatchingResDto.java b/withmate/src/main/java/com/gdscewha/withmate/domain/matching/dto/MatchingResDto.java index 2060dd7..28f884c 100644 --- a/withmate/src/main/java/com/gdscewha/withmate/domain/matching/dto/MatchingResDto.java +++ b/withmate/src/main/java/com/gdscewha/withmate/domain/matching/dto/MatchingResDto.java @@ -1,6 +1,7 @@ package com.gdscewha.withmate.domain.matching.dto; import com.gdscewha.withmate.domain.matching.entity.Matching; +import com.gdscewha.withmate.domain.member.entity.Member; import com.gdscewha.withmate.domain.model.Category; import lombok.AllArgsConstructor; import lombok.Builder; @@ -17,11 +18,10 @@ public class MatchingResDto { private Category category; private String country; - // Matching을 받는 생성자 - public MatchingResDto(Matching matching) { - this.nickname = matching.getMember().getNickname(); + public MatchingResDto(Matching matching, Member member) { + this.nickname = member.getNickname(); this.goal = matching.getGoal(); this.category = matching.getCategory(); - this.country = matching.getMember().getCountry(); + this.country = member.getCountry(); } } diff --git a/withmate/src/main/java/com/gdscewha/withmate/domain/matching/service/MatchingService.java b/withmate/src/main/java/com/gdscewha/withmate/domain/matching/service/MatchingService.java index 6657d6c..7389aed 100644 --- a/withmate/src/main/java/com/gdscewha/withmate/domain/matching/service/MatchingService.java +++ b/withmate/src/main/java/com/gdscewha/withmate/domain/matching/service/MatchingService.java @@ -17,6 +17,7 @@ import com.gdscewha.withmate.domain.relation.service.RelationMateService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @@ -70,7 +71,10 @@ public Matching createMatching(Member member, MatchingInputDto reqDto) { .category(reqDto.getCategory()) .member(member) .build(); - return matchingRepository.save(matching); + matchingRepository.save(matching); + member.setMatching(matching); + memberRepository.save(member); + return matching; } // 매칭 객체 업데이트하기 (전에 매칭을 했던 사람) - 목표, 카테고리 업데이트 @@ -81,15 +85,25 @@ public Matching updateMatching(Matching matching, MatchingInputDto reqDto){ } // 내 매칭 삭제 (매칭 취소) - public Matching deleteMatching() { + public String deleteMyMatching() { Member member = memberService.getCurrentMember(); if (member.getMatching() != null) { matchingRepository.delete(member.getMatching()); member.setMatching(null); memberRepository.save(member); - return member.getMatching(); + return "기존 matching을 취소했습니다."; } - return null; + return "취소할 Matching이 없습니다."; + } + + // 매칭 삭제 + public Member deleteMatching(Member member) { + if (member.getMatching() != null) { + matchingRepository.delete(member.getMatching()); + member.setMatching(null); + memberRepository.save(member); + } + return member; } // 모든 카테고리의 매칭중인 사람들을 조회 @@ -104,7 +118,10 @@ public List getPeopleMatching() { } public List convertToMatchingResDtoList(List matchingList) { return matchingList.stream() - .map(MatchingResDto::new) + .map(matching -> { + Member member = memberRepository.findMemberByMatching(matching); + return new MatchingResDto(matching, member); + }) .collect(Collectors.toList()); } @@ -116,6 +133,7 @@ public MatchedResultDto getMatchedResult(MatchingInputDto reqDto) { } // Matching으로 Mates 관계 맺기 + @Transactional public List relateMatesByCategory(Category category, Matching myMatching) { if (category == null) throw new CategoryException(ErrorCode.CATEGORY_NOT_FOUND); @@ -124,28 +142,30 @@ public List relateMatesByCategory(Category category, Matching myMatchi // matchingList에 2개 이상이어야 매칭 가능 if (matchingList.size() >= 2){ Matching mateMatching = matchingList.get(0); - Member mate = mateMatching.getMember(); + Member mate = memberRepository.findMemberByMatching(mateMatching); Member me = memberService.getCurrentMember(); // mate 탈퇴시 matching도 삭제되므로 유효한 멤버 List mateList = new ArrayList<>(); // 메이트 리스트 mateList.add(mateMatching); mateList.add(myMatching); + List memberList = new ArrayList<>(); + memberList.add(mate); + memberList.add(me); // Relation 생성, MemberRelationPair 생성 Relation pairRelation = relationMateService.createRelation(); - memberRelationService.createMemberRelationPair(mateList, pairRelation); + memberRelationService.createMemberRelationPair(mateList, memberList, pairRelation); // Matching 삭제 - matchingRepository.deleteAll(matchingList); + //matchingRepository.deleteAll(matchingList); // Member들의 isRelationed를 true로 업데이트 후 저장 mate.setIsRelationed(true); me.setIsRelationed(true); - // 각 멤버에 Matching set 하기 - mate.setMatching(mateMatching); - me.setMatching(myMatching); - memberRepository.save(mate); - memberRepository.save(me); + + // 각 멤버에 Matching null로 + memberRepository.save(deleteMatching(mate)); + memberRepository.save(deleteMatching(me)); // 수정된 Matching 리스트 반환 return mateList; } else { diff --git a/withmate/src/main/java/com/gdscewha/withmate/domain/memberrelation/service/MemberRelationService.java b/withmate/src/main/java/com/gdscewha/withmate/domain/memberrelation/service/MemberRelationService.java index 845b88d..5d9d3c0 100644 --- a/withmate/src/main/java/com/gdscewha/withmate/domain/memberrelation/service/MemberRelationService.java +++ b/withmate/src/main/java/com/gdscewha/withmate/domain/memberrelation/service/MemberRelationService.java @@ -43,7 +43,7 @@ public MemberRelation findLastMROfMember(Member member) { } // MR 두 개 만들고 저장, 두 Member의 isRelationed는 MatchingService에서 바꿔줌 - public void createMemberRelationPair(List matchingList, Relation relation){ + public void createMemberRelationPair(List matchingList, List memberList, Relation relation){ Matching matching1 = matchingList.get(0); Matching matching2 = matchingList.get(1); @@ -51,14 +51,14 @@ public void createMemberRelationPair(List matchingList, Relation relat .goal(matching1.getGoal()) .category(matching1.getCategory()) // message is nullable - .member(matching1.getMember()) + .member(memberList.get(0)) .relation(relation) .build(); MemberRelation mateMR = MemberRelation.builder() .goal(matching2.getGoal()) .category(matching2.getCategory()) // message is nullable - .member(matching2.getMember()) + .member(memberList.get(1)) .relation(relation) .build(); mRRepository.save(myMR);