Skip to content

Commit

Permalink
refactor : 매칭 로직이 MemberId가 아닌, Member엔티티를 가지고 동작 하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
suhyun0918 committed Feb 16, 2024
1 parent c2a1b3e commit 605b02d
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 39 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/aliens/backend/block/domain/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public static Block of(Member blockedMember, Member blockingMember) {
return block;
}

public Member getBlockingMember() {
return blockingMember;
}

public Member getBlockedMember() {
return blockedMember;
}

public Long getBlockingMemberId() {
return blockingMember.getId();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package com.aliens.backend.mathcing.business.model;

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.block.domain.Block;

import java.util.List;

public class BlockedPartnerGroup {
private final List<Long> blockedPartners;
private final List<Member> blockedPartners;

public BlockedPartnerGroup(final List<Long> blockedPartners) {
public BlockedPartnerGroup(final List<Member> blockedPartners) {
this.blockedPartners = blockedPartners;
}

public static BlockedPartnerGroup from(final List<Block> blockHistories) {
List<Long> blockedPartners = blockHistories.stream()
.mapToLong(Block::getBlockedMemberId).boxed().toList();
List<Member> blockedPartners = blockHistories.stream()
.map(Block::getBlockedMember).toList();
return new BlockedPartnerGroup(blockedPartners);
}

public boolean contains(Participant participant) {
return blockedPartners.contains(participant.memberId());
return blockedPartners.contains(participant.member());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aliens.backend.mathcing.business.model;

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.block.domain.Block;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.global.exception.RestApiException;
Expand All @@ -10,7 +11,7 @@
import java.util.List;

public record Participant(
Long memberId,
Member member,
Language firstPreferLanguage,
Language secondPreferLanguage,
List<Partner> partners,
Expand All @@ -34,7 +35,7 @@ public static Participant from(final MatchingApplication matchingApplication,
BlockedPartnerGroup blockedPartnerGroup = BlockedPartnerGroup.from(blockHistories);

return new Participant(
matchingApplication.getMemberId(),
matchingApplication.getMember(),
matchingApplication.getFirstPreferLanguage(),
matchingApplication.getSecondPreferLanguage(),
new ArrayList<>(), previousPartnerGroup, blockedPartnerGroup
Expand All @@ -45,13 +46,13 @@ public int getNumberOfPartners() {
return partners.size();
}

public void addPartner(Relationship relationship, Long memberId) {
partners.add(Partner.of(relationship, memberId));
public void addPartner(Relationship relationship, Participant participant) {
partners.add(Partner.of(relationship, participant.member));
}

public boolean isPartnerWith(Participant participant) {
for (Partner partner : partners) {
if (partner.memberId().equals(participant.memberId())) {
if (partner.member().equals(participant.member())) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private boolean canContinueMatching(final Relationship relationship,
}

private void addMatching(final Participant participant, final Participant partner) {
participant.addPartner(relationship, partner.memberId());
partner.addPartner(relationship, participant.memberId());
participant.addPartner(relationship, partner);
partner.addPartner(relationship, participant);
}

private boolean isValidMatching(final Relationship relationship,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.aliens.backend.mathcing.business.model;

import com.aliens.backend.auth.domain.Member;

public record Partner(
Relationship relationship,
Long memberId
Member member
) {
public static Partner of(Relationship relationship, Long memberId) {
return new Partner(relationship, memberId);
public static Partner of(final Relationship relationship, final Member member) {
return new Partner(relationship, member);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package com.aliens.backend.mathcing.business.model;

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.mathcing.domain.MatchingResult;

import java.util.List;

public class PreviousPartnerGroup {
private final List<Long> previousPartners;
private final List<Member> previousPartners;

private PreviousPartnerGroup(final List<Long> previousPartners) {
private PreviousPartnerGroup(final List<Member> previousPartners) {
this.previousPartners = previousPartners;
}

public static PreviousPartnerGroup from(final List<MatchingResult> previousMatchingResults) {
List<Long> previousPartners = previousMatchingResults.stream()
.mapToLong(MatchingResult::getMatchedMemberId).boxed().toList();
List<Member> previousPartners = previousMatchingResults.stream()
.map(MatchingResult::getMatchedMember).toList();
return new PreviousPartnerGroup(previousPartners);
}

public boolean contains(Participant participant) {
return previousPartners.contains(participant.memberId());
return previousPartners.contains(participant.member());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.aliens.backend.mathcing.domain;

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.mathcing.business.model.Participant;
import com.aliens.backend.mathcing.business.model.Partner;
import com.aliens.backend.mathcing.domain.id.MatchingResultId;
import com.aliens.backend.mathcing.business.model.Relationship;
import jakarta.persistence.*;
Expand All @@ -20,17 +23,24 @@ private MatchingResult(final MatchingResultId id, final Relationship relationshi
this.relationship = relationship;
}

public static MatchingResult of(MatchingRound matchingRound,
Long matchingMemberId,
Long matchedMemberId,
Relationship relationship) {
return new MatchingResult(MatchingResultId.of(matchingRound, matchingMemberId, matchedMemberId), relationship);
public static MatchingResult from(final MatchingRound matchingRound,
final Participant participant,
final Partner partner) {
return new MatchingResult(MatchingResultId.of(matchingRound, participant.member(), partner.member()), partner.relationship());
}

public MatchingRound getMatchingRound() {
return id.getMatchingRound();
}

public Member getMatchingMember() {
return id.getMatchingMember();
}

public Member getMatchedMember() {
return id.getMatchedMember();
}

public Long getMatchingMemberId() {
return id.getMatchingMemberId();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aliens.backend.mathcing.domain.id;

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.mathcing.domain.MatchingRound;
import jakarta.persistence.*;

Expand All @@ -11,34 +12,44 @@ public class MatchingResultId implements Serializable {
@JoinColumn(name = "matching_round")
private MatchingRound matchingRound;

@Column(name = "matching_member_id")
private Long matchingMemberId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "matching_member_id")
private Member matchingMember;

@Column(name = "matched_member_id")
private Long matchedMemberId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "matched_member_id")
private Member matchedMember;

protected MatchingResultId() {
}

private MatchingResultId(final MatchingRound matchingRound, final Long matchingMemberId, final Long matchedMemberId) {
private MatchingResultId(final MatchingRound matchingRound, final Member matchingMember, final Member matchedMember) {
this.matchingRound = matchingRound;
this.matchingMemberId = matchingMemberId;
this.matchedMemberId = matchedMemberId;
this.matchingMember = matchingMember;
this.matchedMember = matchedMember;
}

public static MatchingResultId of(MatchingRound matchingRound, Long matchingMemberId, Long matchedMemberId) {
public static MatchingResultId of(final MatchingRound matchingRound,final Member matchingMemberId,final Member matchedMemberId) {
return new MatchingResultId(matchingRound, matchingMemberId, matchedMemberId);
}

public MatchingRound getMatchingRound() {
return matchingRound;
}

public Member getMatchingMember() {
return matchingMember;
}

public Member getMatchedMember() {
return matchedMember;
}

public Long getMatchingMemberId() {
return matchingMemberId;
return matchingMember.getId();
}

public Long getMatchedMemberId() {
return matchedMemberId;
return matchedMember.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public interface MatchingResultRepository extends JpaRepository<MatchingResult,
List<MatchingResult> findAllByMatchingRound(MatchingRound matchingRound);

@Query("SELECT mr FROM MatchingResult mr " +
"WHERE mr.id.matchingRound = :matchingRound AND mr.id.matchingMemberId = :memberId")
"WHERE mr.id.matchingRound = :matchingRound AND mr.id.matchingMember.id = :memberId")
List<MatchingResult> findAllByMatchingRoundAndMemberId(MatchingRound matchingRound, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ public List<MatchingResultResponse> findMatchingResult(final LoginMember loginMe
private void saveMatchingResult(final MatchingRound matchingRound, final List<Participant> participants) {
for (Participant participant : participants) {
for (Partner partner : participant.partners()) {
MatchingResult matchingResult =
MatchingResult.of(matchingRound, participant.memberId(), partner.memberId(), partner.relationship());
matchingResultRepository.save(matchingResult);
MatchingResult matchingResult = MatchingResult.from(matchingRound, participant, partner);
matchBetween(matchingResult);
}
// TODO : 매칭 완료 알림 이벤트 발송 & 채팅방 개설 이벤트 발송
}
Expand Down Expand Up @@ -123,6 +122,10 @@ private List<Block> getBlockListByBlockingMember(Member blockingMember) {
return blockRepository.findAllByBlockingMember(blockingMember);
}

private void matchBetween(MatchingResult matchingResult) {
matchingResultRepository.save(matchingResult);
}

private MatchingOperateRequest createOperateRequest(MatchingRound matchingRound) {
List<MatchingApplication> matchingApplications = getMatchingApplications(matchingRound);
List<MatchingResult> previousMatchingResult = getPreviousMatchingResult(matchingRound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ void operateMatchingTwice() {
operateMatching(MockTime.VALID_RECEPTION_TIME_ON_MONDAY);
saveMatchRound(MockTime.THURSDAY);
operateMatching(MockTime.VALID_RECEPTION_TIME_ON_THURSDAY);

List<MatchingResult> result = matchingResultRepository.findAll();
Assertions.assertThat(result).isNotNull();
}

@Test
Expand Down

0 comments on commit 605b02d

Please sign in to comment.