Skip to content

Commit

Permalink
[OING-320] refactor: 가족 중 몇명이 더 올리면 미션 참여 가능한지 반환하는 API 로직 리팩터링 (#242)
Browse files Browse the repository at this point in the history
* refactor: refact calculateRemainingSurvivalPostCountUntilMissuonUnlocked logic

* feature: add isActiveMember method and refact code

* test: refact test code due to seperated logic

* test: add calculateRemainingSurvivalPostCountUntilMissionUnlocked test code
  • Loading branch information
Ji-soo708 authored Apr 24, 2024
1 parent 7f36605 commit be7db36
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId)
.select(member.count())
.from(member)
.where(member.familyId.eq(familyId)
.and(member.deletedAt.isNull()))
.and(isActiveMember()))
.fetchFirst();

long survivalPostCount = queryFactory
Expand All @@ -139,27 +139,32 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId)
return survivalPostCount >= totalFamilyMembers / 2;
}

public int calculateRemainingSurvivalPostCount(String familyId) {
Long familyMemberCount = queryFactory
@Override
public int countFamilyMembersByFamilyId(String familyId) {
Long count = queryFactory
.select(member.id.count())
.from(member)
.where(member.familyId.eq(familyId)
.and(member.deletedAt.isNull()))
.and(isActiveMember()))
.fetchFirst();
return count.intValue();
}

@Override
public int countTodaySurvivalPostsByFamilyId(String familyId) {
LocalDate today = ZonedDateTime.now().toLocalDate();
Long todaySurvivalCount = queryFactory
Long count = queryFactory
.select(post.id.count())
.from(post)
.where(post.familyId.eq(familyId),
post.type.eq(PostType.SURVIVAL),
dateExpr(post.createdAt).eq(today))
.fetchFirst();
return count.intValue();
}

int requiredSurvivalPostCount = familyMemberCount != null ? (int) Math.floor(familyMemberCount / 2.0) : 0;
int todaySurvivalPostCount = todaySurvivalCount != null ? todaySurvivalCount.intValue() : 0;

return Math.max(requiredSurvivalPostCount - todaySurvivalPostCount, 0);
private BooleanExpression isActiveMember() {
return member.deletedAt.isNull();
}

private DateTimeTemplate<LocalDate> dateExpr(DateTimePath<LocalDateTime> localDateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,27 @@ void setup() {
assertThat(exists).isFalse();
}

void 가족_구성원_수가_짝수인_경우_남은_생존게시글_업로드_수를_확인한다() {
@Test
void 해당_가족_구성원이_오늘_올린_생존신고_게시글_수를_조회한다() {
// given
String familyId = testMember1.getFamilyId();

// when
int remainingSurvivalPostCount = postRepositoryCustomImpl.calculateRemainingSurvivalPostCount(familyId);
int todaySurvivalPostCount = postRepositoryCustomImpl.countTodaySurvivalPostsByFamilyId(familyId);

// then
assertThat(remainingSurvivalPostCount).isEqualTo(1);
assertThat(todaySurvivalPostCount).isEqualTo(0);
}

@Test
void 가족_구성원_수가_홀수인_경우_남은_생존게시글_업로드_수를_확인한다() {
void 가족_구성원_수를_조회한다() {
// given
String familyId = testMember3.getFamilyId();
String familyId = testMember1.getFamilyId();

// when
int remainingSurvivalPostCount = postRepositoryCustomImpl.calculateRemainingSurvivalPostCount(familyId);
int familyMemberCount = postRepositoryCustomImpl.countFamilyMembersByFamilyId(familyId);

// then
assertThat(remainingSurvivalPostCount).isEqualTo(0);
assertThat(familyMemberCount).isEqualTo(2);
}
}
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 @@ -116,7 +116,7 @@ public MissionAvailableStatusResponse getMissionAvailableStatus(String memberId,
public RemainingSurvivalPostCountResponse getRemainingSurvivalPostCount(String memberId, String loginMemberId, String loginFamilyId) {
validateMemberId(loginMemberId, memberId);

int remainingSurvivalPostCount = postService.calculateRemainingSurvivalPostCount(loginFamilyId);
int remainingSurvivalPostCount = postService.calculateRemainingSurvivalPostCountUntilMissionUnlocked(loginFamilyId);
return new RemainingSurvivalPostCountResponse(remainingSurvivalPostCount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ QueryResults<Post> searchPosts(int page, int size, LocalDate date, String member
boolean existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(String memberId, String familyId, PostType type, LocalDate postDate);

boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId);
int calculateRemainingSurvivalPostCount(String familyId);

int countFamilyMembersByFamilyId(String familyId);

int countTodaySurvivalPostsByFamilyId(String familyId);
}
8 changes: 6 additions & 2 deletions post/src/main/java/com/oing/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId)
return postRepository.isCreatedSurvivalPostByMajority(date, familyId);
}

public int calculateRemainingSurvivalPostCount(String familyId) {
return postRepository.calculateRemainingSurvivalPostCount(familyId);
public int calculateRemainingSurvivalPostCountUntilMissionUnlocked(String familyId) {
int familyMemberCount = postRepository.countFamilyMembersByFamilyId(familyId);
int requiredSurvivalPostCount = familyMemberCount / 2;
int todaySurvivalPostCount = postRepository.countTodaySurvivalPostsByFamilyId(familyId);

return Math.max(requiredSurvivalPostCount - todaySurvivalPostCount, 0);
}
}
51 changes: 51 additions & 0 deletions post/src/test/java/com/oing/service/PostServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.oing.service;

import com.oing.domain.Emoji;
import com.oing.domain.Post;
import com.oing.domain.PostType;
import com.oing.domain.Reaction;
import com.oing.dto.request.PostReactionRequest;
import com.oing.repository.PostRepository;
import com.oing.repository.ReactionRepository;
import com.oing.util.IdentityGenerator;
import com.oing.util.PreSignedUrlGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class PostServiceTest {

@InjectMocks
private PostService postService;

@Mock
private PostRepository postRepository;
@Mock
private ApplicationEventPublisher applicationEventPublisher;
@Mock
private PreSignedUrlGenerator preSignedUrlGenerator;
@Mock
private MissionBridge missionBridge;
@Mock
private IdentityGenerator identityGenerator;

@Test
void 미션_키_획득까지_남은_생존신고_게시글_업로드_수_조회_테스트() {
//given
String familyId = "1";

//when
when(postRepository.countFamilyMembersByFamilyId(familyId)).thenReturn(1);
when(postRepository.countTodaySurvivalPostsByFamilyId(familyId)).thenReturn(0);

//then
assertEquals(0, postService.calculateRemainingSurvivalPostCountUntilMissionUnlocked(familyId));
}
}

0 comments on commit be7db36

Please sign in to comment.