Skip to content

Commit

Permalink
[BE] refactor: TemplateMapper와 ReviewGroup 결합 느슨하게 수정 (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
donghoony authored Dec 28, 2024
1 parent 69b507a commit 77d0863
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<SectionResponse> sectionResponses = templateMapper.mapSectionResponses(
reviewGroup.getId(), reviewGroup.getTemplateId()
);
return new TemplateResponse(
reviewGroup.getTemplateId(), reviewGroup.getReviewee(), reviewGroup.getProjectName(), sectionResponses
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<SectionResponse> mapSectionResponses(long templateId, long reviewGroupId) {
Template template = templateRepository.findById(templateId)
.orElseThrow(() -> new TemplateNotFoundByReviewGroupException(reviewGroupId, templateId));

List<SectionResponse> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<SectionResponse> 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)
);
}

Expand All @@ -91,32 +91,35 @@ 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<SectionResponse> 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
void 가이드라인이_없는_경우_가이드_라인을_제공하지_않는다() {
// 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<SectionResponse> 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()
Expand All @@ -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<SectionResponse> 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);
}

Expand All @@ -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);
}
}

0 comments on commit 77d0863

Please sign in to comment.