From 692742408f33f462157ecbdda3966c4333c7ca82 Mon Sep 17 00:00:00 2001 From: Hyeonji <110809927+skylar1220@users.noreply.github.com> Date: Mon, 19 Aug 2024 13:50:32 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20refactor:=20=EB=A6=AC=EB=B7=B0=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EB=84=A4=EC=9D=B4=ED=8B=B0?= =?UTF-8?q?=EB=B8=8C=20=EC=BF=BC=EB=A6=AC=EB=A5=BC=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD=20(#391)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 리뷰 목록에서 카테고리 표시하는 로직을 네이티브 쿼리로 수정 * refactor: left join을 inner join으로 수정 * test: 실패하는 테스트 수정 --- .../repository/OptionItemRepository.java | 18 +++--- .../review/service/ReviewService.java | 26 ++------ .../review/service/ReviewServiceTest.java | 60 +------------------ 3 files changed, 17 insertions(+), 87 deletions(-) diff --git a/backend/src/main/java/reviewme/question/repository/OptionItemRepository.java b/backend/src/main/java/reviewme/question/repository/OptionItemRepository.java index ecd35594d..0b639b6fb 100644 --- a/backend/src/main/java/reviewme/question/repository/OptionItemRepository.java +++ b/backend/src/main/java/reviewme/question/repository/OptionItemRepository.java @@ -7,17 +7,12 @@ import org.springframework.stereotype.Repository; import reviewme.question.domain.OptionItem; import reviewme.question.domain.OptionType; -import reviewme.question.domain.exception.OptionItemNotFoundException; @Repository public interface OptionItemRepository extends JpaRepository { - List findAllByOptionType(OptionType optionType); - List findAllByOptionGroupId(long optionGroupId); - boolean existsByOptionTypeAndId(OptionType optionType, long id); - @Query(value = """ SELECT o.id FROM option_item o LEFT JOIN checkbox_answer_selected_option c @@ -40,7 +35,14 @@ public interface OptionItemRepository extends JpaRepository { """, nativeQuery = true) List findSelectedOptionItemsByReviewIdAndQuestionId(long reviewId, long questionId); - default OptionItem getOptionItemById(long id) { - return findById(id).orElseThrow(() -> new OptionItemNotFoundException(id)); - } + @Query(value = """ + SELECT o.* FROM option_item o + INNER JOIN checkbox_answer_selected_option cao + ON cao.selected_option_id = o.id + INNER JOIN checkbox_answer ca + ON cao.checkbox_answer_id = ca.id + WHERE ca.review_id = :reviewId + AND o.option_type = :#{#optionType.name()} + """, nativeQuery = true) + List findByReviewIdAndOptionType(long reviewId, OptionType optionType); } diff --git a/backend/src/main/java/reviewme/review/service/ReviewService.java b/backend/src/main/java/reviewme/review/service/ReviewService.java index a9c985559..35a8f397e 100644 --- a/backend/src/main/java/reviewme/review/service/ReviewService.java +++ b/backend/src/main/java/reviewme/review/service/ReviewService.java @@ -7,9 +7,7 @@ import reviewme.question.domain.OptionItem; import reviewme.question.domain.OptionType; import reviewme.question.repository.OptionItemRepository; -import reviewme.review.domain.CheckboxAnswer; import reviewme.review.domain.Review; -import reviewme.review.domain.exception.CategoryOptionByReviewNotFoundException; import reviewme.review.domain.exception.ReviewGroupNotFoundByGroupAccessCodeException; import reviewme.review.repository.ReviewRepository; import reviewme.review.service.dto.response.list.ReceivedReviewCategoryResponse; @@ -43,26 +41,12 @@ public ReceivedReviewsResponse findReceivedReviews(String groupAccessCode) { } private ReceivedReviewResponse createReceivedReviewResponse(Review review) { - CheckboxAnswer checkboxAnswer = review.getCheckboxAnswers() - .stream() - .filter(answer -> optionItemRepository.existsByOptionTypeAndId( - OptionType.CATEGORY, answer.getSelectedOptionIds().get(0).getSelectedOptionId() - )) - .findFirst() - .orElseThrow(() -> new CategoryOptionByReviewNotFoundException(review.getId())); + List categoryOptionItems = optionItemRepository.findByReviewIdAndOptionType(review.getId(), + OptionType.CATEGORY); - List categoryResponses = - checkboxAnswer.getSelectedOptionIds() - .stream() - .map(checkBoxAnswerSelectedOptionId -> { - OptionItem optionItem = optionItemRepository.getOptionItemById( - checkBoxAnswerSelectedOptionId.getSelectedOptionId() - ); - return new ReceivedReviewCategoryResponse( - optionItem.getId(), optionItem.getContent() - ); - }) - .toList(); + List categoryResponses = categoryOptionItems.stream() + .map(optionItem -> new ReceivedReviewCategoryResponse(optionItem.getId(), optionItem.getContent())) + .toList(); return new ReceivedReviewResponse( review.getId(), diff --git a/backend/src/test/java/reviewme/review/service/ReviewServiceTest.java b/backend/src/test/java/reviewme/review/service/ReviewServiceTest.java index d58f8ca88..29bfe5693 100644 --- a/backend/src/test/java/reviewme/review/service/ReviewServiceTest.java +++ b/backend/src/test/java/reviewme/review/service/ReviewServiceTest.java @@ -13,20 +13,17 @@ import reviewme.question.domain.QuestionType; import reviewme.question.repository.OptionGroupRepository; import reviewme.question.repository.OptionItemRepository; +import reviewme.question.repository.QuestionRepository; import reviewme.review.domain.CheckboxAnswer; import reviewme.review.domain.Review; import reviewme.review.domain.exception.ReviewGroupNotFoundByGroupAccessCodeException; -import reviewme.review.service.dto.response.list.ReceivedReviewCategoryResponse; -import reviewme.review.service.dto.response.list.ReceivedReviewsResponse; import reviewme.review.repository.CheckboxAnswerRepository; -import reviewme.question.repository.QuestionRepository; import reviewme.review.repository.ReviewRepository; +import reviewme.review.service.dto.response.list.ReceivedReviewsResponse; import reviewme.reviewgroup.domain.ReviewGroup; import reviewme.reviewgroup.repository.ReviewGroupRepository; import reviewme.support.ServiceTest; -import reviewme.template.domain.Section; import reviewme.template.domain.Template; -import reviewme.template.domain.VisibleType; import reviewme.template.repository.SectionRepository; import reviewme.template.repository.TemplateRepository; @@ -95,57 +92,4 @@ class ReviewServiceTest { // then assertThat(response.reviews()).hasSize(2); } - - @Test - void 리뷰_목록을_반환할때_선택한_카테고리만_함께_반환한다() { - // given - String groupAccessCode = "groupAccessCode"; - Question question1 = questionRepository.save( - new Question(true, QuestionType.CHECKBOX, "프로젝트 기간 동안, 팀원의 강점이 드러났던 순간을 선택해주세요. (1~2개)", null, 1) - ); - Question question2 = questionRepository.save( - new Question(true, QuestionType.CHECKBOX, "커뮤니케이션, 협업 능력에서 어떤 부분이 인상 깊었는지 선택해주세요. (1개 이상)", null, 2) - ); - - OptionGroup categoryOptionGroup = optionGroupRepository.save(new OptionGroup(question1.getId(), 1, 2)); - OptionGroup keywordOptionGroup = optionGroupRepository.save(new OptionGroup(question2.getId(), 1, 10)); - - OptionItem categoryOption1 = new OptionItem("커뮤니케이션 능력 ", categoryOptionGroup.getId(), 1, OptionType.CATEGORY); - OptionItem categoryOption2 = new OptionItem("시간 관리 능력", categoryOptionGroup.getId(), 2, OptionType.CATEGORY); - OptionItem keywordOption = new OptionItem("얘기를 잘 들어줘요", keywordOptionGroup.getId(), 2, OptionType.KEYWORD); - optionItemRepository.saveAll(List.of(categoryOption1, categoryOption2, keywordOption)); - - Section section1 = sectionRepository.save( - new Section(VisibleType.ALWAYS, List.of(question1.getId()), null, "팀원과 함께 한 기억을 떠올려볼게요.", 1) - ); - Section section2 = sectionRepository.save( - new Section(VisibleType.CONDITIONAL, List.of(question2.getId()), null, "선택한 순간들을 바탕으로 리뷰를 작성해볼게요.", 1) - ); - Template template = templateRepository.save(new Template(List.of(section1.getId(), section2.getId()))); - - ReviewGroup reviewGroup = reviewGroupRepository.save( - new ReviewGroup("커비", "리뷰미", "reviewRequestCode", groupAccessCode) - ); - CheckboxAnswer categoryAnswer = new CheckboxAnswer(question1.getId(), List.of(categoryOption1.getId())); - CheckboxAnswer keywordAnswer = new CheckboxAnswer(question2.getId(), List.of(keywordOption.getId())); - reviewRepository.save( - new Review(template.getId(), reviewGroup.getId(), List.of(), List.of(categoryAnswer, keywordAnswer)) - ); - - // when - ReceivedReviewsResponse response = reviewService.findReceivedReviews(groupAccessCode); - - // then - List categoryContents = optionItemRepository.findAllByOptionType(OptionType.CATEGORY) - .stream() - .map(OptionItem::getContent) - .toList(); - - assertThat(response.reviews()) - .map(review -> review.categories() - .stream() - .map(ReceivedReviewCategoryResponse::content) - .toList()) - .allSatisfy(content -> assertThat(categoryContents).containsAll(content)); - } }