Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

게시글 등록/ 수정시 이미지를 batch insert 하도록 수정함 #528

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading