Skip to content

Commit

Permalink
Refactor: Change survey count
Browse files Browse the repository at this point in the history
Refactor: Change survey count
  • Loading branch information
GitJIHO authored Nov 13, 2024
2 parents 3cd76bd + 2bca510 commit 283ff6e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/survey")
Expand All @@ -28,11 +26,11 @@ public ResponseEntity<StringTypeMessageResponse> saveSurveyWrittenByMember(@Requ
return ResponseEntity.status(HttpStatus.CREATED).body(new StringTypeMessageResponse("저장되었습니다."));
}

@Operation(summary = "사용자가 작성한 진단표 결과 모두 조회", description = "사용자가 작성했던 모든 진단표 결과를 조회합니다.")
@GetMapping
public ResponseEntity<List<SurveyResponse>> getAllAnswersWrittenByMember(@RequestAttribute("memberId") Long memberId) {
List<SurveyResponse> surveyResponses = surveyService.getAllSurveyAnswerWrittenByUser(memberId);
return ResponseEntity.ok().body(surveyResponses);
@Operation(summary = "사용자가 작성한 가장 최근의 진단표 결과 조회", description = "사용자가 작성한 가장 최근의 진단표 결과 조회를 조회합니다.")
@GetMapping("/user")
public ResponseEntity<SurveyResponse> getMostRecentlyWrittenSurveyWrittenByUser(@RequestAttribute("memberId") Long memberId) {
SurveyResponse surveyResponse = surveyService.getMostRecentlyWrittenSurveyWrittenByUser(memberId);
return ResponseEntity.ok().body(surveyResponse);
}

@Operation(summary = "인공지능이 작성한 가장 최근의 진단표 결과 조회", description = "인공지능이 작성한 가장 최근의 진단표 결과를 조회합니다.")
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/gdg/kkia/survey/service/SurveyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -32,17 +31,14 @@ public void saveSurveyAnswerWrittenByUser(Long memberId, SurveyRequest surveyReq
}

@Transactional(readOnly = true)
public List<SurveyResponse> 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
Expand Down

0 comments on commit 283ff6e

Please sign in to comment.