diff --git a/backend/src/main/java/reviewme/highlight/domain/HighlightedLines.java b/backend/src/main/java/reviewme/highlight/domain/HighlightedLines.java index f7000ecb2..f24827d30 100644 --- a/backend/src/main/java/reviewme/highlight/domain/HighlightedLines.java +++ b/backend/src/main/java/reviewme/highlight/domain/HighlightedLines.java @@ -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; @@ -37,4 +39,12 @@ private void validateLineIndexRange(int lineIndex) { throw new InvalidHighlightLineIndexException(lineIndex, lines.size()); } } + + public List 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(); + } } diff --git a/backend/src/main/java/reviewme/highlight/service/HighlightService.java b/backend/src/main/java/reviewme/highlight/service/HighlightService.java index 7cb9f9c70..cff5063a5 100644 --- a/backend/src/main/java/reviewme/highlight/service/HighlightService.java +++ b/backend/src/main/java/reviewme/highlight/service/HighlightService.java @@ -30,7 +30,6 @@ public void editHighlight(HighlightsRequest highlightsRequest, ReviewGroup revie Set answerIds = answerRepository.findIdsByQuestionId(highlightsRequest.questionId()); highlightRepository.deleteAllByAnswerIds(answerIds); - highlightRepository.saveAll(highlights); } } diff --git a/backend/src/main/java/reviewme/highlight/service/dto/HighlightRequest.java b/backend/src/main/java/reviewme/highlight/service/dto/HighlightRequest.java index 673cc8e6a..1371c6959 100644 --- a/backend/src/main/java/reviewme/highlight/service/dto/HighlightRequest.java +++ b/backend/src/main/java/reviewme/highlight/service/dto/HighlightRequest.java @@ -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( @@ -13,4 +15,15 @@ public record HighlightRequest( @Valid @NotEmpty(message = "하이라이트 된 라인을 입력해주세요.") List lines ) { + public List toFragments() { + return lines.stream() + .flatMap(this::mapRangesToFragment) + .toList(); + } + + private Stream mapRangesToFragment(HighlightedLineRequest line) { + return line.ranges() + .stream() + .map(range -> new HighlightFragment(answerId, line.index(), range.startIndex(), range.endIndex())); + } } diff --git a/backend/src/main/java/reviewme/highlight/service/dto/HighlightsRequest.java b/backend/src/main/java/reviewme/highlight/service/dto/HighlightsRequest.java index b8f26cba6..b1f7f6de3 100644 --- a/backend/src/main/java/reviewme/highlight/service/dto/HighlightsRequest.java +++ b/backend/src/main/java/reviewme/highlight/service/dto/HighlightsRequest.java @@ -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( @@ -20,4 +21,10 @@ public List getUniqueAnswerIds() { .distinct() .toList(); } + + public List toFragments() { + return highlights.stream() + .flatMap(request -> request.toFragments().stream()) + .toList(); + } } diff --git a/backend/src/main/java/reviewme/highlight/service/mapper/HighlightFragment.java b/backend/src/main/java/reviewme/highlight/service/mapper/HighlightFragment.java new file mode 100644 index 000000000..33e08f56e --- /dev/null +++ b/backend/src/main/java/reviewme/highlight/service/mapper/HighlightFragment.java @@ -0,0 +1,4 @@ +package reviewme.highlight.service.mapper; + +public record HighlightFragment(long answerId, int lineIndex, int startIndex, int endIndex) { +} diff --git a/backend/src/main/java/reviewme/highlight/service/mapper/HighlightMapper.java b/backend/src/main/java/reviewme/highlight/service/mapper/HighlightMapper.java index edbec9013..512546030 100644 --- a/backend/src/main/java/reviewme/highlight/service/mapper/HighlightMapper.java +++ b/backend/src/main/java/reviewme/highlight/service/mapper/HighlightMapper.java @@ -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 @@ -25,53 +18,19 @@ public class HighlightMapper { private final TextAnswerRepository textAnswerRepository; public List mapToHighlights(HighlightsRequest highlightsRequest) { - Map answerHighlightLines = textAnswerRepository + Map 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 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 mapLinesToHighlights(Map answerHighlightLines) { - List highlights = new ArrayList<>(); - for (Entry answerHighlightLine : answerHighlightLines.entrySet()) { - createHighlightsForAnswer(answerHighlightLine, highlights); - } - return highlights; - } + .collect(Collectors.toMap(TextAnswer::getId, answer -> new HighlightedLines(answer.getContent()))); - private void createHighlightsForAnswer(Entry answerHighlightLine, - List highlights) { - long answerId = answerHighlightLine.getKey(); - List 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 highlightedLines, int lineIndex, long answerId, - List 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(); } } diff --git a/backend/src/test/java/reviewme/highlight/domain/HighlightedLinesTest.java b/backend/src/test/java/reviewme/highlight/domain/HighlightedLinesTest.java index 53d81c209..d3e4a443c 100644 --- a/backend/src/test/java/reviewme/highlight/domain/HighlightedLinesTest.java +++ b/backend/src/test/java/reviewme/highlight/domain/HighlightedLinesTest.java @@ -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 highlights = lines.toHighlights(1L); + + // then + assertThat(highlights).containsExactly( + new Highlight(1L, 0, new HighlightRange(0, 0)), + new Highlight(1L, 2, new HighlightRange(2, 2)) + ); + } }