Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] feat: 섹션 이름 조회 API 구현 #801

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package reviewme.template.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reviewme.review.service.exception.ReviewGroupNotFoundByReviewRequestCodeException;
import reviewme.reviewgroup.domain.ReviewGroup;
import reviewme.reviewgroup.repository.ReviewGroupRepository;
import reviewme.template.repository.SectionRepository;
import reviewme.template.service.dto.response.SectionNameResponse;
import reviewme.template.service.dto.response.SectionNamesResponse;

@Service
@RequiredArgsConstructor
public class SectionService {

private final ReviewGroupRepository reviewGroupRepository;
private final SectionRepository sectionRepository;

@Transactional(readOnly = true)
public SectionNamesResponse getSectionNames(String reviewRequestCode) {
return null;
ReviewGroup reviewGroup = reviewGroupRepository.findByReviewRequestCode(reviewRequestCode)
.orElseThrow(() -> new ReviewGroupNotFoundByReviewRequestCodeException(reviewRequestCode));

List<SectionNameResponse> sectionNameResponses = sectionRepository.findAllByTemplateId(reviewGroup.getTemplateId())
.stream()
.map(section -> new SectionNameResponse(section.getId(), section.getSectionName()))
.toList();
Comment on lines +26 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(다같이 생각 나누면 좋겠습니다)
현재 이 로직은 섹션의 ID, 이름만을 필요로 해요. 하지만 Repository에서는 Section을 반환하기 때문에 불필요한 Column까지 조회하고 있네요 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

템플릿 내 섹션이 많지 않아서 괜찮을 것 같다는 생각입니다. (사용자가 작성해야 하는 리뷰 항목이랑 직결되니 아주 많아 지지 않을 것 같아요)
다만, 후에 조회를 분리하여 조회용 dto를 도입한다고 하면 어차피 조회용 dto를 만들어야 하니 그때는 id와 name만을 가지는 dto를 만들어서 사용해도 괜찮을 것 같습니다~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엔티티의 하나의 속성만 필요하다면 해당 속성만 가져오는 게 되겠지만, 그렇지 않은 경우는 1. db-service dto를 만들거나 2. 일단 엔티티째로 가져와서 꺼내쓰기 이 두가지 방법이 떠올라요.

1번으로 하면 불필요한 속성을 안가져올 수 있지만 별도의 dto를 만들어야한다는 단점이 있고,
2번은 불필요한 연관관계 쿼리가 나가거나, 필요하지 않은 데이터까지 메모리에 불러와진다는 단점이 있겠네요.
하지만 2번의 단점 2가지 모두 캐시를 적용하면 상관없어진다고 생각해서 section 엔티티 통째로 받아오는 것이 간단해보입니다!

비교할만한 다른 방법이 또 있을까요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쿼리 전용 Dto 를 사용하면 효율적일 부분들이 분명히 존재합니다.
예를 들어서 여러 엔티티에 대한 정보를 조회해와야 하는 경우
혹은 FetchType 이 Eager 로 되어있는 다른 컬럼들을 로드하는 것을 막을 수 있는 경우
하지만 이런 코드가 많아진다면, 레포지토리 함수 재사용률이 떨어지고 너무 많은 함수가 생길 수 있을 것 같아요.

그런데 레포지토리에서 도메인을 반환하는 함수에 캐시를 적용한다면
"이번에 반드시 필요하지 않은 데이터까지 한번에 조회하는 문제"가 해결될거라 생각해요.!


return new SectionNamesResponse(sectionNameResponses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package reviewme.template.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static reviewme.fixture.ReviewGroupFixture.리뷰_그룹;
import static reviewme.fixture.TemplateFixture.템플릿;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import reviewme.review.service.exception.ReviewGroupNotFoundByReviewRequestCodeException;
import reviewme.reviewgroup.domain.ReviewGroup;
import reviewme.reviewgroup.repository.ReviewGroupRepository;
import reviewme.support.ServiceTest;
import reviewme.template.domain.Section;
import reviewme.template.domain.VisibleType;
import reviewme.template.repository.SectionRepository;
import reviewme.template.repository.TemplateRepository;
import reviewme.template.service.dto.response.SectionNameResponse;
import reviewme.template.service.dto.response.SectionNamesResponse;

@ServiceTest
class SectionServiceTest {

@Autowired
private SectionService sectionService;

@Autowired
private ReviewGroupRepository reviewGroupRepository;

@Autowired
private TemplateRepository templateRepository;

@Autowired
private SectionRepository sectionRepository;

@Test
void 템플릿에_있는_섹션_이름_목록을_응답한다() {
// given
String sectionName1 = "섹션1";
String sectionName2 = "섹션2";
String sectionName3 = "섹션3";

Section visibleSection1 = sectionRepository.save(
new Section(VisibleType.ALWAYS, List.of(1L), null, sectionName1, "헤더", 1));
Section visibleSection2 = sectionRepository.save(
new Section(VisibleType.ALWAYS, List.of(2L), null, sectionName2, "헤더", 2));
Section nonVisibleSection = sectionRepository.save(
new Section(VisibleType.CONDITIONAL, List.of(1L), 1L, sectionName3, "헤더", 3));
templateRepository.save(
템플릿(List.of(nonVisibleSection.getId(), visibleSection2.getId(), visibleSection1.getId())));

ReviewGroup reviewGroup = reviewGroupRepository.save(리뷰_그룹());

// when
SectionNamesResponse actual = sectionService.getSectionNames(reviewGroup.getReviewRequestCode());

// then
assertThat(actual.sections()).extracting(SectionNameResponse::name)
.containsExactly(sectionName1, sectionName2, sectionName3);
}

@Test
void 템플릿에_있는_섹션_이름_목록_조회시_리뷰_요청_코드가_존재하지_않는_경우_예외가_발생한다() {
// given
ReviewGroup reviewGroup = reviewGroupRepository.save(리뷰_그룹());

// when, then
assertThatThrownBy(() -> sectionService.getSectionNames(
reviewGroup.getReviewRequestCode() + "wrong"))
.isInstanceOf(ReviewGroupNotFoundByReviewRequestCodeException.class);
}
}