From b2156627f3b5be75be6b098b1dbee66d662195d5 Mon Sep 17 00:00:00 2001 From: gitjiho Date: Wed, 13 Nov 2024 20:42:51 +0900 Subject: [PATCH] refactor: change surveyResponse List -> most recent --- .../kkia/survey/controller/SurveyController.java | 12 +++++------- .../com/gdg/kkia/survey/service/SurveyService.java | 14 +++++--------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/gdg/kkia/survey/controller/SurveyController.java b/src/main/java/com/gdg/kkia/survey/controller/SurveyController.java index d36f394..438c60f 100644 --- a/src/main/java/com/gdg/kkia/survey/controller/SurveyController.java +++ b/src/main/java/com/gdg/kkia/survey/controller/SurveyController.java @@ -11,8 +11,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.List; - @RestController @RequiredArgsConstructor @RequestMapping("/api/survey") @@ -28,11 +26,11 @@ public ResponseEntity saveSurveyWrittenByMember(@Requ return ResponseEntity.status(HttpStatus.CREATED).body(new StringTypeMessageResponse("저장되었습니다.")); } - @Operation(summary = "사용자가 작성한 진단표 결과 모두 조회", description = "사용자가 작성했던 모든 진단표 결과를 조회합니다.") - @GetMapping - public ResponseEntity> getAllAnswersWrittenByMember(@RequestAttribute("memberId") Long memberId) { - List surveyResponses = surveyService.getAllSurveyAnswerWrittenByUser(memberId); - return ResponseEntity.ok().body(surveyResponses); + @Operation(summary = "사용자가 작성한 가장 최근의 진단표 결과 조회", description = "사용자가 작성한 가장 최근의 진단표 결과 조회를 조회합니다.") + @GetMapping("/user") + public ResponseEntity getMostRecentlyWrittenSurveyWrittenByUser(@RequestAttribute("memberId") Long memberId) { + SurveyResponse surveyResponse = surveyService.getMostRecentlyWrittenSurveyWrittenByUser(memberId); + return ResponseEntity.ok().body(surveyResponse); } @Operation(summary = "인공지능이 작성한 가장 최근의 진단표 결과 조회", description = "인공지능이 작성한 가장 최근의 진단표 결과를 조회합니다.") diff --git a/src/main/java/com/gdg/kkia/survey/service/SurveyService.java b/src/main/java/com/gdg/kkia/survey/service/SurveyService.java index e339670..d3438e6 100644 --- a/src/main/java/com/gdg/kkia/survey/service/SurveyService.java +++ b/src/main/java/com/gdg/kkia/survey/service/SurveyService.java @@ -13,7 +13,6 @@ import java.time.LocalDateTime; import java.util.List; -import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -32,17 +31,14 @@ public void saveSurveyAnswerWrittenByUser(Long memberId, SurveyRequest surveyReq } @Transactional(readOnly = true) - public List getAllSurveyAnswerWrittenByUser(Long memberId) { + public SurveyResponse getMostRecentlyWrittenSurveyWrittenByUser(Long memberId) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new NotFoundException("id에 해당하는 멤버를 찾을 수 없습니다.")); - return surveyRepository.findAllByMemberAndRole(member, Survey.Role.USER) - .stream() - .map(Survey -> new SurveyResponse( - Survey.getId(), - Survey.getSurveyedDatetime(), - Survey.getAnswer())) - .collect(Collectors.toList()); + Survey survey = surveyRepository.findTopByMemberAndRoleOrderBySurveyedDatetimeDesc(member, Survey.Role.USER) + .orElseThrow(() -> new NotFoundException("작성된 survey가 없습니다.")); + + return new SurveyResponse(survey.getId(), survey.getSurveyedDatetime(), survey.getAnswer()); } @Transactional