From e1c897596c25882a2f976bfe17b69064be8e6521 Mon Sep 17 00:00:00 2001 From: gitjiho Date: Fri, 15 Nov 2024 03:11:45 +0900 Subject: [PATCH] fix: change DQ DTO --- .../DailyQuestionResponseController.java | 6 +++--- .../dto/DailyQuestionResponse.java | 4 +++- .../repository/DailyResponseRepository.java | 5 +++++ .../service/DailyQuestionService.java | 21 +++++++++++++++---- .../service/DailyResponseService.java | 6 +++++- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java b/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java index 355148b..40a5c29 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/controller/DailyQuestionResponseController.java @@ -33,10 +33,10 @@ public ResponseEntity addDailyQuestion(@RequestBody D return ResponseEntity.status(HttpStatus.CREATED).body(new StringTypeMessageResponse("질문이 추가되었습니다.")); } - @Operation(summary = "랜덤한 하나의 질문 조회", description = "최근 조회된 질문 제외한 랜덤한 하나의 질문을 조회합니다.") + @Operation(summary = "랜덤한 하나의 질문 조회", description = "최근 조회된 질문 제외한 랜덤한 하나의 질문을 조회합니다. 사용자의 답변 여부도 함께 리턴되며, 만약 사용자의 답변이 있을 시 답변도 리턴됩니다.") @GetMapping("/question") - public ResponseEntity getRandomQuestionExcludingRecent() { - DailyQuestionResponse dailyQuestionResponse = dailyQuestionService.getRandomQuestionExcludingRecent(); + public ResponseEntity getRandomQuestionExcludingRecent(@RequestAttribute("memberId") Long memberId) { + DailyQuestionResponse dailyQuestionResponse = dailyQuestionService.getRandomQuestionExcludingRecent(memberId); return ResponseEntity.ok().body(dailyQuestionResponse); } diff --git a/src/main/java/com/gdg/kkia/dailyresponse/dto/DailyQuestionResponse.java b/src/main/java/com/gdg/kkia/dailyresponse/dto/DailyQuestionResponse.java index df28ec9..09b6455 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/dto/DailyQuestionResponse.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/dto/DailyQuestionResponse.java @@ -2,6 +2,8 @@ public record DailyQuestionResponse( Long id, - String question + String question, + boolean AnsweredToday, + String answer ) { } diff --git a/src/main/java/com/gdg/kkia/dailyresponse/repository/DailyResponseRepository.java b/src/main/java/com/gdg/kkia/dailyresponse/repository/DailyResponseRepository.java index 1fb40b6..500ecea 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/repository/DailyResponseRepository.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/repository/DailyResponseRepository.java @@ -6,9 +6,14 @@ import java.time.LocalDate; import java.util.List; +import java.util.Optional; public interface DailyResponseRepository extends JpaRepository { List findAllByMemberAndResponseDate(Member member, LocalDate localDate); + Optional findByMemberId(Long memberId); + + boolean existsByMemberIdAndResponseDate(Long memberId, LocalDate localDate); + } diff --git a/src/main/java/com/gdg/kkia/dailyresponse/service/DailyQuestionService.java b/src/main/java/com/gdg/kkia/dailyresponse/service/DailyQuestionService.java index 0ef3fb7..055d713 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/service/DailyQuestionService.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/service/DailyQuestionService.java @@ -4,7 +4,9 @@ import com.gdg.kkia.dailyresponse.dto.DailyQuestionRequest; import com.gdg.kkia.dailyresponse.dto.DailyQuestionResponse; import com.gdg.kkia.dailyresponse.entity.DailyQuestion; +import com.gdg.kkia.dailyresponse.entity.DailyResponse; import com.gdg.kkia.dailyresponse.repository.DailyQuestionRepository; +import com.gdg.kkia.dailyresponse.repository.DailyResponseRepository; import jakarta.annotation.PostConstruct; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; @@ -19,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.time.LocalDate; import java.util.LinkedList; import java.util.List; import java.util.Objects; @@ -33,6 +36,7 @@ public class DailyQuestionService { private final static int NUMBER_OF_RECENTLY_QUESTION_TO_EXCLUDE = 3; private final DailyQuestionRepository dailyQuestionRepository; + private final DailyResponseRepository dailyResponseRepository; @PostConstruct public void loadQuestionsFromFile() { @@ -59,7 +63,14 @@ public void addDailyQuestion(DailyQuestionRequest dailyQuestionRequest) { } @Transactional - public DailyQuestionResponse getRandomQuestionExcludingRecent() { + public DailyQuestionResponse getRandomQuestionExcludingRecent(Long memberId) { + DailyResponse dailyResponse = dailyResponseRepository.findByMemberId(memberId) + .orElse(null); + + if (dailyResponse != null && dailyResponse.getResponseDate().equals(LocalDate.now())) { + return new DailyQuestionResponse(dailyResponse.getDailyQuestion().getId(), dailyResponse.getDailyQuestion().getQuestion(), true, dailyResponse.getResponse()); + } + HttpSession session = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest().getSession(); List recentlyFetchedQuestionIds = (List) session.getAttribute("recentlyQuestionIds"); @@ -83,10 +94,10 @@ public DailyQuestionResponse getRandomQuestionExcludingRecent() { } session.setAttribute("recentlyQuestionIds", recentlyFetchedQuestionIds); - return new DailyQuestionResponse(randomQuestion.getId(), randomQuestion.getQuestion()); + return new DailyQuestionResponse(randomQuestion.getId(), randomQuestion.getQuestion(), false, "아직 응답이 없습니다."); } - return new DailyQuestionResponse(null, "조회된 질문이 없습니다."); + return new DailyQuestionResponse(null, "조회된 질문이 없습니다.", false, null); } @Transactional(readOnly = true) @@ -95,7 +106,9 @@ public List getAllDailyQuestionForManager() { .stream() .map(DailyQuestion -> new DailyQuestionResponse( DailyQuestion.getId(), - DailyQuestion.getQuestion())) + DailyQuestion.getQuestion(), + false, + "아직 응답이 없습니다.")) .collect(Collectors.toList()); } diff --git a/src/main/java/com/gdg/kkia/dailyresponse/service/DailyResponseService.java b/src/main/java/com/gdg/kkia/dailyresponse/service/DailyResponseService.java index 941ae55..358190a 100644 --- a/src/main/java/com/gdg/kkia/dailyresponse/service/DailyResponseService.java +++ b/src/main/java/com/gdg/kkia/dailyresponse/service/DailyResponseService.java @@ -1,5 +1,6 @@ package com.gdg.kkia.dailyresponse.service; +import com.gdg.kkia.common.exception.BadRequestException; import com.gdg.kkia.common.exception.NotFoundException; import com.gdg.kkia.common.exception.UnauthorizedException; import com.gdg.kkia.dailyresponse.dto.DailyResponseRequest; @@ -28,12 +29,15 @@ public class DailyResponseService { private final PointLogService pointLogService; public void saveResponseOfDailyQuestion(Long memberId, DailyResponseRequest dailyResponseRequest) { + if (dailyResponseRepository.existsByMemberIdAndResponseDate(memberId, LocalDate.now())) { + throw new BadRequestException("이미 오늘 응답을 했습니다."); + } + Member member = memberRepository.findById(memberId) .orElseThrow(() -> new NotFoundException("memberId에 해당하는 멤버가 없습니다.")); DailyQuestion dailyQuestion = dailyQuestionRepository.findById(dailyResponseRequest.questionId()) .orElseThrow(() -> new NotFoundException("questionId에 해당하는 질문이 없습니다.")); - DailyResponse dailyResponse = new DailyResponse(dailyResponseRequest.response(), member, dailyQuestion); dailyResponseRepository.save(dailyResponse);