Skip to content

Commit

Permalink
Feat(Matching) : /api/match/peson 개발, 코드 리팩토링, 예외처리 등
Browse files Browse the repository at this point in the history
  • Loading branch information
seohyun-lee committed Feb 17, 2024
1 parent 1e5ca0d commit 8442dfa
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<MatchingResDto> matchingList = matchingService.getPeopleMatching();
List<MatchingResDto> matchingList = matchingService.getCurrentMatchingList();
if (matchingList.isEmpty())
return ResponseEntity.ok().body("매칭 가능한 상대방이 없습니다"); // 사람이 없음; 매칭 대기 띄워야 함
return ResponseEntity.ok().body(matchingList);
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.util.Pair;

import java.util.List;

Expand All @@ -23,12 +24,12 @@ public class MatchedResultDto {
private Category category2;

// List<Matching>을 받는 생성자
public MatchedResultDto(List<Matching> 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<Matching, Matching> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -29,6 +27,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class MatchingService {
Expand All @@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -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<Matching> 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<MatchingResDto> getPeopleMatching() {
public List<MatchingResDto> getCurrentMatchingList() {
Category[] categories = Category.values();
List<MatchingResDto> matchingResList = new ArrayList<>();
for (Category c : categories) {
Expand All @@ -136,50 +134,54 @@ public List<MatchingResDto> convertToMatchingResDtoList(List<Matching> matchingL

// tryMatching로부터 생성되는 매칭 결과를 컨트롤러에 반환
public MatchedResultDto getMatchedResult(MatchingInputDto reqDto) {
Matching myMatching = createOrUpdateMatching(reqDto); // 내 매칭 생성
List<Matching> matchingList = relateMatesByCategory(reqDto.getCategory(), myMatching); // 카테고리로 Mates 관계 맺기
return new MatchedResultDto(matchingList);
Pair<Matching, Matching> matchingPair = relateMatesByCategory(reqDto.getCategory(), reqDto); // 카테고리로 Mates 관계 맺기
if (matchingPair == null)
return null;
return new MatchedResultDto(matchingPair);
}

// Matching으로 Mates 관계 맺기
@Transactional
public List<Matching> relateMatesByCategory(Category category, Matching myMatching) {
if (category == null)
throw new CategoryException(ErrorCode.CATEGORY_NOT_FOUND);
public Pair<Matching, Matching> 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<Matching> 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<Matching> mateList = new ArrayList<>(); // 메이트 리스트
mateList.add(mateMatching);
mateList.add(myMatching);
List<Member> 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<Matching, Matching> matchingPair = Pair.of(mateMatching, myMatching);
Pair<Member, Member> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,22 +53,19 @@ public MemberRelation findLastMROfMember(Member member) {
}

// MR 두 개 만들고 저장, 두 Member의 isRelationed는 MatchingService에서 바꿔줌
public void createMemberRelationPair(List<Matching> matchingList, List<Member> memberList, Relation relation){
Matching matching1 = matchingList.get(0);
Matching matching2 = matchingList.get(1);

public void createMemberRelationPair(Pair<Matching, Matching> matchingPair, Pair<Member, Member> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8442dfa

Please sign in to comment.