-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
967e99d
feat: 섹션 이름 목록 응답 기능 구현
Kimprodp cea873f
test: 섹션 이름 목록 응답 기능 테스트 작성
Kimprodp 22c8057
refactor: section의 questionIds eager 로딩 변경
Kimprodp 8cb04d2
test: 섹션 이름 목록 테스트 시, 이름으로 검증할 수 있도록 변경
Kimprodp be6424b
test: 섹션 이름 목록 반환 테스트 시, 검증하려는 섹션명을 따로 빼는 것으로 변경
Kimprodp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 18 additions & 1 deletion
19
backend/src/main/java/reviewme/template/service/SectionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
return new SectionNamesResponse(sectionNameResponses); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
backend/src/test/java/reviewme/template/service/SectionServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(다같이 생각 나누면 좋겠습니다)
현재 이 로직은 섹션의 ID, 이름만을 필요로 해요. 하지만
Repository
에서는Section
을 반환하기 때문에 불필요한 Column까지 조회하고 있네요 🤔There was a problem hiding this comment.
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를 만들어서 사용해도 괜찮을 것 같습니다~
There was a problem hiding this comment.
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 엔티티 통째로 받아오는 것이 간단해보입니다!
비교할만한 다른 방법이 또 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
쿼리 전용 Dto 를 사용하면 효율적일 부분들이 분명히 존재합니다.
예를 들어서 여러 엔티티에 대한 정보를 조회해와야 하는 경우
혹은 FetchType 이 Eager 로 되어있는 다른 컬럼들을 로드하는 것을 막을 수 있는 경우
하지만 이런 코드가 많아진다면, 레포지토리 함수 재사용률이 떨어지고 너무 많은 함수가 생길 수 있을 것 같아요.
그런데 레포지토리에서 도메인을 반환하는 함수에 캐시를 적용한다면
"이번에 반드시 필요하지 않은 데이터까지 한번에 조회하는 문제"가 해결될거라 생각해요.!