From 6b8dc692641f92866a8271267abd0c0daa33c6ec Mon Sep 17 00:00:00 2001 From: dongyeoppp Date: Wed, 10 Jul 2024 04:27:45 +0900 Subject: [PATCH] fix: get challenge modify --- .../controller/ChallengeController.java | 8 +++--- .../namanmoo/service/AnswerService.java | 25 +++++++++++++++++-- .../namanmoo/service/ChallengeService.java | 2 +- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main/java/ongjong/namanmoo/controller/ChallengeController.java b/src/main/java/ongjong/namanmoo/controller/ChallengeController.java index dccbd38..3eaf92b 100644 --- a/src/main/java/ongjong/namanmoo/controller/ChallengeController.java +++ b/src/main/java/ongjong/namanmoo/controller/ChallengeController.java @@ -95,7 +95,7 @@ public ApiResponse getNormalChallenge(@RequestParam("challen Member member = memberService.findMemberByLoginId(); // 로그인한 멤버 찾기 boolean isComplete = answerService.findIsCompleteAnswer(challenge, member); Long challengeDate = answerService.findDateByChallengeMember(challenge); - List answers = answerService.findAnswerByChallenge(challenge); + List answers = answerService.findAnswerByChallengeandFamily(challenge, member); NormalChallengeDto normalChallengeDto = new NormalChallengeDto(challenge, isComplete, challengeDate, answers); @@ -123,7 +123,7 @@ public ApiResponse getGroupChallenge(@RequestParam("challenge Long challengeDate = answerService.findDateByChallengeMember(challenge); Member member = memberService.findMemberByLoginId(); // 로그인한 멤버 찾기 boolean isComplete = answerService.findIsCompleteAnswer(challenge, member); - List allAnswers = answerService.findAnswersByChallenges(challenge); // 특정 그룹 챌린지에 매핑된 answer list 찾기 + List allAnswers = answerService.findAnswersByChallenges(challenge, member); // 특정 그룹 챌린지에 매핑된 answer list 찾기 GroupChallengeDto groupChallengeDto = new GroupChallengeDto(challenge, challengeDate, isComplete, allAnswers); return new ApiResponse<>("success", "Challenge retrieved successfully", groupChallengeDto); @@ -153,7 +153,7 @@ public ApiResponse getPhotoChallenge(@RequestParam("challenge boolean isComplete = answerService.findIsCompleteAnswer(challenge, member); Long challengeDate = answerService.findDateByChallengeMember(challenge); - List answers = answerService.findAnswerByChallenge(challenge); + List answers = answerService.findAnswerByChallengeandFamily(challenge, member); PhotoChallengeDto photoChallengeDto = new PhotoChallengeDto(challenge, isComplete, challengeDate, answers); @@ -328,7 +328,7 @@ public ApiResponse getVoiceChallenge(@RequestParam("challenge Member member = memberService.findMemberByLoginId(); // 로그인한 멤버 찾기 boolean isComplete = answerService.findIsCompleteAnswer(challenge, member); Long challengeDate = answerService.findDateByChallengeMember(challenge); - List answers = answerService.findAnswerByChallenge(challenge); + List answers = answerService.findAnswerByChallengeandFamily(challenge, member); VoiceChallengeDto voiceChallengeDto = new VoiceChallengeDto(challenge, isComplete, challengeDate, answers); diff --git a/src/main/java/ongjong/namanmoo/service/AnswerService.java b/src/main/java/ongjong/namanmoo/service/AnswerService.java index fe8f368..66642e6 100644 --- a/src/main/java/ongjong/namanmoo/service/AnswerService.java +++ b/src/main/java/ongjong/namanmoo/service/AnswerService.java @@ -20,8 +20,10 @@ import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; @Slf4j @Service @@ -141,6 +143,19 @@ public List findAnswerByChallenge(Challenge challenge){ return answerRepository.findByChallenge(challenge); } + // 해당 챌린지와 매핑된 답변 중 로그인 하고 있는 맴버 가족의 답변리스트만을 반환 + @Transactional(readOnly = true) + public List findAnswerByChallengeandFamily(Challenge challenge, Member member){ + Family family = member.getFamily(); + // answers 리스트를 순회하면서 answer의 memberid값을 member를 구하고 그 member가 가지고 있는 familyid가 위에서 구한 familyid와 다를 경우 answers에서 제거해야해 + return answerRepository.findByChallenge(challenge).stream() + .filter(answer -> { + Member answerMember = answer.getMember(); + return answerMember != null && family.getFamilyId().equals(answerMember.getFamily().getFamilyId()); + }) + .collect(Collectors.toList()); + } + // 가족 구성원들의 답변 유무 검사 @Transactional(readOnly = true) public boolean isAnyAnswerComplete(Challenge challenge, Family family) { @@ -226,14 +241,20 @@ public void offBalloon(Long challengeDate) throws Exception { } @Transactional(readOnly = true) - public List findAnswersByChallenges(Challenge challenge) { // 특정 그룹 챌린지에 매핑된 answer list 찾기 + public List findAnswersByChallenges(Challenge challenge, Member member) { // 특정 그룹 챌린지에 매핑된 answer list 찾기 List groupChallenges = challengeService.findChallengesByChallengeNum(challenge.getChallengeNum()); // challengeNum이 같은 챌린지 찾기 + Family family = member.getFamily(); List allAnswers = new ArrayList<>(); // 해당 그룹질문으로 묶인 answer 가져오기 for (Challenge relatedChallenge : groupChallenges) { List answers = findAnswerByChallenge(relatedChallenge); allAnswers.addAll(answers); } - return allAnswers; + return allAnswers.stream() + .filter(answer -> { + Member answerMember = answer.getMember(); + return answerMember != null && family.getFamilyId().equals(answerMember.getFamily().getFamilyId()); + }) + .collect(Collectors.toList()); } } diff --git a/src/main/java/ongjong/namanmoo/service/ChallengeService.java b/src/main/java/ongjong/namanmoo/service/ChallengeService.java index 4fe9bc8..8f86b1b 100644 --- a/src/main/java/ongjong/namanmoo/service/ChallengeService.java +++ b/src/main/java/ongjong/namanmoo/service/ChallengeService.java @@ -46,7 +46,7 @@ public Long findCurrentChallengeNum(Long familyId, Long challengeDate) { / for (Lucky lucky : luckies) { if (lucky.isRunning()) { DateUtil dateUtil = DateUtil.getInstance(); - return dateUtil.getDateDifference(lucky.getChallengeStartDate(), dateUtil.timestampToString(challengeDate))+1; // 현재 진행되어야할 challenge를 반환 + return dateUtil.getDateDifference(lucky.getChallengeStartDate(), dateUtil.timestampToString(challengeDate)); // 현재 진행되어야할 challenge를 반환 } } return null;