Skip to content

Commit

Permalink
[OING-335] �fix: 가족구성원 랭킹 API의 생존신고 수가 미션 수를 포함하는 현상 해결 (#257)
Browse files Browse the repository at this point in the history
* fix: Fix wrongly written post counting query that count survival and mission post both

* refactor: Refactor the query to compare createdAt of Post by eqauals with year and month to between for index
  • Loading branch information
Kwon770 committed May 30, 2024
1 parent 42f75c3 commit a968906
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,32 @@ public QueryResults<Post> searchPosts(int page, int size, LocalDate date, String

@Override
public long countMonthlyPostByFamilyId(int year, int month, String familyId) {
LocalDateTime from = LocalDateTime.of(year, month, 1, 0, 0);
LocalDateTime to = from.plusMonths(1).minusDays(1);

return queryFactory
.select(post.count())
.from(post)
.where(post.familyId.eq(familyId),
post.createdAt.year().eq(year),
post.createdAt.month().eq(month))
.where(
post.familyId.eq(familyId),
post.createdAt.between(from, to)
)
.fetchFirst();
}

@Override
public long countMonthlyPostByMemberId(int year, int month, String memberId) {
public long countMonthlySurvivalPostByMemberId(int year, int month, String memberId) {
LocalDateTime from = LocalDateTime.of(year, month, 1, 0, 0);
LocalDateTime to = from.plusMonths(1).minusDays(1);

return queryFactory
.select(post.count())
.from(post)
.where(post.memberId.eq(memberId),
post.createdAt.year().eq(year),
post.createdAt.month().eq(month))
.where(
post.type.eq(SURVIVAL),
post.memberId.eq(memberId),
post.createdAt.between(from, to)
)
.fetchFirst();
}

Expand Down
2 changes: 1 addition & 1 deletion post/src/main/java/com/oing/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public ArrayResponse<PostRankerResponse> getFamilyMembersMonthlySurvivalRanking(

List<PostRankerResponse> postRankerResponses = familyMembersIds.stream().map(familyMemberId -> new PostRankerDTO(
familyMemberId,
postService.countMonthlyPostByMemberId(dateTime, familyMemberId),
postService.countMonthlySurvivalPostByMemberId(dateTime, familyMemberId),
commentService.countMonthlyCommentByMemberId(dateTime, familyMemberId),
reactionService.countMonthlyReactionByMemberId(dateTime, familyMemberId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ QueryResults<Post> searchPosts(int page, int size, LocalDate date, String member

long countMonthlyPostByFamilyId(int year, int month, String familyId);

long countMonthlyPostByMemberId(int year, int month, String memberId);
long countMonthlySurvivalPostByMemberId(int year, int month, String memberId);

boolean existsByFamilyIdAndCreatedAt(String familyId, LocalDate postDate);

Expand Down
4 changes: 2 additions & 2 deletions post/src/main/java/com/oing/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ public int calculateRemainingSurvivalPostCountUntilMissionUnlocked(String family
return Math.max(requiredSurvivalPostCount - todaySurvivalPostCount, 0);
}

public long countMonthlyPostByMemberId(LocalDate date, String memberId) {
public long countMonthlySurvivalPostByMemberId(LocalDate date, String memberId) {
int year = date.getYear();
int month = date.getMonthValue();

return postRepository.countMonthlyPostByMemberId(year, month, memberId);
return postRepository.countMonthlySurvivalPostByMemberId(year, month, memberId);
}

public boolean isCreatedSurvivalPostToday(String memberId, String familyId) {
Expand Down

0 comments on commit a968906

Please sign in to comment.