Skip to content

Commit

Permalink
Merge pull request #69 from hellokitty-coding-club/feature/#68-home-a…
Browse files Browse the repository at this point in the history
…pi-simple-fix
  • Loading branch information
great-park authored Aug 23, 2023
2 parents 05bc8fd + c16da1c commit 74e5f46
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ public class MissionItemHolder {

private final MissionService missionService;

public Function<Long, MissionContentData> getMissionListFunction(MissionContentType missionContentType) {
public Function<Long, MissionContentData> getMissionListFunction(MissionContentType missionContentType, String memberType) {
return switch (missionContentType) {
case TOTAL_MISSION_LIST_V1 -> missionService::getTotalMissions;
case ON_GOING_MISSION_LIST_V1 -> missionService::getOngoingMissions;
case RECOMMENDED_MISSION_LIST_V1 -> missionService::getRecommendMissions;
case ON_GOING_MISSION_LIST_V1 -> switch (memberType) {
case "JUNIOR" -> missionService::getJuniorOngoingMissions;
case "SENIOR" -> missionService::getSeniorOngoingMissions;
default -> null;
};
default -> null;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.util.List;

public interface MissionCustomRepository {
List<Mission> getOnGoingMissions(Long memberId);
List<Mission> getJuniorOnGoingMissions(Long memberId);

List<Mission> getSeniorOngoingMissions(Long memberId);

List<Mission> getRecommendedMissions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ public class MissionCustomRepositoryImpl implements MissionCustomRepository {

@Override
@Cacheable(value = "on_going_missions", key = "#memberId")
public List<Mission> getOnGoingMissions(Long memberId) {
public List<Mission> getJuniorOnGoingMissions(Long memberId) {
return getMissions(isMemberParticipating(memberId), isNotCompleted());
}

@Override
@Cacheable(value = "on_going_missions", key = "#memberId")
public List<Mission> getSeniorOngoingMissions(Long memberId) {
return getMissions(memberId, isMissionNotFinished());
}

@Override // todo: get recommended missions
public List<Mission> getRecommendedMissions () {
return null;
Expand All @@ -47,6 +53,17 @@ private List<Mission> getMissions(BooleanExpression isParticipating, BooleanExpr
.join(missionRegistration).on(mission.missionId.eq(missionRegistration.mission.missionId))
.join(member).on(member.memberId.eq(missionRegistration.junior.memberId))
.where(isParticipating.and(isNotCompleted))
.orderBy(mission.createdAt.desc())
.limit(3)
.fetch();
}

private List<Mission> getMissions(Long memberId, BooleanExpression isMissionNotFinished) {
return jpaQueryFactory
.select(mission)
.from(mission)
.where(isMissionNotFinished.and(isWriterMatchingMember(memberId)))
.orderBy(mission.createdAt.desc())
.limit(3)
.fetch();
}
Expand All @@ -56,6 +73,7 @@ private List<Mission> getMissions(BooleanExpression isMissionNotFinished) {
.select(mission)
.from(mission)
.where(isMissionNotFinished)
.orderBy(mission.createdAt.desc())
.limit(3)
.fetch();
}
Expand All @@ -71,4 +89,9 @@ private BooleanExpression isNotCompleted() {
private BooleanExpression isMissionNotFinished() {
return mission.missionStatus.ne(MissionStatus.MISSION_FINISHED);
}

private BooleanExpression isWriterMatchingMember(Long memberId) {
return mission.writer.memberId.eq(memberId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import swm.hkcc.LGTM.app.modules.member.service.MemberService;
import swm.hkcc.LGTM.app.modules.mission.constant.MissionContentType;
import swm.hkcc.LGTM.app.modules.mission.domain.MissionContentData;
import swm.hkcc.LGTM.app.modules.mission.domain.MissionContentSequence;
Expand All @@ -26,6 +27,7 @@
public class HomeServiceImpl implements HomeService{
private final HomeServerDrivenUISequenceFactory sequenceFactory;
private final MissionItemHolder missionItemHolder;
private final MemberService memberService;

private static final String RESPONSE_SCREEN_NAME = "Home";

Expand All @@ -43,7 +45,8 @@ public ServerDrivenScreenResponse getHomeScreen(Long memberId, int version) {

private void processMissionContentType(Long memberId, MissionContentType missionContentType, List<ServerDrivenContent> serverDrivenContentList) {
if (missionContentType.getViewType() == ViewType.ITEM) {
Function<Long, MissionContentData> missionListFunction = missionItemHolder.getMissionListFunction(missionContentType);
String memberType = memberService.getMemberType(memberId);
Function<Long, MissionContentData> missionListFunction = missionItemHolder.getMissionListFunction(missionContentType, memberType);
MissionContentData missionContentData = missionListFunction.apply(memberId);

ServerDrivenContents missionContents = ServerDrivenContents.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import swm.hkcc.LGTM.app.modules.mission.dto.MissionDetailViewResponse;

public interface MissionService {
MissionContentData getOngoingMissions(Long memberId);
MissionContentData getJuniorOngoingMissions(Long memberId);

MissionContentData getSeniorOngoingMissions(Long memberId);

MissionContentData getRecommendMissions(Long memberId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,22 @@ public class MissionServiceImpl implements MissionService {
private final MemberService memberService;

@Override
public MissionContentData getOngoingMissions(Long memberId) {
List<Mission> missions = missionRepository.getOnGoingMissions(memberId);
public MissionContentData getJuniorOngoingMissions(Long memberId) {
List<Mission> missions = missionRepository.getJuniorOnGoingMissions(memberId);

return MissionContentData.of(
missions.stream()
.map(mission -> MissionMapper.missionToMissionDto(
mission,
techTagPerMissionRepository.findTechTagsByMissionId(mission.getMissionId())
))
.toList()
);
}

@Override
public MissionContentData getSeniorOngoingMissions(Long memberId) {
List<Mission> missions = missionRepository.getSeniorOngoingMissions(memberId);

return MissionContentData.of(
missions.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ void getMissionListFunction() {
MissionContentType totalMissionContentType = MissionContentType.TOTAL_MISSION_LIST_V1;
MissionContentType onGoingMissionContentType = MissionContentType.ON_GOING_MISSION_LIST_V1;
MissionContentType recommendedMissionContentType = MissionContentType.RECOMMENDED_MISSION_LIST_V1;

String memberType = "JUNIOR";
// Simulate the expected behavior of the missionService mock
MissionContentData<MissionDto> onGoingMissionData = Mockito.mock(MissionContentData.class);
MissionContentData<MissionDetailsDto> totalMissionData = Mockito.mock(MissionContentData.class);
MissionContentData<MissionDetailsDto> recommendedMissionData = Mockito.mock(MissionContentData.class);
when(missionService.getTotalMissions(anyLong())).thenReturn(totalMissionData);
when(missionService.getOngoingMissions(anyLong())).thenReturn(onGoingMissionData);
when(missionService.getJuniorOngoingMissions(anyLong())).thenReturn(onGoingMissionData);
when(missionService.getRecommendMissions(anyLong())).thenReturn(recommendedMissionData);

// when
Function<Long, MissionContentData> totalMissionListFunction = missionItemHolder.getMissionListFunction(totalMissionContentType);
Function<Long, MissionContentData> onGoingMissionListFunction = missionItemHolder.getMissionListFunction(onGoingMissionContentType);
Function<Long, MissionContentData> recommendedMissionListFunction = missionItemHolder.getMissionListFunction(recommendedMissionContentType);
Function<Long, MissionContentData> totalMissionListFunction = missionItemHolder.getMissionListFunction(totalMissionContentType, memberType);
Function<Long, MissionContentData> onGoingMissionListFunction = missionItemHolder.getMissionListFunction(onGoingMissionContentType, memberType);
Function<Long, MissionContentData> recommendedMissionListFunction = missionItemHolder.getMissionListFunction(recommendedMissionContentType, memberType);

// then
assertThat(totalMissionListFunction).isNotNull();
Expand All @@ -67,7 +67,7 @@ void getMissionListFunction() {

// Verify that the missionService methods are called with the correct arguments
verify(missionService).getTotalMissions(1L);
verify(missionService).getOngoingMissions(2L);
verify(missionService).getJuniorOngoingMissions(2L);
verify(missionService).getRecommendMissions(3L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void getOnGoingMissions() {
Long memberId = member.getMemberId();

// When
List<Mission> onGoingMissions = missionRepository.getOnGoingMissions(memberId);
List<Mission> onGoingMissions = missionRepository.getJuniorOnGoingMissions(memberId);

// Then
assertThat(onGoingMissions).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import swm.hkcc.LGTM.app.modules.member.service.MemberService;
import swm.hkcc.LGTM.app.modules.mission.constant.MissionContentType;
import swm.hkcc.LGTM.app.modules.mission.domain.MissionContentData;
import swm.hkcc.LGTM.app.modules.mission.domain.MissionContentSequence;
Expand Down Expand Up @@ -35,6 +36,9 @@ class HomeServiceImplTest {
@Mock
private MissionService missionService;

@Mock
private MemberService memberService;

@InjectMocks
private HomeServiceImpl homeService;

Expand All @@ -48,6 +52,7 @@ void getHomeScreen_WithNonEmptyContents() {
// given
Long memberId = 1L;
int version = 1;
String memberType = "JUNIOR";

List<MissionContentType> mockMissionContentTypeList = List.of(
MissionContentType.ON_GOING_MISSION_TITLE_V1,
Expand All @@ -62,17 +67,18 @@ void getHomeScreen_WithNonEmptyContents() {
);
MissionContentSequence mockContentSequence = new MissionContentSequence(mockMissionContentTypeList);
when(sequenceFactory.getServerDrivenUISequence(version)).thenReturn(mockContentSequence);
when(memberService.getMemberType(memberId)).thenReturn(memberType);

MissionContentData ongoingMissionContent = MissionContentData.of(List.of(createMockMissionDto(), createMockMissionDto()));
MissionContentData recommendMissionContent = MissionContentData.of(List.of(createMockMissionDetailsDto(), createMockMissionDetailsDto()));
MissionContentData totalMissionContent = MissionContentData.of(List.of(createMockMissionDetailsDto(), createMockMissionDetailsDto()));
when(missionService.getOngoingMissions(anyLong())).thenReturn(ongoingMissionContent);
when(missionService.getJuniorOngoingMissions(anyLong())).thenReturn(ongoingMissionContent);
when(missionService.getRecommendMissions(anyLong())).thenReturn(recommendMissionContent);
when(missionService.getTotalMissions(anyLong())).thenReturn(totalMissionContent);

when(missionItemHolder.getMissionListFunction(ON_GOING_MISSION_LIST_V1)).thenReturn(missionService::getOngoingMissions);
when(missionItemHolder.getMissionListFunction(RECOMMENDED_MISSION_LIST_V1)).thenReturn(missionService::getRecommendMissions);
when(missionItemHolder.getMissionListFunction(TOTAL_MISSION_LIST_V1)).thenReturn(missionService::getTotalMissions);
when(missionItemHolder.getMissionListFunction(ON_GOING_MISSION_LIST_V1, memberType)).thenReturn(missionService::getJuniorOngoingMissions);
when(missionItemHolder.getMissionListFunction(RECOMMENDED_MISSION_LIST_V1, memberType)).thenReturn(missionService::getRecommendMissions);
when(missionItemHolder.getMissionListFunction(TOTAL_MISSION_LIST_V1, memberType)).thenReturn(missionService::getTotalMissions);


// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public void setUp() {
@DisplayName("진행중인 미션을 가져온다. MissionDto의 리스트를 담은 MissionContentData를 반환한다.")
public void getOngoingMissions() {
// Given
when(missionRepository.getOnGoingMissions(1L))
when(missionRepository.getJuniorOnGoingMissions(1L))
.thenReturn(Arrays.asList(createMockMission(1L), createMockMission(2L)));

// When
MissionContentData result = missionService.getOngoingMissions(1L);
MissionContentData result = missionService.getJuniorOngoingMissions(1L);

// Then
assertEquals(2, result.getMissionData().size());
Expand Down

0 comments on commit 74e5f46

Please sign in to comment.