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

Feat: 챌린지 랜덤 기능 구현 #293

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -8,7 +8,10 @@

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import static com.dnd.runus.global.constant.TimeConstant.SERVER_TIMEZONE_ID;

Expand All @@ -26,15 +29,22 @@ public List<ChallengesResponse> getChallenges(long memberId) {
.toOffsetDateTime();
OffsetDateTime yesterday = todayMidnight.minusDays(1);

List<ChallengesResponse> challengesResponses;
// 어제 기록이 없으면
if (!runningRecordRepository.hasByMemberIdAndStartAtBetween(memberId, yesterday, todayMidnight)) {
return challengeRepository.findAllIsNotDefeatYesterday().stream()
challengesResponses = challengeRepository.findAllIsNotDefeatYesterday().stream()
.map(ChallengesResponse::from)
.toList();
.collect(Collectors.toList());
} else {
challengesResponses = challengeRepository.findAllChallenges().stream()
.map(ChallengesResponse::from)
.collect(Collectors.toList());
}

return challengeRepository.findAllChallenges().stream()
.map(ChallengesResponse::from)
.toList();
// 랜덤으로 2개 리턴
Random randomWithSeed = new Random(todayMidnight.toEpochSecond());
Collections.shuffle(challengesResponses, randomWithSeed);

return challengesResponses.subList(0, 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import java.util.List;

import static com.dnd.runus.global.constant.TimeConstant.SERVER_TIMEZONE_ID;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;

@ExtendWith(MockitoExtension.class)
class ChallengeDataServiceTest {
class ChallengeServiceTest {

@Mock
private RunningRecordRepository runningRecordRepository;
Expand All @@ -44,7 +44,7 @@ void setUp() {
member = new Member(MemberRole.USER, "nickname");
}

@DisplayName("어제 기록이 있는경우 챌린지 리스트 조회 : 챌린지 name에 '어제'값이 포함한 값이 있어야함")
@DisplayName("어제 기록이 있는경우 챌린지 리스트 조회 : 챌린지 리스트 크기가 2이어야함")
@Test
void getChallengesWithYesterdayRecords() {
// given
Expand All @@ -65,10 +65,10 @@ void getChallengesWithYesterdayRecords() {
List<ChallengesResponse> challenges = challengeService.getChallenges(member.memberId());

// then
assertTrue(challenges.stream().anyMatch(c -> c.title().contains("어제")));
assertThat(challenges.size()).isEqualTo(2);
}

@DisplayName("어제 기록이 없는 경우 챌린지 리스트 조회 : 챌린지 name에 '어제'값이 포함한 값이 없어야함")
@DisplayName("어제 기록이 없는 경우 챌린지 리스트 조회 : 챌린지 리스트 크기가 2이어야함")
@Test
void getChallengesWithoutYesterdayRecords() {
// given
Expand All @@ -85,6 +85,6 @@ void getChallengesWithoutYesterdayRecords() {
List<ChallengesResponse> challenges = challengeService.getChallenges(member.memberId());

// then
assertTrue(challenges.stream().noneMatch(c -> c.title().contains("어제")));
assertThat(challenges.size()).isEqualTo(2);
}
}