diff --git a/backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepository.java b/backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepository.java new file mode 100644 index 000000000..b1e20ef36 --- /dev/null +++ b/backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepository.java @@ -0,0 +1,9 @@ +package reviewme.highlight.repository; + +import java.util.Collection; +import reviewme.highlight.domain.Highlight; + +public interface HighlightJdbcRepository { + + void saveAll(Collection highlights); +} diff --git a/backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepositoryImpl.java b/backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepositoryImpl.java new file mode 100644 index 000000000..077f6a531 --- /dev/null +++ b/backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepositoryImpl.java @@ -0,0 +1,24 @@ +package reviewme.highlight.repository; + +import java.util.Collection; +import lombok.RequiredArgsConstructor; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.core.namedparam.SqlParameterSource; +import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils; +import reviewme.highlight.domain.Highlight; + +@RequiredArgsConstructor +public class HighlightJdbcRepositoryImpl implements HighlightJdbcRepository { + + private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + @Override + public void saveAll(Collection highlights) { + SqlParameterSource[] parameterSources = SqlParameterSourceUtils.createBatch(highlights.toArray()); + String insertSql = """ + INSERT INTO highlight (answer_id, line_index, start_index, end_index) + VALUES (:answerId, :lineIndex, :highlightRange.startIndex, :highlightRange.endIndex) + """; + namedParameterJdbcTemplate.batchUpdate(insertSql, parameterSources); + } +} diff --git a/backend/src/main/java/reviewme/highlight/repository/HighlightRepository.java b/backend/src/main/java/reviewme/highlight/repository/HighlightRepository.java index 74760e09c..104e4cfc1 100644 --- a/backend/src/main/java/reviewme/highlight/repository/HighlightRepository.java +++ b/backend/src/main/java/reviewme/highlight/repository/HighlightRepository.java @@ -2,12 +2,16 @@ import java.util.Collection; import java.util.List; -import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; import reviewme.highlight.domain.Highlight; -public interface HighlightRepository extends JpaRepository { +public interface HighlightRepository extends Repository, HighlightJdbcRepository { + + Highlight save(Highlight highlight); + + boolean existsById(long id); @Modifying @Query(""" diff --git a/backend/src/test/java/reviewme/highlight/repository/HighlightRepositoryTest.java b/backend/src/test/java/reviewme/highlight/repository/HighlightRepositoryTest.java index 40b584f47..6e46ed088 100644 --- a/backend/src/test/java/reviewme/highlight/repository/HighlightRepositoryTest.java +++ b/backend/src/test/java/reviewme/highlight/repository/HighlightRepositoryTest.java @@ -16,6 +16,24 @@ class HighlightRepositoryTest { @Autowired private HighlightRepository highlightRepository; + @Test + void 한_번에_여러_하이라이트를_벌크_삽입한다() { + // given + List highlights = List.of( + new Highlight(1L, 1, new HighlightRange(1, 2)), + new Highlight(1L, 1, new HighlightRange(3, 5)) + ); + + // when + highlightRepository.saveAll(highlights); + + // then + List actual = highlightRepository.findAllByAnswerIdsOrderedAsc(List.of(1L)); + assertThat(actual) + .extracting(Highlight::getHighlightRange) + .containsExactly(new HighlightRange(1, 2), new HighlightRange(3, 5)); + } + @Test void 하이라이트를_줄번호_시작_인덱스_순서대로_정렬해서_가져온다() { // given diff --git a/backend/src/test/java/reviewme/highlight/service/HighlightServiceTest.java b/backend/src/test/java/reviewme/highlight/service/HighlightServiceTest.java index 207a68d29..c4ea9f3bb 100644 --- a/backend/src/test/java/reviewme/highlight/service/HighlightServiceTest.java +++ b/backend/src/test/java/reviewme/highlight/service/HighlightServiceTest.java @@ -102,7 +102,7 @@ class HighlightServiceTest { highlightService.editHighlight(highlightsRequest, reviewGroup); // then - List highlights = highlightRepository.findAll(); + List highlights = highlightRepository.findAllByAnswerIdsOrderedAsc(List.of(textAnswer.getId())); assertAll( () -> assertThat(highlights.get(0).getAnswerId()).isEqualTo(textAnswer.getId()), () -> assertThat(highlights.get(0).getHighlightRange()).isEqualTo(