From 9bda0e4c6ee00d410534fc8df15031b1a23db4ec Mon Sep 17 00:00:00 2001 From: donghoony Date: Wed, 13 Nov 2024 21:11:25 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=95=98=EC=9D=B4=EB=9D=BC=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8=20=EB=B2=8C=ED=81=AC=20=EC=82=BD=EC=9E=85=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/HighlightJdbcRepository.java | 9 ++++++ .../HighlightJdbcRepositoryImpl.java | 28 +++++++++++++++++++ .../repository/HighlightRepository.java | 8 ++++-- .../repository/HighlightRepositoryTest.java | 16 +++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepository.java create mode 100644 backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepositoryImpl.java 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..52e20c98a --- /dev/null +++ b/backend/src/main/java/reviewme/highlight/repository/HighlightJdbcRepositoryImpl.java @@ -0,0 +1,28 @@ +package reviewme.highlight.repository; + +import java.util.Collection; +import lombok.RequiredArgsConstructor; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import reviewme.highlight.domain.Highlight; + +@Repository +@RequiredArgsConstructor +public class HighlightJdbcRepositoryImpl implements HighlightJdbcRepository { + + private final JdbcTemplate jdbcTemplate; + + @Override + public void saveAll(Collection highlights) { + jdbcTemplate.batchUpdate( + "INSERT INTO highlight (answer_id, line_index, start_index, end_index) VALUES (?, ?, ?, ?)", + highlights, + highlights.size(), + (ps, highlight) -> { + ps.setLong(1, highlight.getAnswerId()); + ps.setInt(2, highlight.getLineIndex()); + ps.setInt(3, highlight.getHighlightRange().getStartIndex()); + ps.setInt(4, highlight.getHighlightRange().getEndIndex()); + }); + } +} 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..534baf7c5 100644 --- a/backend/src/test/java/reviewme/highlight/repository/HighlightRepositoryTest.java +++ b/backend/src/test/java/reviewme/highlight/repository/HighlightRepositoryTest.java @@ -16,6 +16,22 @@ class HighlightRepositoryTest { @Autowired private HighlightRepository highlightRepository; + @Test + void 한_번에_여러_하이라이트를_벌크_삽입한다() { + // given + List highlights = List.of( + new Highlight(1L, 1, new HighlightRange(1, 2)), + new Highlight(1L, 2, new HighlightRange(2, 3)) + ); + + // when + highlightRepository.saveAll(highlights); + + // then + List actual = highlightRepository.findAllByAnswerIdsOrderedAsc(List.of(1L)); + assertThat(actual).containsExactlyElementsOf(highlights); + } + @Test void 하이라이트를_줄번호_시작_인덱스_순서대로_정렬해서_가져온다() { // given