Skip to content

Commit

Permalink
feat: 하이라이트 벌크 삽입 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
donghoony committed Nov 13, 2024
1 parent cfdaeac commit 9bda0e4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package reviewme.highlight.repository;

import java.util.Collection;
import reviewme.highlight.domain.Highlight;

public interface HighlightJdbcRepository {

void saveAll(Collection<Highlight> highlights);
}
Original file line number Diff line number Diff line change
@@ -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<Highlight> 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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Highlight, Long> {
public interface HighlightRepository extends Repository<Highlight, Long>, HighlightJdbcRepository {

Highlight save(Highlight highlight);

boolean existsById(long id);

@Modifying
@Query("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ class HighlightRepositoryTest {
@Autowired
private HighlightRepository highlightRepository;

@Test
void 한_번에_여러_하이라이트를_벌크_삽입한다() {
// given
List<Highlight> 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<Highlight> actual = highlightRepository.findAllByAnswerIdsOrderedAsc(List.of(1L));
assertThat(actual).containsExactlyElementsOf(highlights);
}

@Test
void 하이라이트를_줄번호_시작_인덱스_순서대로_정렬해서_가져온다() {
// given
Expand Down

0 comments on commit 9bda0e4

Please sign in to comment.