Skip to content

Commit

Permalink
[BE] feat: 하이라이트 벌크 삽입 추가 (#961)
Browse files Browse the repository at this point in the history
* feat: 하이라이트 벌크 삽입 추가

* refactor: 불필요한 `@Repository` 제거

* fix: `findAll` 사용하지 않도록 수정

* fix: 테스트 수정

* refactor: `NamedParameterJdbcTemplate` 사용하도록 수정
  • Loading branch information
donghoony authored Nov 17, 2024
1 parent 55db0f6 commit 8858e02
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 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,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<Highlight> 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);
}
}
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,24 @@ 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, 1, new HighlightRange(3, 5))
);

// when
highlightRepository.saveAll(highlights);

// then
List<Highlight> actual = highlightRepository.findAllByAnswerIdsOrderedAsc(List.of(1L));
assertThat(actual)
.extracting(Highlight::getHighlightRange)
.containsExactly(new HighlightRange(1, 2), new HighlightRange(3, 5));
}

@Test
void 하이라이트를_줄번호_시작_인덱스_순서대로_정렬해서_가져온다() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class HighlightServiceTest {
highlightService.editHighlight(highlightsRequest, reviewGroup);

// then
List<Highlight> highlights = highlightRepository.findAll();
List<Highlight> highlights = highlightRepository.findAllByAnswerIdsOrderedAsc(List.of(textAnswer.getId()));
assertAll(
() -> assertThat(highlights.get(0).getAnswerId()).isEqualTo(textAnswer.getId()),
() -> assertThat(highlights.get(0).getHighlightRange()).isEqualTo(
Expand Down

0 comments on commit 8858e02

Please sign in to comment.