Skip to content

Commit

Permalink
게시글 등록/ 수정시 이미지를 batch insert 하도록 수정함 (#528)
Browse files Browse the repository at this point in the history
* feat: 이미지 저장시 batch insert 하도록 수정

* feat: 게시글 수정시에도 이미지 정보를 batch update를 사용하도록 수정함
  • Loading branch information
jyeost authored Nov 20, 2023
1 parent e2dfa4d commit 151ecba
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edonymyeon.backend.image.postimage.repository;

import edonymyeon.backend.image.postimage.domain.PostImageInfo;

import java.util.List;

public interface PostImageInfoCustomRepository {

void batchSave(final List<PostImageInfo> imageInfos, final Long postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edonymyeon.backend.image.postimage.repository;

import edonymyeon.backend.image.postimage.domain.PostImageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

@RequiredArgsConstructor
public class PostImageInfoCustomRepositoryImpl implements PostImageInfoCustomRepository {

private final JdbcTemplate jdbcTemplate;

@Override
public void batchSave(final List<PostImageInfo> imageInfos, final Long postId) {
final LocalDateTime now = LocalDateTime.now();
jdbcTemplate.batchUpdate("INSERT INTO post_image_info (created_at, post_id, store_name) values (?, ?, ?)",
new BatchPreparedStatementSetter() {
@Override
public void setValues(final PreparedStatement ps, final int i) throws SQLException {
ps.setObject(1, now);
ps.setLong(2, postId);
ps.setString(3, imageInfos.get(i).getStoreName());
}

@Override
public int getBatchSize() {
return imageInfos.size();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.util.List;

public interface PostImageInfoRepository extends JpaRepository<PostImageInfo, Long> {
public interface PostImageInfoRepository extends JpaRepository<PostImageInfo, Long>, PostImageInfoCustomRepository {

List<PostImageInfo> findAllByPostId(final Long postId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public PostIdResponse createPost(final MemberId memberId, final PostRequest post

final PostImageInfos postImageInfos = PostImageInfos.of(post,
imageService.saveAll(postRequest.newImages(), ImageType.POST));
postImageInfoRepository.saveAll(postImageInfos.getPostImageInfos());
postImageInfoRepository.batchSave(postImageInfos.getPostImageInfos(), post.getId());

return new PostIdResponse(post.getId());
}
Expand Down Expand Up @@ -133,8 +133,7 @@ public PostIdResponse updatePost(

final List<Long> imageIdsToDelete = post.getImageIdsToDeleteBy(remainedImageNames, imagesToAdd);
postImageInfoRepository.deleteAllByIds(imageIdsToDelete); //이때 기존 이미지중 삭제되는 것들은 softDelete

postImageInfoRepository.saveAll(imagesToAdd.getPostImageInfos()); // //새로 추가된 이미지들을 DB에 저장
postImageInfoRepository.batchSave(imagesToAdd.getPostImageInfos(), post.getId()); //새로 추가된 이미지들을 DB에 저장

return new PostIdResponse(postId);
}
Expand Down

0 comments on commit 151ecba

Please sign in to comment.