Skip to content

Commit

Permalink
[BE] refactor: 하이라이트 매핑 로직 간소화 (#962)
Browse files Browse the repository at this point in the history
* refactor: 하이라이트 매핑 로직 간소화

* refactor: `Fragments` 변환 로직 dto 내부로 이동

* refactor: 내부 값 사용하도록 변경

* refactor: Stream 가독성 향상

* style: 개행 삭제

* test: 하이라이트가 안 쳐지는 라인이 잘 변환되는지 테스트

Co-authored-by: Hyeonji <[email protected]>

* refactor: 메서드 네임 확실하게 수정

---------

Co-authored-by: Hyeonji <[email protected]>
  • Loading branch information
donghoony and skylar1220 authored Nov 17, 2024
1 parent 8858e02 commit 9b59f8d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.IntStream;
import lombok.Getter;
import reviewme.highlight.domain.exception.InvalidHighlightLineIndexException;
import reviewme.highlight.domain.exception.NegativeHighlightLineIndexException;
Expand Down Expand Up @@ -37,4 +39,12 @@ private void validateLineIndexRange(int lineIndex) {
throw new InvalidHighlightLineIndexException(lineIndex, lines.size());
}
}

public List<Highlight> toHighlights(long answerId) {
return IntStream.range(0, lines.size())
.mapToObj(lineIndex -> lines.get(lineIndex).getRanges().stream()
.map(range -> new Highlight(answerId, lineIndex, range)))
.flatMap(Function.identity())
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public void editHighlight(HighlightsRequest highlightsRequest, ReviewGroup revie

Set<Long> answerIds = answerRepository.findIdsByQuestionId(highlightsRequest.questionId());
highlightRepository.deleteAllByAnswerIds(answerIds);

highlightRepository.saveAll(highlights);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Stream;
import reviewme.highlight.service.mapper.HighlightFragment;

public record HighlightRequest(

Expand All @@ -13,4 +15,15 @@ public record HighlightRequest(
@Valid @NotEmpty(message = "하이라이트 된 라인을 입력해주세요.")
List<HighlightedLineRequest> lines
) {
public List<HighlightFragment> toFragments() {
return lines.stream()
.flatMap(this::mapRangesToFragment)
.toList();
}

private Stream<HighlightFragment> mapRangesToFragment(HighlightedLineRequest line) {
return line.ranges()
.stream()
.map(range -> new HighlightFragment(answerId, line.index(), range.startIndex(), range.endIndex()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import reviewme.highlight.service.mapper.HighlightFragment;

public record HighlightsRequest(

Expand All @@ -20,4 +21,10 @@ public List<Long> getUniqueAnswerIds() {
.distinct()
.toList();
}

public List<HighlightFragment> toFragments() {
return highlights.stream()
.flatMap(request -> request.toFragments().stream())
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package reviewme.highlight.service.mapper;

public record HighlightFragment(long answerId, int lineIndex, int startIndex, int endIndex) {
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package reviewme.highlight.service.mapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import reviewme.highlight.domain.HighlightedLines;
import reviewme.highlight.domain.HighlightedLine;
import reviewme.highlight.domain.Highlight;
import reviewme.highlight.domain.HighlightRange;
import reviewme.highlight.service.dto.HighlightIndexRangeRequest;
import reviewme.highlight.service.dto.HighlightRequest;
import reviewme.highlight.service.dto.HighlightedLineRequest;
import reviewme.highlight.domain.HighlightedLines;
import reviewme.highlight.service.dto.HighlightsRequest;
import reviewme.review.domain.Answer;
import reviewme.review.domain.TextAnswer;
import reviewme.review.repository.TextAnswerRepository;

@Component
Expand All @@ -25,53 +18,19 @@ public class HighlightMapper {
private final TextAnswerRepository textAnswerRepository;

public List<Highlight> mapToHighlights(HighlightsRequest highlightsRequest) {
Map<Long, HighlightedLines> answerHighlightLines = textAnswerRepository
Map<Long, HighlightedLines> answerIdHighlightedLines = textAnswerRepository
.findAllById(highlightsRequest.getUniqueAnswerIds())
.stream()
.collect(Collectors.toMap(Answer::getId, answer -> new HighlightedLines(answer.getContent())));
addIndexRanges(highlightsRequest, answerHighlightLines);
return mapLinesToHighlights(answerHighlightLines);
}

private void addIndexRanges(HighlightsRequest highlightsRequest, Map<Long, HighlightedLines> answerHighlightLines) {
for (HighlightRequest highlightRequest : highlightsRequest.highlights()) {
HighlightedLines highlightedLines = answerHighlightLines.get(highlightRequest.answerId());
addIndexRangesForAnswer(highlightRequest, highlightedLines);
}
}

private void addIndexRangesForAnswer(HighlightRequest highlightRequest, HighlightedLines highlightedLines) {
for (HighlightedLineRequest lineRequest : highlightRequest.lines()) {
int lineIndex = lineRequest.index();
for (HighlightIndexRangeRequest rangeRequest : lineRequest.ranges()) {
highlightedLines.addRange(lineIndex, rangeRequest.startIndex(), rangeRequest.endIndex());
}
}
}

private List<Highlight> mapLinesToHighlights(Map<Long, HighlightedLines> answerHighlightLines) {
List<Highlight> highlights = new ArrayList<>();
for (Entry<Long, HighlightedLines> answerHighlightLine : answerHighlightLines.entrySet()) {
createHighlightsForAnswer(answerHighlightLine, highlights);
}
return highlights;
}
.collect(Collectors.toMap(TextAnswer::getId, answer -> new HighlightedLines(answer.getContent())));

private void createHighlightsForAnswer(Entry<Long, HighlightedLines> answerHighlightLine,
List<Highlight> highlights) {
long answerId = answerHighlightLine.getKey();
List<HighlightedLine> highlightedLines = answerHighlightLine.getValue().getLines();

for (int lineIndex = 0; lineIndex < highlightedLines.size(); lineIndex++) {
createHighlightForLine(highlightedLines, lineIndex, answerId, highlights);
for (HighlightFragment fragment : highlightsRequest.toFragments()) {
HighlightedLines highlightedLines = answerIdHighlightedLines.get(fragment.answerId());
highlightedLines.addRange(fragment.lineIndex(), fragment.startIndex(), fragment.endIndex());
}
}

private void createHighlightForLine(List<HighlightedLine> highlightedLines, int lineIndex, long answerId,
List<Highlight> highlights) {
for (HighlightRange range : highlightedLines.get(lineIndex).getRanges()) {
Highlight highlight = new Highlight(answerId, lineIndex, range);
highlights.add(highlight);
}
return answerIdHighlightedLines.entrySet()
.stream()
.flatMap(entry -> entry.getValue().toHighlights(entry.getKey()).stream())
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,21 @@ class HighlightedLinesTest {
assertThatCode(() -> highlightedLines.addRange(invalidLineIndex, 0, 1))
.isInstanceOf(InvalidHighlightLineIndexException.class);
}

@Test
void 하이라이트가_존재하는_부분만_엔티티로_변환한다() {
// given
HighlightedLines lines = new HighlightedLines("0\n11\n222");
lines.addRange(0, 0, 0);
lines.addRange(2, 2, 2);

// when
List<Highlight> highlights = lines.toHighlights(1L);

// then
assertThat(highlights).containsExactly(
new Highlight(1L, 0, new HighlightRange(0, 0)),
new Highlight(1L, 2, new HighlightRange(2, 2))
);
}
}

0 comments on commit 9b59f8d

Please sign in to comment.