Igloo-Club / Igloo-Club-BE

1 stars 1 forks source link

[fix] 눈길 매칭 기능 날짜 추가 & 요청 시마다 요일과 날짜 계산 #77

Closed ryuseunghan closed 4 months ago

ryuseunghan commented 4 months ago

🔥 Related Issues

💜 작업 내용

✅ PR Point

- `MatchYoilAndTimeResponse` DTO 를 이용해서  `findCommonYoil`, `findNextYoil`을 `yoil`만 반환하는 메서드에서 `matchYoil`과 `matchDate`(추천 만남 날짜)를 반환하도록 변경
MatchYoilAndTimeResponse.java

@Getter @AllArgsConstructor @Builder public class MatchYoilAndTimeResponse { private Yoil matchYoil;

@JsonFormat(pattern = "yyyyMMdd")
private LocalDate matchDate;

}

findCommonYoil.java
public MatchYoilAndTimeResponse findCommonYoil(Member member1, Member member2) {
    ,,,,
    for (Yoil yoil : list2) {
        if (set1.contains(yoil.getDayOfWeek())) {
            if (isAfter11AM) {
                return findNextYoil(yoil, now, set1);
            } else {
                //해당 부분을 통해 `matchYoil`에 해당되는 날짜를 계산
                int offset = (7+yoil.getDayOfWeek().getValue()-now.getDayOfWeek().getValue())%7;
                return new MatchYoilAndTimeResponse(yoil, LocalDate.now().plusDays(offset));
            }
        }
    }
    return null;
}
findNextYoil.java
private MatchYoilAndTimeResponse findNextYoil(Yoil yoil, LocalDateTime now, Set<DayOfWeek> availableDays) {
    DayOfWeek today = now.getDayOfWeek();
    int offset = 0;
    DayOfWeek nextDay = today;

    do {
        offset++;
        nextDay = DayOfWeek.of((today.getValue() + offset - 1) % 7 + 1); // 요일을 순환
    } while (!availableDays.contains(nextDay)); // 다음 사용 가능한 요일을 찾을 때까지 반복

    for (Yoil nextYoil : Yoil.values()) {
        if (nextYoil.getDayOfWeek().equals(nextDay)) {
            //해당 부분을 통해 `matchYoil`에 해당되는 날짜를 계산
            return new MatchYoilAndTimeResponse(nextYoil, LocalDate.now().plusDays(offset));
        }
    }
    //해당 부분을 통해 `matchYoil`에 해당되는 날짜를 계산
    return new MatchYoilAndTimeResponse(yoil, LocalDate.now().plusDays(offset));
}
- `NungilMatchResponse` 필드 추가, create 메서드 파라미터 수정
- `NungilMatchResponse` 변경에 따라 `getMatchedNungil ` 수정

public class NungilMatchResponse {

@Nullable
private String matchYoil;

@JsonFormat(pattern = "yyyyMMdd")
private LocalDate matchDate;

,,,,,

public static NungilMatchResponse create(Nungil nungil, Long chatRoomId, Yoil matchYoil, LocalDate matchDate) {

    NungilMatchResponse response = new NungilMatchResponse();

    response.matchYoil = (matchYoil != null) ? matchYoil.getTitle() : null;

    response.matchDate = (matchYoil != null) ? matchDate : null;

    ,,,,,,

    return response;
}

}

public NungilMatchResponse getMatchedNungil(Long nungilId, Member member){

    ,,,,,
    // 두 사용자의 공통되는 요일과 그에 해당되는 일자
    MatchYoilAndTimeResponse response = findCommonYoil(nungil.getMember(), nungil.getReceiver());

    Yoil matchYoil = (response != null) ? response.getMatchYoil() : null;
    LocalDate matchDate = (response != null) ? response.getMatchDate() : null;

    return NungilMatchResponse.create(nungil, chatRoomId, matchYoil, matchDate);
}

## 😡 Trouble Shooting
- `response.getMatchYoil() `로 인해 매칭 되는 요일이 없을 때 NullPointerException 발생

User java.lang.NullPointerException: Cannot invoke "com.iglooclub.nungil.dto.MatchYoilAndTimeResponse.getMatchYoil()" because "response" is null

- `Yoil matchYoil = (response.getMatchYoil() != null) ? response.getMatchYoil() : null; ` 해당 코드를 아래와 같이 수정

Yoil matchYoil = (response != null) ? response.getMatchYoil() : null; LocalDate matchDate = (response != null) ? response.getMatchDate() : null;


## ☀ 스크린샷 / GIF / 화면 녹화
case 1 ) 매칭되는 요일이 없을 때
![image](https://github.com/Igloo-Club/Igloo-Club-BE/assets/106146847/715df40b-9f36-4b4d-8a3c-60d426b11010)
case 2 ) 오늘이 아닌 매칭되는 요일이 존재할 때
![image](https://github.com/Igloo-Club/Igloo-Club-BE/assets/106146847/1053483a-c511-486b-b03a-4c1e35b8ffc7)
case 3 ) 11시 이후에 매칭되는 요일이 존재하며 해당 요일이 오늘일 때(현재 요일 일요일, 매칭되는 요일은 일요일/월요일)
![image](https://github.com/Igloo-Club/Igloo-Club-BE/assets/106146847/368bf318-2c2b-4f04-9c18-723921983d18)