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 1b5735a..d836b83 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 @@ -7,6 +7,7 @@ import com.gdscewha.withmate.domain.matching.service.MatchingService; import com.gdscewha.withmate.domain.member.entity.Member; import com.gdscewha.withmate.domain.member.service.MemberService; +import com.gdscewha.withmate.domain.model.Category; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -38,10 +39,19 @@ public ResponseEntity getMatchingInfo() { return ResponseEntity.ok().body(resDto); } - // 매핑 가능한 사람 보기 (카테고리 순서대로 반환됨) + // 특정 카테고리의 매칭 가능한 사람 1명 보기 + @GetMapping("/match/person") + public ResponseEntity getPersonMatching(@RequestParam Category category) { + MatchingResDto matchingResDto = matchingService.getMatchingByCategory(category); + if (matchingResDto == null) + return ResponseEntity.ok().body("매칭 가능한 상대방이 없습니다"); // 사람이 없음; 매칭 대기 띄워야 함 + return ResponseEntity.ok().body(matchingResDto); + } + + // 매칭 가능한 사람들 모두 보기 (카테고리 순서대로 반환됨) @GetMapping("/match/people") public ResponseEntity getPeopleMatching() { - List matchingList = matchingService.getPeopleMatching(); + List matchingList = matchingService.getCurrentMatchingList(); if (matchingList.isEmpty()) return ResponseEntity.ok().body("매칭 가능한 상대방이 없습니다"); // 사람이 없음; 매칭 대기 띄워야 함 return ResponseEntity.ok().body(matchingList); @@ -55,6 +65,8 @@ public ResponseEntity postNewMatchingRelation(@RequestBody MatchingInputDto r return ResponseEntity.badRequest().body("목표 혹은 카테고리가 비어있습니다."); // 정상적으로 매칭 맺기 가능 MatchedResultDto resultDto = matchingService.getMatchedResult(reqDto); + if (resultDto == null) + return ResponseEntity.badRequest().body("매칭할 메이트가 없거나, 내가 매칭 불가능한 상태입니다."); return ResponseEntity.ok().body(resultDto); } @@ -66,6 +78,8 @@ public ResponseEntity updateMatchingInfo(@RequestBody MatchingInputDto reqDto return ResponseEntity.badRequest().body("목표 혹은 카테고리가 비어있습니다."); // 정상적으로 매칭 생성/업데이트 가능 Matching matching = matchingService.createOrUpdateMatching(reqDto); + if (matching == null) + return ResponseEntity.badRequest().body("이미 메이트가 있습니다."); return ResponseEntity.ok().body(matching); } diff --git a/withmate/src/main/java/com/gdscewha/withmate/domain/matching/dto/MatchedResultDto.java b/withmate/src/main/java/com/gdscewha/withmate/domain/matching/dto/MatchedResultDto.java index 0c4912f..32fb04a 100644 --- a/withmate/src/main/java/com/gdscewha/withmate/domain/matching/dto/MatchedResultDto.java +++ b/withmate/src/main/java/com/gdscewha/withmate/domain/matching/dto/MatchedResultDto.java @@ -6,6 +6,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.springframework.data.util.Pair; import java.util.List; @@ -23,12 +24,12 @@ public class MatchedResultDto { private Category category2; // List을 받는 생성자 - public MatchedResultDto(List matching) { - this.nickname1 = matching.get(0).getMember().getNickname(); - this.goal1 = matching.get(0).getGoal(); - this.category1 = matching.get(0).getCategory(); - this.nickname2 = matching.get(1).getMember().getNickname(); - this.goal2 = matching.get(1).getGoal(); - this.category2 = matching.get(1).getCategory(); + public MatchedResultDto(Pair matchedPair) { + this.nickname1 = matchedPair.getFirst().getMember().getNickname(); + this.goal1 = matchedPair.getFirst().getGoal(); + this.category1 = matchedPair.getFirst().getCategory(); + this.nickname2 = matchedPair.getSecond().getMember().getNickname(); + this.goal2 = matchedPair.getSecond().getGoal(); + this.category2 = matchedPair.getSecond().getCategory(); } } 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 667e73e..19f9f22 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 @@ -1,9 +1,7 @@ package com.gdscewha.withmate.domain.matching.service; -import com.gdscewha.withmate.common.response.exception.CategoryException; import com.gdscewha.withmate.common.response.exception.ErrorCode; import com.gdscewha.withmate.common.response.exception.MatchingException; -import com.gdscewha.withmate.domain.journey.entity.Journey; import com.gdscewha.withmate.domain.journey.service.JourneyService; import com.gdscewha.withmate.domain.matching.dto.MatchedResultDto; import com.gdscewha.withmate.domain.matching.dto.MatchingInputDto; @@ -17,10 +15,10 @@ import com.gdscewha.withmate.domain.model.Category; import com.gdscewha.withmate.domain.relation.entity.Relation; import com.gdscewha.withmate.domain.relation.service.RelationMateService; -import com.gdscewha.withmate.domain.sticker.service.StickerService; -import com.gdscewha.withmate.domain.week.entity.Week; import com.gdscewha.withmate.domain.week.service.WeekService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -29,6 +27,7 @@ import java.util.Optional; import java.util.stream.Collectors; +@Slf4j @Service @RequiredArgsConstructor public class MatchingService { @@ -41,8 +40,6 @@ public class MatchingService { // Relation 만들 때 필요한 Service들 private final JourneyService journeyService; private final WeekService weekService; - private final StickerService stickerService; - // 내 매칭 받아오기 (MatchingResDto 반환, 존재하지 않으면 null 반환) public MatchingInputDto getCurrentMatchingDto() { @@ -64,6 +61,8 @@ public Boolean checkMatchingAvailability(MatchingInputDto reqDto) { // 내 매칭을 생성 혹은 기존 매칭 업데이트 public Matching createOrUpdateMatching(MatchingInputDto reqDto) { Member member = memberService.getCurrentMember(); + if (member.getIsRelationed()) + return null; if (member.getMatching() == null) { // 매칭한 적 없을 때 생성 return createMatching(member, reqDto); @@ -105,18 +104,17 @@ public String deleteMyMatching() { return "취소할 Matching이 없습니다."; } - // 매칭 삭제 - public Member deleteMatching(Member member) { - if (member.getMatching() != null) { - matchingRepository.delete(member.getMatching()); - member.setMatching(null); - memberRepository.save(member); - } - return member; + // 특정 카테고리의 매칭중인 사람 1명을 반환 + public MatchingResDto getMatchingByCategory(Category category) { + List matchingList = matchingRepository.findAllByCategory(category); + if (matchingList == null || matchingList.isEmpty()) + return null; + Member member = memberRepository.findMemberByMatching(matchingList.get(0)); + return new MatchingResDto(matchingList.get(0), member); } // 모든 카테고리의 매칭중인 사람들을 조회 - public List getPeopleMatching() { + public List getCurrentMatchingList() { Category[] categories = Category.values(); List matchingResList = new ArrayList<>(); for (Category c : categories) { @@ -136,50 +134,54 @@ public List convertToMatchingResDtoList(List matchingL // tryMatching로부터 생성되는 매칭 결과를 컨트롤러에 반환 public MatchedResultDto getMatchedResult(MatchingInputDto reqDto) { - Matching myMatching = createOrUpdateMatching(reqDto); // 내 매칭 생성 - List matchingList = relateMatesByCategory(reqDto.getCategory(), myMatching); // 카테고리로 Mates 관계 맺기 - return new MatchedResultDto(matchingList); + Pair matchingPair = relateMatesByCategory(reqDto.getCategory(), reqDto); // 카테고리로 Mates 관계 맺기 + if (matchingPair == null) + return null; + return new MatchedResultDto(matchingPair); } // Matching으로 Mates 관계 맺기 @Transactional - public List relateMatesByCategory(Category category, Matching myMatching) { - if (category == null) - throw new CategoryException(ErrorCode.CATEGORY_NOT_FOUND); + public Pair relateMatesByCategory(Category category, MatchingInputDto reqDto) { + Member me = memberService.getCurrentMember(); + if (me.getIsRelationed()) { + log.info("멤버: isRelationed"); + return null; + } + if (me.getMatching().equals(null)){ + log.info("멤버: matched"); + return null; + } // 같은 카테고리의 Matching 리스트 List matchingList = matchingRepository.findAllByCategory(category); - // matchingList에 2개 이상이어야 매칭 가능 - if (matchingList.size() >= 2){ + // matchingList에 1개 이상이어야 매칭 가능 (내 매칭은 db에 저장하지 않음) + if (matchingList.size() >= 1){ Matching mateMatching = matchingList.get(0); 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); + if (mate == null || mateMatching == null) + throw new MatchingException(ErrorCode.MATCHING_NOT_FOUND); + Matching myMatching = Matching.builder() // 내 매칭 생성 (DB에 저장 X) + .goal(reqDto.getGoal()) + .category(reqDto.getCategory()) + .member(me) + .build(); + Pair matchingPair = Pair.of(mateMatching, myMatching); + Pair memberPair = Pair.of(mate, me); // Relation 생성, MemberRelationPair 생성 Relation relation = relationMateService.createRelation(); - memberRelationService.createMemberRelationPair(mateList, memberList, relation); + memberRelationService.createMemberRelationPair(matchingPair, memberPair, relation); // 첫번째 Journey 생성 - Journey journey = journeyService.createJourney(relation); + journeyService.createJourney(relation); // 첫번째 Week 생성 - Week week = weekService.createWeek(); - + weekService.createWeek(); // Member들의 isRelationed를 true로 업데이트 후 저장 mate.setIsRelationed(true); me.setIsRelationed(true); - - // 각 멤버에 Matching null로, Matching 삭제 - memberRepository.save(deleteMatching(mate)); - memberRepository.save(deleteMatching(me)); - // 수정된 Matching 리스트 반환 - return mateList; + // Mate의 매칭 삭제 + matchingRepository.delete(mateMatching); + return matchingPair; } else { throw new MatchingException(ErrorCode.MATCHING_NOT_FOUND); } 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 0413a40..e1f36f4 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 @@ -9,6 +9,7 @@ import com.gdscewha.withmate.domain.memberrelation.repository.MemberRelationRepository; import com.gdscewha.withmate.domain.relation.entity.Relation; import lombok.RequiredArgsConstructor; +import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; import java.util.List; @@ -52,22 +53,19 @@ public MemberRelation findLastMROfMember(Member member) { } // MR 두 개 만들고 저장, 두 Member의 isRelationed는 MatchingService에서 바꿔줌 - public void createMemberRelationPair(List matchingList, List memberList, Relation relation){ - Matching matching1 = matchingList.get(0); - Matching matching2 = matchingList.get(1); - + public void createMemberRelationPair(Pair matchingPair, Pair memberPair, Relation relation){ MemberRelation myMR = MemberRelation.builder() - .goal(matching1.getGoal()) - .category(matching1.getCategory()) + .goal(matchingPair.getFirst().getGoal()) + .category(matchingPair.getFirst().getCategory()) // message is nullable - .member(memberList.get(0)) + .member(memberPair.getFirst()) .relation(relation) .build(); MemberRelation mateMR = MemberRelation.builder() - .goal(matching2.getGoal()) - .category(matching2.getCategory()) + .goal(matchingPair.getSecond().getGoal()) + .category(matchingPair.getSecond().getCategory()) // message is nullable - .member(memberList.get(1)) + .member(memberPair.getSecond()) .relation(relation) .build(); mRRepository.save(myMR); diff --git a/withmate/src/main/java/com/gdscewha/withmate/domain/week/service/WeekService.java b/withmate/src/main/java/com/gdscewha/withmate/domain/week/service/WeekService.java index 8c4b079..895883e 100644 --- a/withmate/src/main/java/com/gdscewha/withmate/domain/week/service/WeekService.java +++ b/withmate/src/main/java/com/gdscewha/withmate/domain/week/service/WeekService.java @@ -39,7 +39,7 @@ public Week createWeek() { public Week updateStickerCountOfCurrentWeek(Long diff){ Member member = memberService.getCurrentMember(); Week week = getCurrentWeek(member); - if(week == null) + if (week == null) throw new WeekException(ErrorCode.WEEK_NOT_FOUND); Long newStickerCount = week.getStickerCount() + diff; if (newStickerCount < 0)