Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

최신 알림 존재 여부 및 읽기 기능 구현 #783

Merged
merged 13 commits into from
Oct 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ private LocalDateTime getLatestAlarmCreatedAt(
final Optional<Alarm> maybeAlarm,
final Optional<ReportActionAlarm> maybeReportActionAlarm
) {
if (maybeAlarm.isPresent()) {
return maybeAlarm.get().getCreatedAt();
}
if (maybeReportActionAlarm.isPresent()) {
if (maybeAlarm.isEmpty()) {
return maybeReportActionAlarm.get().getCreatedAt();
}
if (maybeReportActionAlarm.isEmpty()) {
return maybeAlarm.get().getCreatedAt();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybeAlarm과 maybeReportActionAlarm이 둘 다 비어있으면

return maybeReportActionAlarm.get().getCreatedAt();

이 부분에서 예외가 터질 것 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

말씀해주신 부분은 public메서드에서 검증해주고 있습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위에 메서드에서 진행하고 있었군요.

그러면 hasLatestAlarm, getLatestAlarmCreatedAt 메서드 포함해서

private boolean hasLatestAlarm(final Member member) {
    final Optional<Alarm> maybeAlarm = alarmRepository.findByMemberOrderByCreatedAtDesc(member);
    final Optional<ReportActionAlarm> maybeReportActionAlarm =
            reportActionAlarmRepository.findByMemberOrderByCreatedAtDesc(member);
    
    LocalDateTime latestAlarmCreatedAt = getLatestAlarmCreatedAt(maybeAlarm, maybeReportActionAlarm);
    return latestAlarmCreatedAt != null && member.hasLatestAlarmCompareTo(latestAlarmCreatedAt);
}

private LocalDateTime getLatestAlarmCreatedAt(
        final Optional<Alarm> maybeAlarm,
        final Optional<ReportActionAlarm> maybeReportActionAlarm
) {
    return Stream.of(maybeAlarm.map(Alarm::getCreatedAt), maybeReportActionAlarm.map(ReportActionAlarm::getCreatedAt))
            .filter(Optional::isPresent)
            .map(Optional::get)
            .max(LocalDateTime::compareTo)
            .orElse(null);
}

이런 식으로 바꿔보는 것은 어떨까요?? if문이 너무 많아서 stream으로 처리하게 해보았습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private boolean hasLatestAlarm(final Member member) {
    final Optional<Alarm> maybeAlarm = alarmRepository.findByMemberOrderByIdDesc(member);
    final Optional<ReportActionAlarm> maybeReportActionAlarm =
            reportActionAlarmRepository.findByMemberOrderByIdDesc(member);
    final List<Optional<LocalDateTime>> maybeCreatedAts = List.of(
            maybeAlarm.map(Alarm::getCreatedAt),
            maybeReportActionAlarm.map(ReportActionAlarm::getCreatedAt)
    );

    return getLatestAlarmCreatedAt(maybeCreatedAts, member);
}

private boolean getLatestAlarmCreatedAt(
        final List<Optional<LocalDateTime>> maybeCreatedAts,
        final Member member
) {
    return maybeCreatedAts.stream()
            .filter(Optional::isPresent)
            .map(Optional::get)
            .anyMatch(member::hasLatestAlarmCompareTo);
}

그렇게해서 만들어진 레전드 로직...

final Alarm alarm = maybeAlarm.get();
final ReportActionAlarm reportActionAlarm = maybeReportActionAlarm.get();
return alarm.getLatestAlarmCreatedAt(reportActionAlarm.getCreatedAt());
Expand Down