diff --git a/backend/src/main/java/reviewme/template/service/TemplateService.java b/backend/src/main/java/reviewme/template/service/TemplateService.java index 1b6370878..0a19289d9 100644 --- a/backend/src/main/java/reviewme/template/service/TemplateService.java +++ b/backend/src/main/java/reviewme/template/service/TemplateService.java @@ -1,10 +1,12 @@ package reviewme.template.service; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import reviewme.reviewgroup.domain.ReviewGroup; import reviewme.reviewgroup.service.ReviewGroupService; +import reviewme.template.service.dto.response.SectionResponse; import reviewme.template.service.dto.response.TemplateResponse; import reviewme.template.service.mapper.TemplateMapper; @@ -18,6 +20,11 @@ public class TemplateService { @Transactional(readOnly = true) public TemplateResponse generateReviewForm(String reviewRequestCode) { ReviewGroup reviewGroup = reviewGroupService.getReviewGroupByReviewRequestCode(reviewRequestCode); - return templateMapper.mapToTemplateResponse(reviewGroup); + List sectionResponses = templateMapper.mapSectionResponses( + reviewGroup.getId(), reviewGroup.getTemplateId() + ); + return new TemplateResponse( + reviewGroup.getTemplateId(), reviewGroup.getReviewee(), reviewGroup.getProjectName(), sectionResponses + ); } } diff --git a/backend/src/main/java/reviewme/template/service/mapper/TemplateMapper.java b/backend/src/main/java/reviewme/template/service/mapper/TemplateMapper.java index 41f003e8c..951459e8e 100644 --- a/backend/src/main/java/reviewme/template/service/mapper/TemplateMapper.java +++ b/backend/src/main/java/reviewme/template/service/mapper/TemplateMapper.java @@ -3,7 +3,6 @@ import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; -import reviewme.reviewgroup.domain.ReviewGroup; import reviewme.template.domain.OptionGroup; import reviewme.template.domain.OptionItem; import reviewme.template.domain.Question; @@ -20,7 +19,6 @@ import reviewme.template.service.dto.response.OptionItemResponse; import reviewme.template.service.dto.response.QuestionResponse; import reviewme.template.service.dto.response.SectionResponse; -import reviewme.template.service.dto.response.TemplateResponse; import reviewme.template.service.exception.MissingOptionItemsInOptionGroupException; import reviewme.template.service.exception.QuestionInSectionNotFoundException; import reviewme.template.service.exception.SectionInTemplateNotFoundException; @@ -38,23 +36,14 @@ public class TemplateMapper { private final OptionGroupRepository optionGroupRepository; private final OptionItemRepository optionItemRepository; - public TemplateResponse mapToTemplateResponse(ReviewGroup reviewGroup) { - Template template = templateRepository.findById(reviewGroup.getTemplateId()) - .orElseThrow(() -> new TemplateNotFoundByReviewGroupException( - reviewGroup.getId(), reviewGroup.getTemplateId() - )); + public List mapSectionResponses(long templateId, long reviewGroupId) { + Template template = templateRepository.findById(templateId) + .orElseThrow(() -> new TemplateNotFoundByReviewGroupException(reviewGroupId, templateId)); - List sectionResponses = template.getSectionIds() + return template.getSectionIds() .stream() .map(this::mapToSectionResponse) .toList(); - - return new TemplateResponse( - template.getId(), - reviewGroup.getReviewee(), - reviewGroup.getProjectName(), - sectionResponses - ); } private SectionResponse mapToSectionResponse(TemplateSection templateSection) { diff --git a/backend/src/test/java/reviewme/template/service/mapper/TemplateMapperTest.java b/backend/src/test/java/reviewme/template/service/mapper/TemplateMapperTest.java index 0814b4400..9669113de 100644 --- a/backend/src/test/java/reviewme/template/service/mapper/TemplateMapperTest.java +++ b/backend/src/test/java/reviewme/template/service/mapper/TemplateMapperTest.java @@ -14,22 +14,22 @@ import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import reviewme.reviewgroup.domain.ReviewGroup; +import reviewme.reviewgroup.repository.ReviewGroupRepository; +import reviewme.support.ServiceTest; import reviewme.template.domain.OptionGroup; import reviewme.template.domain.Question; +import reviewme.template.domain.Section; +import reviewme.template.domain.Template; import reviewme.template.repository.OptionGroupRepository; import reviewme.template.repository.OptionItemRepository; import reviewme.template.repository.QuestionRepository; -import reviewme.reviewgroup.domain.ReviewGroup; -import reviewme.reviewgroup.repository.ReviewGroupRepository; -import reviewme.support.ServiceTest; -import reviewme.template.domain.Section; -import reviewme.template.service.exception.MissingOptionItemsInOptionGroupException; -import reviewme.template.service.exception.SectionInTemplateNotFoundException; import reviewme.template.repository.SectionRepository; import reviewme.template.repository.TemplateRepository; import reviewme.template.service.dto.response.QuestionResponse; import reviewme.template.service.dto.response.SectionResponse; -import reviewme.template.service.dto.response.TemplateResponse; +import reviewme.template.service.exception.MissingOptionItemsInOptionGroupException; +import reviewme.template.service.exception.SectionInTemplateNotFoundException; @ServiceTest class TemplateMapperTest { @@ -67,22 +67,22 @@ class TemplateMapperTest { Section section1 = sectionRepository.save(항상_보이는_섹션(List.of(question1.getId()))); Section section2 = sectionRepository.save(항상_보이는_섹션(List.of(question2.getId()))); - templateRepository.save(템플릿(List.of(section1.getId(), section2.getId()))); + Template template = templateRepository.save(템플릿(List.of(section1.getId(), section2.getId()))); ReviewGroup reviewGroup = reviewGroupRepository.save(리뷰_그룹()); // when - TemplateResponse templateResponse = templateMapper.mapToTemplateResponse(reviewGroup); + List sectionResponses = templateMapper.mapSectionResponses( + template.getId(), reviewGroup.getId() + ); // then assertAll( - () -> assertThat(templateResponse.revieweeName()).isEqualTo(reviewGroup.getReviewee()), - () -> assertThat(templateResponse.projectName()).isEqualTo(reviewGroup.getProjectName()), - () -> assertThat(templateResponse.sections()).hasSize(2), - () -> assertThat(templateResponse.sections().get(0).header()).isEqualTo(section1.getHeader()), - () -> assertThat(templateResponse.sections().get(0).questions()).hasSize(1), - () -> assertThat(templateResponse.sections().get(1).header()).isEqualTo(section2.getHeader()), - () -> assertThat(templateResponse.sections().get(1).questions()).hasSize(1) + () -> assertThat(sectionResponses).hasSize(2), + () -> assertThat(sectionResponses.get(0).header()).isEqualTo(section1.getHeader()), + () -> assertThat(sectionResponses.get(0).questions()).hasSize(1), + () -> assertThat(sectionResponses.get(1).header()).isEqualTo(section2.getHeader()), + () -> assertThat(sectionResponses.get(1).questions()).hasSize(1) ); } @@ -91,16 +91,17 @@ class TemplateMapperTest { // given Question question = questionRepository.save(서술형_필수_질문()); Section section = sectionRepository.save(항상_보이는_섹션(List.of(question.getId()))); - templateRepository.save(템플릿(List.of(section.getId()))); + Template template = templateRepository.save(템플릿(List.of(section.getId()))); ReviewGroup reviewGroup = reviewGroupRepository.save(리뷰_그룹()); // when - TemplateResponse templateResponse = templateMapper.mapToTemplateResponse(reviewGroup); + List sectionResponses = templateMapper.mapSectionResponses( + template.getId(), reviewGroup.getId() + ); // then - SectionResponse sectionResponse = templateResponse.sections().get(0); - assertThat(sectionResponse.onSelectedOptionId()).isNull(); + assertThat(sectionResponses.get(0).onSelectedOptionId()).isNull(); } @Test @@ -108,15 +109,17 @@ class TemplateMapperTest { // given Question question = questionRepository.save(서술형_필수_질문()); Section section = sectionRepository.save(항상_보이는_섹션(List.of(question.getId()))); - templateRepository.save(템플릿(List.of(section.getId()))); + Template template = templateRepository.save(템플릿(List.of(section.getId()))); ReviewGroup reviewGroup = reviewGroupRepository.save(리뷰_그룹()); // when - TemplateResponse templateResponse = templateMapper.mapToTemplateResponse(reviewGroup); + List sectionResponses = templateMapper.mapSectionResponses( + template.getId(), reviewGroup.getId() + ); // then - QuestionResponse questionResponse = templateResponse.sections().get(0).questions().get(0); + QuestionResponse questionResponse = sectionResponses.get(0).questions().get(0); assertAll( () -> assertThat(questionResponse.hasGuideline()).isFalse(), () -> assertThat(questionResponse.guideline()).isNull() @@ -128,26 +131,28 @@ class TemplateMapperTest { // given Question question = questionRepository.save(서술형_필수_질문()); Section section = sectionRepository.save(항상_보이는_섹션(List.of(question.getId()))); - templateRepository.save(템플릿(List.of(section.getId()))); + Template template = templateRepository.save(템플릿(List.of(section.getId()))); ReviewGroup reviewGroup = reviewGroupRepository.save(리뷰_그룹()); // when - TemplateResponse templateResponse = templateMapper.mapToTemplateResponse(reviewGroup); + List sectionResponses = templateMapper.mapSectionResponses( + template.getId(), reviewGroup.getId() + ); // then - QuestionResponse questionResponse = templateResponse.sections().get(0).questions().get(0); + QuestionResponse questionResponse = sectionResponses.get(0).questions().get(0); assertThat(questionResponse.optionGroup()).isNull(); } @Test void 템플릿_매핑_시_템플릿에_제공할_섹션이_없을_경우_예외가_발생한다() { // given - templateRepository.save(템플릿(List.of(1L))); + Template template = templateRepository.save(템플릿(List.of(1L))); ReviewGroup reviewGroup = reviewGroupRepository.save(리뷰_그룹()); // when, then - assertThatThrownBy(() -> templateMapper.mapToTemplateResponse(reviewGroup)) + assertThatThrownBy(() -> templateMapper.mapSectionResponses(template.getId(), reviewGroup.getId())) .isInstanceOf(SectionInTemplateNotFoundException.class); } @@ -158,12 +163,12 @@ class TemplateMapperTest { optionGroupRepository.save(선택지_그룹(question.getId())); Section section = sectionRepository.save(항상_보이는_섹션(List.of(question.getId()))); - templateRepository.save(템플릿(List.of(section.getId()))); + Template template = templateRepository.save(템플릿(List.of(section.getId()))); ReviewGroup reviewGroup = reviewGroupRepository.save(리뷰_그룹()); // when, then - assertThatThrownBy(() -> templateMapper.mapToTemplateResponse(reviewGroup)) + assertThatThrownBy(() -> templateMapper.mapSectionResponses(template.getId(), reviewGroup.getId())) .isInstanceOf(MissingOptionItemsInOptionGroupException.class); } }