Skip to content

Commit

Permalink
여러 BlockStrategy의 saveBlocks 메서드 리팩토링 (Fastcampus-Final-Team3#191)
Browse files Browse the repository at this point in the history
* recommit : processBlocks 메서드 분리 오류 해결

* feat : save 메서드를 saveAll로 변경하기 위해 saveAll 메서드 추가 (Fastcampus-Final-Team3#190)

* refactor : 여러 BlockStrategy의 saveBlocks 메서드 분리 및 saveAll 사용 (Fastcampus-Final-Team3#190)

1. subData를 각 SaveRequest로 변환하는 부분을 convertSubDataToBlockSaveRequests 메서드로 분리
2. SaveRequest를 Entity로 변환하는 부분을 convertToBlocks 메서드로 분리
3. 모든 Entity를 저장하는 부분을 saveAllBlock 메서드로 변경하여 한 번에 모든 Entity들을 저장
4. 저장된 블록들의 정보를 배열에 추가하는 부분을 addToBlockInfoArray 메서드로 분리
  • Loading branch information
dpdmstjs authored Oct 24, 2023
1 parent eb9ccf4 commit 1f6f5e1
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import com.javajober.core.exception.ApplicationException;
import org.springframework.data.repository.Repository;

import java.util.List;
import java.util.Optional;

public interface FileBlockRepository extends Repository<FileBlock, Long> {

FileBlock save(final FileBlock fileBlock);

List<FileBlock> saveAll(final Iterable<FileBlock> fileBlocks);

Optional<FileBlock> findByIdAndDeletedAtIsNull(final Long id);

default FileBlock findFileBlock(final Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import com.javajober.blocks.freeBlock.domain.FreeBlock;
import org.springframework.data.repository.Repository;

import java.util.List;
import java.util.Optional;

public interface FreeBlockRepository extends Repository<FreeBlock, Long> {

FreeBlock save(final FreeBlock freeBlock);

List<FreeBlock> saveAll(final Iterable<FreeBlock> freeBlocks);

Optional<FreeBlock> findByIdAndDeletedAtIsNull(final Long freeId);

default FreeBlock findFreeBlock(final Long freeId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.javajober.blocks.listBlock.repository;

import java.util.List;
import java.util.Optional;

import com.javajober.blocks.listBlock.domain.ListBlock;
Expand All @@ -12,6 +13,8 @@ public interface ListBlockRepository extends Repository<ListBlock, Long> {

ListBlock save(final ListBlock listBlock);

List<ListBlock> saveAll(final Iterable<ListBlock> listBlocks);

Optional<ListBlock> findByIdAndDeletedAtIsNull(final Long id);

default ListBlock findListBlock(final Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.javajober.blocks.freeBlock.dto.request.FreeBlockSaveRequest;
import com.javajober.blocks.styleSetting.backgroundSetting.domain.BackgroundSetting;
import com.javajober.blocks.styleSetting.backgroundSetting.dto.request.BackgroundSettingStringSaveRequest;
import com.javajober.blocks.styleSetting.backgroundSetting.filedto.BackgroundSettingSaveRequest;
import com.javajober.blocks.styleSetting.backgroundSetting.filedto.BackgroundSettingUpdateRequest;
import com.javajober.blocks.styleSetting.backgroundSetting.repository.BackgroundSettingRepository;
import com.javajober.blocks.styleSetting.blockSetting.domain.BlockSetting;
import com.javajober.blocks.styleSetting.blockSetting.dto.request.BlockSettingSaveRequest;
import com.javajober.blocks.styleSetting.blockSetting.dto.request.BlockSettingUpdateRequest;
import com.javajober.blocks.styleSetting.blockSetting.repository.BlockSettingRepository;
import com.javajober.blocks.styleSetting.dto.request.StyleSettingStringSaveRequest;
import com.javajober.blocks.styleSetting.themeSetting.dto.request.ThemeSettingSaveRequest;
import com.javajober.core.util.file.FileImageService;
import com.javajober.blocks.fileBlock.domain.FileBlock;
import com.javajober.blocks.fileBlock.filedto.FileBlockSaveRequest;
import com.javajober.blocks.fileBlock.filedto.FileBlockUpdateRequest;
import com.javajober.blocks.fileBlock.repository.FileBlockRepository;
import com.javajober.blocks.freeBlock.domain.FreeBlock;
import com.javajober.blocks.freeBlock.dto.request.FreeBlockSaveRequest;
import com.javajober.blocks.freeBlock.dto.request.FreeBlockUpdateRequest;
import com.javajober.blocks.freeBlock.repository.FreeBlockRepository;
import com.javajober.blocks.listBlock.domain.ListBlock;
Expand Down Expand Up @@ -139,7 +136,7 @@ public SpaceWallSaveResponse save(final SpaceWallSaveRequest spaceWallSaveReques
Long position = blocksPositionCounter.getAndIncrement();
switch (blockType) {
case FREE_BLOCK:
List<FreeBlockSaveRequest> freeBlockRequests = jsonMapper.convertValue(block.getSubData(),
List<com.javajober.blocks.freeBlock.dto.request.FreeBlockSaveRequest> freeBlockRequests = jsonMapper.convertValue(block.getSubData(),
new TypeReference<List<FreeBlockSaveRequest>>() {
});
List<Long> freeBlockIds = saveFreeBlocks(freeBlockRequests);
Expand Down Expand Up @@ -272,11 +269,11 @@ private Long saveWallInfoBlock(final WallInfoBlockSaveRequest wallInfoBlockSaveR
return wallInfoBlockRepository.save(wallInfoBlock).getId();
}

private List<Long> saveFreeBlocks(final List<FreeBlockSaveRequest> subData) {
private List<Long> saveFreeBlocks(final List<com.javajober.blocks.freeBlock.dto.request.FreeBlockSaveRequest> subData) {

List<Long> freeBlockIds = new ArrayList<>();
subData.forEach(block -> {
FreeBlock freeBlock = FreeBlockSaveRequest.toEntity(block);
com.javajober.blocks.freeBlock.domain.FreeBlock freeBlock = com.javajober.blocks.freeBlock.dto.request.FreeBlockSaveRequest.toEntity(block);
freeBlockIds.add(freeBlockRepository.save(freeBlock).getId());
});
return freeBlockIds;
Expand Down Expand Up @@ -357,10 +354,10 @@ private List<Long> updateFreeBlocks(final List<FreeBlockUpdateRequest> subData)
List<Long> updatedFreeBlockIds = new ArrayList<>();
for (FreeBlockUpdateRequest updateRequest : subData) {
if(updateRequest.getFreeBlockId() == null ){
FreeBlock freeBlock = new FreeBlock(updateRequest.getFreeTitle(),updateRequest.getFreeContent());
com.javajober.blocks.freeBlock.domain.FreeBlock freeBlock = new com.javajober.blocks.freeBlock.domain.FreeBlock(updateRequest.getFreeTitle(),updateRequest.getFreeContent());
updatedFreeBlockIds.add(freeBlockRepository.save(freeBlock).getId());
}else {
FreeBlock freeBlock = freeBlockRepository.findFreeBlock(updateRequest.getFreeBlockId());
com.javajober.blocks.freeBlock.domain.FreeBlock freeBlock = freeBlockRepository.findFreeBlock(updateRequest.getFreeBlockId());
freeBlock.update(updateRequest);
updatedFreeBlockIds.add(freeBlockRepository.save(freeBlock).getId());
}
Expand Down
31 changes: 17 additions & 14 deletions src/main/java/com/javajober/spaceWall/service/SpaceWallService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.javajober.space.domain.AddSpace;
import com.javajober.blocks.fileBlock.domain.FileBlock;
import com.javajober.blocks.fileBlock.repository.FileBlockRepository;
import com.javajober.blocks.freeBlock.domain.FreeBlock;
import com.javajober.blocks.freeBlock.repository.FreeBlockRepository;
import com.javajober.blocks.listBlock.domain.ListBlock;
import com.javajober.blocks.listBlock.repository.ListBlockRepository;
Expand Down Expand Up @@ -128,16 +127,7 @@ public SpaceWallSaveResponse save(final Long memberId, final SpaceWallStringRequ
processWallInfoBlock(data, blockInfoArray, blocksPositionCounter);

List<BlockSaveRequest<?>> blocks = data.getBlocks();
blocks.forEach(block -> {

BlockType blockType = BlockType.findBlockTypeByString(block.getBlockType());
Long position = blocksPositionCounter.getAndIncrement();

String strategyName = blockType.getStrategyName();
MoveBlockStrategy blockProcessingStrategy = blockStrategyFactory.findMoveBlockStrategy(strategyName);

blockProcessingStrategy.saveBlocks(block.getSubData(), blockInfoArray, position);
});
processBlocks(blocks, blockInfoArray, blocksPositionCounter);

processStyleSettingBlock(data, blockInfoArray, blocksPositionCounter);

Expand All @@ -147,7 +137,7 @@ public SpaceWallSaveResponse save(final Long memberId, final SpaceWallStringRequ
return new SpaceWallSaveResponse(spaceWallId);
}

private void validateSpaceOwnership(Member member, AddSpace addSpace) {
private void validateSpaceOwnership(final Member member, final AddSpace addSpace) {
Long memberId = member.getId();
Long spaceId = addSpace.getMember().getId();

Expand All @@ -171,6 +161,19 @@ private void processWallInfoBlock(final DataStringSaveRequest data, final ArrayN
wallInfoBlockStrategy.saveBlocks(data, blockInfoArray, wallInfoBlockPosition);
}

private void processBlocks(final List<BlockSaveRequest<?>> blocks, final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter) {
blocks.forEach(block -> {

BlockType blockType = BlockType.findBlockTypeByString(block.getBlockType());
Long position = blocksPositionCounter.getAndIncrement();

String strategyName = blockType.getStrategyName();
MoveBlockStrategy blockProcessingStrategy = blockStrategyFactory.findMoveBlockStrategy(strategyName);

blockProcessingStrategy.saveBlocks(block.getSubData(), blockInfoArray, position);
});
}

private void processStyleSettingBlock(final DataStringSaveRequest data, final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter) {

String styleSettingBlockStrategyName = BlockType.STYLE_SETTING.getStrategyName();
Expand Down Expand Up @@ -272,10 +275,10 @@ private List<Long> updateFreeBlocks(final List<FreeBlockUpdateRequest> subData)
List<Long> updatedFreeBlockIds = new ArrayList<>();
for (FreeBlockUpdateRequest updateRequest : subData) {
if(updateRequest.getFreeBlockId() == null ){
FreeBlock freeBlock = new FreeBlock(updateRequest.getFreeTitle(),updateRequest.getFreeContent());
com.javajober.blocks.freeBlock.domain.FreeBlock freeBlock = new com.javajober.blocks.freeBlock.domain.FreeBlock(updateRequest.getFreeTitle(),updateRequest.getFreeContent());
updatedFreeBlockIds.add(freeBlockRepository.save(freeBlock).getId());
}else {
FreeBlock freeBlock = freeBlockRepository.findFreeBlock(updateRequest.getFreeBlockId());
com.javajober.blocks.freeBlock.domain.FreeBlock freeBlock = freeBlockRepository.findFreeBlock(updateRequest.getFreeBlockId());
freeBlock.update(updateRequest);
updatedFreeBlockIds.add(freeBlockRepository.save(freeBlock).getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand All @@ -24,7 +25,6 @@ public class FileBlockStrategy implements MoveBlockStrategy {
private final BlockJsonProcessor blockJsonProcessor;
private final FileBlockRepository fileBlockRepository;


public FileBlockStrategy(BlockJsonProcessor blockJsonProcessor, FileBlockRepository fileBlockRepository) {
this.blockJsonProcessor = blockJsonProcessor;
this.fileBlockRepository = fileBlockRepository;
Expand All @@ -33,21 +33,43 @@ public FileBlockStrategy(BlockJsonProcessor blockJsonProcessor, FileBlockReposit
@Override
public void saveBlocks(final List<?> subData, final ArrayNode blockInfoArray, final Long position) {

List<FileBlockStringSaveRequest> fileBlockRequests = convertSubDataToFileBlockSaveRequests(subData);

List<FileBlock> fileBlocks = convertToFileBlocks(fileBlockRequests);

List<FileBlock> savedFileBlocks = saveAllFileBlock(fileBlocks);

addToFileBlockInfoArray(savedFileBlocks, blockInfoArray, position);
}

private List<FileBlockStringSaveRequest> convertSubDataToFileBlockSaveRequests(final List<?> subData) {
List<FileBlockStringSaveRequest> fileBlockRequests = new ArrayList<>();

subData.forEach(block -> {
FileBlockStringSaveRequest request = blockJsonProcessor.convertValue(block, FileBlockStringSaveRequest.class);
Long fileBlockId = saveFreeBlock(request);
blockJsonProcessor.addBlockInfoToArray(blockInfoArray, fileBlockId, FILE_BLOCK, fileBlockId, "");
fileBlockRequests.add(request);
});
return fileBlockRequests;
}

private Long saveFreeBlock(FileBlockStringSaveRequest request) {
FileBlock fileBlock = FileBlockStringSaveRequest.toEntity(request);
return fileBlockRepository.save(fileBlock).getId();
private List<FileBlock> convertToFileBlocks(final List<FileBlockStringSaveRequest> fileBlockSaveRequests) {
return fileBlockSaveRequests.stream()
.map(FileBlockStringSaveRequest::toEntity)
.collect(Collectors.toList());
}

private List<FileBlock> saveAllFileBlock(final List<FileBlock> fileBlocks) {
return fileBlockRepository.saveAll(fileBlocks);
}

private void addToFileBlockInfoArray (final List<FileBlock> savedFileBlocks, final ArrayNode blockInfoArray, final Long position) {
savedFileBlocks.forEach(savedFileBlock ->
blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, FILE_BLOCK, savedFileBlock.getId(), "")
);
}

@Override
public List<CommonResponse> createMoveBlockDTO(List<JsonNode> blocksWithSamePosition) {
public List<CommonResponse> createMoveBlockDTO(final List<JsonNode> blocksWithSamePosition) {
List<CommonResponse> subData = new ArrayList<>();
for (JsonNode block : blocksWithSamePosition) {
long blockId = block.path("block_id").asLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.javajober.blocks.freeBlock.domain.FreeBlock;
import com.javajober.blocks.freeBlock.dto.request.FreeBlockSaveRequest;
import com.javajober.blocks.freeBlock.dto.response.FreeBlockResponse;
import com.javajober.core.util.response.CommonResponse;
import org.springframework.stereotype.Component;

import com.javajober.blocks.freeBlock.domain.FreeBlock;
import com.javajober.blocks.freeBlock.dto.request.FreeBlockSaveRequest;
import com.javajober.blocks.freeBlock.repository.FreeBlockRepository;
import com.javajober.spaceWall.domain.BlockType;
import com.javajober.spaceWall.strategy.BlockJsonProcessor;
Expand All @@ -31,24 +32,47 @@ public FreeBlockStrategy(final BlockJsonProcessor blockJsonProcessor, final Free
@Override
public void saveBlocks(final List<?> subData, final ArrayNode blockInfoArray, final Long position) {

List<FreeBlockSaveRequest> freeBlockRequests = convertSubDataToFreeBlockSaveRequests(subData);

List<FreeBlock> freeBlocks = convertToFreeBlocks(freeBlockRequests);

List<FreeBlock> savedFreeBlocks = saveAllFreeBlock(freeBlocks);

addToFreeBlockInfoArray(savedFreeBlocks, blockInfoArray, position);
}

private List<FreeBlockSaveRequest> convertSubDataToFreeBlockSaveRequests(final List<?> subData) {
List<FreeBlockSaveRequest> freeBlockRequests = new ArrayList<>();

subData.forEach(block -> {
FreeBlockSaveRequest request = blockJsonProcessor.convertValue(block, FreeBlockSaveRequest.class);
Long freeBlockId = saveFreeBlock(request);
blockJsonProcessor.addBlockInfoToArray(blockInfoArray, freeBlockId, FREE_BLOCK, freeBlockId, "");
freeBlockRequests.add(request);
});
return freeBlockRequests;
}

private List<FreeBlock> convertToFreeBlocks(final List<FreeBlockSaveRequest> freeBlockRequests) {
return freeBlockRequests.stream()
.map(FreeBlockSaveRequest::toEntity)
.collect(Collectors.toList());
}

private List<FreeBlock> saveAllFreeBlock(final List<FreeBlock> freeBlocks) {
return freeBlockRepository.saveAll(freeBlocks);
}

private Long saveFreeBlock(FreeBlockSaveRequest request) {
FreeBlock freeBlock = FreeBlockSaveRequest.toEntity(request);
return freeBlockRepository.save(freeBlock).getId();
private void addToFreeBlockInfoArray (final List<FreeBlock> savedFreeBlocks, final ArrayNode blockInfoArray, final Long position) {
savedFreeBlocks.forEach(savedFreeBlock ->
blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, FREE_BLOCK, savedFreeBlock.getId(), "")
);
}

@Override
public List<CommonResponse> createMoveBlockDTO(List<JsonNode> blocksWithSamePosition) {
public List<CommonResponse> createMoveBlockDTO(final List<JsonNode> blocksWithSamePosition) {
List<CommonResponse> subData = new ArrayList<>();
for (JsonNode block : blocksWithSamePosition) {
long blockId = block.path("block_id").asLong();
FreeBlock freeBlock = freeBlockRepository.findFreeBlock(blockId);
com.javajober.blocks.freeBlock.domain.FreeBlock freeBlock = freeBlockRepository.findFreeBlock(blockId);
subData.add(FreeBlockResponse.from(freeBlock));
}
return subData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -32,20 +33,43 @@ public ListBlockStrategy(final BlockJsonProcessor blockJsonProcessor, final List
@Override
public void saveBlocks(final List<?> subData, final ArrayNode blockInfoArray, final Long position) {

List<ListBlockSaveRequest> listBlockRequests = convertSubDataToListBlockSaveRequests(subData);

List<ListBlock> listBlocks = convertToListBlocks(listBlockRequests);

List<ListBlock> savedListBlocks = saveAllListBlock(listBlocks);

addToListBlockInfoArray(savedListBlocks, blockInfoArray, position);
}

private List<ListBlockSaveRequest> convertSubDataToListBlockSaveRequests(final List<?> subData) {
List<ListBlockSaveRequest> listBlockRequests = new ArrayList<>();

subData.forEach(block -> {
ListBlockSaveRequest request = blockJsonProcessor.convertValue(block, ListBlockSaveRequest.class);
ListBlock listBlock = saveListBlock(request);
blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, LIST_BLOCK, listBlock.getId(), listBlock.getListUUID());
listBlockRequests.add(request);
});
return listBlockRequests;
}

private List<ListBlock> convertToListBlocks(final List<ListBlockSaveRequest> listBlockRequests) {
return listBlockRequests.stream()
.map(ListBlockSaveRequest::toEntity)
.collect(Collectors.toList());
}

private List<ListBlock> saveAllListBlock(final List<ListBlock> listBlocks) {
return listBlockRepository.saveAll(listBlocks);
}

private ListBlock saveListBlock(ListBlockSaveRequest request) {
ListBlock listBlock = ListBlockSaveRequest.toEntity(request);
return listBlockRepository.save(listBlock);
private void addToListBlockInfoArray (final List<ListBlock> savedListBlocks, final ArrayNode blockInfoArray, final Long position) {
savedListBlocks.forEach(savedListBlock ->
blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, LIST_BLOCK, savedListBlock.getId(), savedListBlock.getListUUID())
);
}

@Override
public List<CommonResponse> createMoveBlockDTO(List<JsonNode> blocksWithSamePosition) {
public List<CommonResponse> createMoveBlockDTO(final List<JsonNode> blocksWithSamePosition) {
List<CommonResponse> subData = new ArrayList<>();
for (JsonNode block : blocksWithSamePosition) {
long blockId = block.path("block_id").asLong();
Expand Down
Loading

0 comments on commit 1f6f5e1

Please sign in to comment.