diff --git a/gateway/src/main/java/com/oing/controller/CalendarController.java b/gateway/src/main/java/com/oing/controller/CalendarController.java index 134a0799..22552623 100644 --- a/gateway/src/main/java/com/oing/controller/CalendarController.java +++ b/gateway/src/main/java/com/oing/controller/CalendarController.java @@ -91,24 +91,35 @@ public BannerResponse getBanner(String yearMonth, String familyId) { int allFamilyMembersUploadedStreaks = 0; boolean allFamilyMembersUploadedStreaked = true; - // 한 달 동안 '가족이 전부 올린 날'과 '가족이 전부 올린 날의 연속'을 계산하기 위해, 1일부터 마지막 날까지 순회한다. + // 한 달 동안 '가족이 전부 업로드한 날'과 '전부 업로드한 날의 연속'을 계산하기 위해, 1일부터 마지막 날까지 순회한다. while (startDate.isBefore(endDate)) { - List familyMembersIds = memberService.findFamilyMembersIdsByFamilyJoinAtBefore(familyId, startDate.plusDays(1)); boolean allFamilyMembersUploaded = true; - for (String memberId : familyMembersIds) { - if (!memberPostService.existsByMemberIdAndFamilyIdAndCreatedAt(memberId, familyId, startDate)) { - allFamilyMembersUploaded = false; - break; + + if (memberPostService.existsByFamilyIdAndCreatedAt(familyId, startDate)) { + List familyMembersIds = memberService.findFamilyMembersIdsByFamilyJoinAtBefore(familyId, startDate.plusDays(1)); + for (String memberId : familyMembersIds) { + if (!memberPostService.existsByMemberIdAndFamilyIdAndCreatedAt(memberId, familyId, startDate)) { + allFamilyMembersUploaded = false; + break; + } } + + } else { + // 게시글이 없다면, 계산에서 제외 + // 해당 날짜에 가족과 게시물이 없는 경우, allFamilyMembersUploaded = true 가 되는 것을 방지 + // edge case: 게시물을 하나라도 없로드 하고 글을 업로드하지 않은 회원이 탈퇴하면, allFamilyMembersUploaded이 true로 변함 -> 핸들링할 수 없는 케이스 + allFamilyMembersUploaded = false; } + + // (가족 전부 업로드한 날의 수, 연속해서 업로드한 여부, 연속해서 업로드한 날의 수) 계산 if (allFamilyMembersUploaded) { allFamilyMembersUploadedDays++; if (allFamilyMembersUploadedStreaked) - allFamilyMembersUploadedStreaks++; // 가족 전체 업로드가 연속되면, Streak + 1 + allFamilyMembersUploadedStreaks++; } else { - allFamilyMembersUploadedStreaked = false; // 가족 전체 업로드가 연속되지 못하면, Streak false + allFamilyMembersUploadedStreaked = false; } startDate = startDate.plusDays(1); diff --git a/gateway/src/main/java/com/oing/repository/MemberPostRepositoryCustomImpl.java b/gateway/src/main/java/com/oing/repository/MemberPostRepositoryCustomImpl.java index 53714e90..b45d7f98 100644 --- a/gateway/src/main/java/com/oing/repository/MemberPostRepositoryCustomImpl.java +++ b/gateway/src/main/java/com/oing/repository/MemberPostRepositoryCustomImpl.java @@ -5,6 +5,7 @@ import com.querydsl.core.types.Ops; import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.DateTimePath; import com.querydsl.core.types.dsl.Expressions; import com.querydsl.jpa.JPAExpressions; import com.querydsl.core.types.dsl.DateTimeTemplate; @@ -86,16 +87,31 @@ private BooleanExpression eqMemberId(String memberId) { } @Override - public boolean existsByMemberIdAndFamilyIdAndCreatedAt(String memberId, String familyId, LocalDate postDate) { - DateTimeTemplate createdAtDate = Expressions.dateTimeTemplate(LocalDate.class, - "DATE({0})", memberPost.createdAt); + public boolean existsByFamilyIdAndCreatedAt(String familyId, LocalDate postDate) { + return queryFactory + .select(memberPost.id) + .from(memberPost) + .where( + memberPost.familyId.eq(familyId), + dateExpr(memberPost.createdAt).eq(postDate) + ) + .fetchFirst() != null; + } + @Override + public boolean existsByMemberIdAndFamilyIdAndCreatedAt(String memberId, String familyId, LocalDate postDate) { return queryFactory .select(memberPost.id) .from(memberPost) - .where(memberPost.memberId.eq(memberId) - .and(memberPost.familyId.eq(familyId)) - .and(createdAtDate.eq(postDate))) + .where( + memberPost.memberId.eq(memberId), + memberPost.familyId.eq(familyId), + dateExpr(memberPost.createdAt).eq(postDate) + ) .fetchFirst() != null; } + + private DateTimeTemplate dateExpr(DateTimePath localDateTime) { + return Expressions.dateTimeTemplate(LocalDate.class, "DATE({0})", localDateTime); + } } diff --git a/post/src/main/java/com/oing/repository/MemberPostRepositoryCustom.java b/post/src/main/java/com/oing/repository/MemberPostRepositoryCustom.java index 20f6c39b..1ba6f6fd 100644 --- a/post/src/main/java/com/oing/repository/MemberPostRepositoryCustom.java +++ b/post/src/main/java/com/oing/repository/MemberPostRepositoryCustom.java @@ -16,5 +16,8 @@ public interface MemberPostRepositoryCustom { long countMonthlyPostByFamilyId(int year, int month, String familyId); + boolean existsByFamilyIdAndCreatedAt(String familyId, LocalDate postDate); + boolean existsByMemberIdAndFamilyIdAndCreatedAt(String memberId, String familyId, LocalDate postDate); + } diff --git a/post/src/main/java/com/oing/service/MemberPostService.java b/post/src/main/java/com/oing/service/MemberPostService.java index 441bdb5b..6f7cc773 100644 --- a/post/src/main/java/com/oing/service/MemberPostService.java +++ b/post/src/main/java/com/oing/service/MemberPostService.java @@ -94,6 +94,10 @@ public List getMemberIdsPostedToday(LocalDate date) { return memberPostRepository.getMemberIdsPostedToday(date); } + public boolean existsByFamilyIdAndCreatedAt(String familyId, LocalDate postDate) { + return memberPostRepository.existsByFamilyIdAndCreatedAt(familyId, postDate); + } + public boolean existsByMemberIdAndFamilyIdAndCreatedAt(String memberId, String familyId, LocalDate postDate) { return memberPostRepository.existsByMemberIdAndFamilyIdAndCreatedAt(memberId, familyId, postDate); }