Skip to content

Commit

Permalink
공유페이지 멀티파트 파일 처리를 위한 수정 리팩토링 완료 (#223)
Browse files Browse the repository at this point in the history
* style : blockProcessingStrategy -> moveBlockStrategy로 변수명 수정 (#222)

* style : 공백 제거 (#222)

* feat : MoveBlockStrategy 인터페이스에 'updateStringBlocks' 메서드 추가 (#222)

* feat : FixBlockStrategy 인터페이스에 'updateStringBlocks' 메서드 추가 (#222)

* refactor : base64와 멀티파트 수정 메서드 분리 (#222)

* style : 불필요한 import 제거 (#222)

* style : FileBlock 클래스에 fileUpdate 메서드 'update' 메서드명으로 변경 (#222)

* feat : FileBlock 엔티티 변환하는 'toEntity' 메서드 추가 (#222)

* feat : BackgroundSetting 엔티티 변환하는 'toEntity' 메서드 추가 (#222)

* refactor : FileBlockStrategy 클래스의 'updateBlocks' 메서드 리팩토링 (#222)

- 멀티파트 파일을 가져오는 로직 추가

* refactor : StyleSettingStrategy 클래스의 'updateBlocks' 메서드 리팩토링 (#222)

- 멀티파트 파일을 가져오는 로직 추가

* refactor : WallInfoBlockStrategy 클래스의 'updateBlocks' 메서드 리팩토링 (#222)

- 멀티파트 파일을 가져오는 로직 추가

* refactor : FileUploadService 클래스에 'update' 메서드 리팩토링 (#222)

 - 멀티파트를 가져올 수 있도록 로직 리팩토링

* fix : 'validateAddSpaceId' 메서드와 'checkDuplicateShareURL' 메서드에 FlagType 매개변수 추가하여 SAVED 상태일 때만 예외 처리하도록 수정
  • Loading branch information
dpdmstjs authored Oct 28, 2023
1 parent 9a41edd commit 7a97b31
Show file tree
Hide file tree
Showing 15 changed files with 339 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public FileBlock(final String fileTitle, final String fileDescription, final Str
this.fileName = fileName;
}

public void fileUpdate(final FileBlockUpdateRequest request, String file) {
public void update(final FileBlockUpdateRequest request, String file) {
this.fileTitle = request.getFileTitle();
this.fileDescription = request.getFileDescription();
this.file = file;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.javajober.blocks.fileBlock.filedto;

import com.javajober.blocks.fileBlock.domain.FileBlock;
import com.javajober.blocks.fileBlock.dto.request.FileBlockStringUpdateRequest;

import lombok.Getter;

@Getter
Expand All @@ -12,4 +15,12 @@ public class FileBlockUpdateRequest {
public FileBlockUpdateRequest() {

}

public static FileBlock toEntity(final FileBlockUpdateRequest request, String file) {
return FileBlock.builder()
.fileTitle(request.getFileTitle())
.fileDescription(request.getFileDescription())
.file(file)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.LocalDateTime;

import com.javajober.blocks.styleSetting.backgroundSetting.dto.request.BackgroundStringUpdateRequest;
import com.javajober.blocks.styleSetting.backgroundSetting.filedto.BackgroundSettingUpdateRequest;

@Getter
@Table(name = "background_setting")
Expand Down Expand Up @@ -57,4 +58,10 @@ public void update(final BackgroundStringUpdateRequest request) {
this.gradation = request.getGradation();
this.styleImageURL = request.getStyleImgURL();
}

public void update(final BackgroundSettingUpdateRequest request, String styleImageURL) {
this.solidColor = request.getSolidColor();
this.gradation = request.getGradation();
this.styleImageURL = styleImageURL;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.javajober.spaceWall.dto.request;

import com.javajober.spaceWall.filedto.DataUpdateRequest;

import lombok.Getter;

@Getter
Expand All @@ -10,8 +8,8 @@ public class SpaceWallStringUpdateRequest {
private DataStringUpdateRequest data;

private SpaceWallStringUpdateRequest() {

}

public SpaceWallStringUpdateRequest(final DataStringUpdateRequest data) {
this.data = data;
}
Expand Down
158 changes: 143 additions & 15 deletions src/main/java/com/javajober/spaceWall/service/FileUploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.javajober.spaceWall.domain.SpaceWall;
import com.javajober.spaceWall.dto.request.BlockSaveRequest;
import com.javajober.spaceWall.filedto.DataSaveRequest;
import com.javajober.spaceWall.filedto.DataUpdateRequest;
import com.javajober.spaceWall.filedto.SpaceWallSaveRequest;
import com.javajober.spaceWall.dto.response.SpaceWallSaveResponse;
import com.javajober.spaceWall.filedto.SpaceWallUpdateRequest;
Expand All @@ -28,7 +29,12 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -42,10 +48,10 @@ public class FileUploadService {
private final BlockJsonProcessor blockJsonProcessor;

public FileUploadService(final SpaceWallRepository spaceWallRepository,
final MemberRepository memberRepository,
final AddSpaceRepository addSpaceRepository,
final BlockStrategyFactory blockStrategyFactory,
final BlockJsonProcessor blockJsonProcessor) {
final MemberRepository memberRepository,
final AddSpaceRepository addSpaceRepository,
final BlockStrategyFactory blockStrategyFactory,
final BlockJsonProcessor blockJsonProcessor) {

this.spaceWallRepository = spaceWallRepository;
this.memberRepository = memberRepository;
Expand All @@ -67,9 +73,9 @@ public SpaceWallSaveResponse save(final Long memberId, final SpaceWallSaveReques

validateSpaceOwnership(member, addSpace);

validateAddSpaceId(addSpace.getId());
validateAddSpaceId(addSpace.getId(), flagType);

checkDuplicateShareURL(data.getShareURL());
checkDuplicateShareURL(data.getShareURL(), flagType);

SpaceWallCategoryType spaceWallCategoryType = SpaceWallCategoryType.findSpaceWallCategoryTypeByString(data.getCategory());

Expand Down Expand Up @@ -101,16 +107,16 @@ private void validateSpaceOwnership(final Member member, final AddSpace addSpace
}
}

private void validateAddSpaceId (final Long spaceId) {
private void validateAddSpaceId (final Long spaceId, FlagType flagType) {
boolean existsSpaceId = spaceWallRepository.existsByAddSpaceId(spaceId);
if (existsSpaceId) {
if (existsSpaceId && flagType == FlagType.SAVED) {
throw new ApplicationException(ApiStatus.INVALID_DATA, "스페이스 하나당 공유페이지 하나만 생성 가능합니다.");
}
}

private void checkDuplicateShareURL(final String shareURL) {
private void checkDuplicateShareURL(final String shareURL, FlagType flagType) {
boolean existsShareURL = spaceWallRepository.existsByShareURLAndFlag(shareURL, FlagType.SAVED);
if (existsShareURL) {
if (existsShareURL && flagType == FlagType.SAVED) {
throw new ApplicationException(ApiStatus.ALREADY_EXIST, "이미 사용중인 shareURL입니다.");
}
}
Expand Down Expand Up @@ -140,13 +146,13 @@ private void processBlocks(final List<BlockSaveRequest<?>> blocks, final ArrayNo
Long position = blocksPositionCounter.getAndIncrement();

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

if (BlockStrategyName.FileBlockStrategy.name().equals(blockProcessingStrategy.getStrategyName())) {
blockProcessingStrategy.uploadFile(files.get(fileIndexCounter.getAndIncrement()));
if (BlockStrategyName.FileBlockStrategy.name().equals(moveBlockStrategy.getStrategyName())) {
moveBlockStrategy.uploadFile(files.get(fileIndexCounter.getAndIncrement()));
}

blockProcessingStrategy.saveBlocks(block, blockInfoArray, position);
moveBlockStrategy.saveBlocks(block, blockInfoArray, position);
});
} catch (IndexOutOfBoundsException e) {
throw new ApplicationException(ApiStatus.INVALID_DATA, "파일이 첨부되지 않은 파일블록이 있습니다.");
Expand Down Expand Up @@ -177,7 +183,129 @@ public SpaceWallSaveResponse update(final Long memberId, final SpaceWallUpdateRe
final List<MultipartFile> files, final MultipartFile backgroundImgURL,
final MultipartFile wallInfoImgURL, final MultipartFile styleImgURL){

DataUpdateRequest data = spaceWallRequest.getData();

return new SpaceWallSaveResponse(1L);
Long spaceWallId = data.getSpaceWallId();

Member member = memberRepository.findMember(memberId);

AddSpace addSpace = addSpaceRepository.findAddSpace(data.getSpaceId());

validateSpaceOwnership(member, addSpace);

SpaceWall spaceWall = spaceWallRepository.findSpaceWall(spaceWallId, addSpace.getId(), memberId, flagType);

ArrayNode blockInfoArray = blockJsonProcessor.createArrayNode();

List<BlockSaveRequest<?>> blocksRequest = data.getBlocks();

AtomicLong blocksPositionCounter = new AtomicLong(INITIAL_POSITION);

updateWallInfoBlock(data, backgroundImgURL, wallInfoImgURL, blockInfoArray, blocksPositionCounter);

Map<BlockType, Set<Long>> existingBlockIdsByType = new HashMap<>();

Map<BlockType, Set<Long>> updatedBlockIdsByType = new HashMap<>();

String existingBlocks = spaceWall.getBlocks();

processBlocks(blocksRequest, files, blockInfoArray, blocksPositionCounter, existingBlockIdsByType, updatedBlockIdsByType, existingBlocks);

deleteRemainingBlocks(existingBlockIdsByType, updatedBlockIdsByType);

updateStyleSettingBlock(data, styleImgURL, blockInfoArray, blocksPositionCounter);

String blocks = blockInfoArray.toString();

spaceWall.fileUpdate(data, flagType, blocks);

spaceWallId = spaceWallRepository.save(spaceWall).getId();

return new SpaceWallSaveResponse(spaceWallId);
}

private void updateWallInfoBlock(final DataUpdateRequest data, final MultipartFile backgroundImgURL, final MultipartFile wallInfoImgURL,
final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter) {

String wallInfoBlockStrategyName = BlockType.WALL_INFO_BLOCK.getStrategyName();

FixBlockStrategy wallInfoBlockStrategy = blockStrategyFactory.findFixBlockStrategy(wallInfoBlockStrategyName);

wallInfoBlockStrategy.uploadTwoFiles(backgroundImgURL, wallInfoImgURL);

Long wallInfoBlockPosition = blocksPositionCounter.getAndIncrement();

wallInfoBlockStrategy.updateBlocks(data, blockInfoArray, wallInfoBlockPosition);
}

private void processBlocks(final List<BlockSaveRequest<?>> blocks, final List<MultipartFile> files, final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter,
final Map<BlockType, Set<Long>> existingBlockIdsByType, final Map<BlockType, Set<Long>> updatedBlockIdsByType, final String existingBlocks) {

try {
AtomicInteger fileIndexCounter = new AtomicInteger();

blocks.forEach(block -> {
BlockType blockType = BlockType.findBlockTypeByString(block.getBlockType());

Long position = blocksPositionCounter.getAndIncrement();
String strategyName = blockType.getStrategyName();
MoveBlockStrategy moveBlockStrategy = blockStrategyFactory.findMoveBlockStrategy(strategyName);

if (BlockStrategyName.FileBlockStrategy.name().equals(moveBlockStrategy.getStrategyName())) {
moveBlockStrategy.uploadFile(files.get(fileIndexCounter.getAndIncrement()));
}

processBlock(block, blockInfoArray, position, existingBlockIdsByType, updatedBlockIdsByType,
existingBlocks, blockType, moveBlockStrategy);
});
} catch (IndexOutOfBoundsException e) {
throw new ApplicationException(ApiStatus.INVALID_DATA, "파일이 첨부되지 않은 파일블록이 있습니다.");
}
}

private void processBlock(final BlockSaveRequest<?> block, final ArrayNode blockInfoArray, final Long position,
final Map<BlockType, Set<Long>> existingBlockIdsByType, final Map<BlockType, Set<Long>> updatedBlockIdsByType, final String existingBlocks, final BlockType blockType, MoveBlockStrategy moveBlockStrategy) {

if (!existingBlockIdsByType.containsKey(blockType)) {
Set<Long> existingBlockIdsForThisType = blockJsonProcessor.existingBlockIds(existingBlocks, blockType);
existingBlockIdsByType.put(blockType, existingBlockIdsForThisType);
}

Set<Long> updatedIds = moveBlockStrategy.updateBlocks(block, blockInfoArray, position);

updatedIds.forEach(blockId ->
blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, blockType, blockId, block.getBlockUUID()));

if (!updatedBlockIdsByType.containsKey(blockType)) {
updatedBlockIdsByType.put(blockType, new HashSet<>());
}

updatedBlockIdsByType.get(blockType).addAll(updatedIds);
}

private void deleteRemainingBlocks(final Map<BlockType, Set<Long>> existingBlockIdsByType, final Map<BlockType, Set<Long>> updatedBlockIdsByType) {

for (BlockType blockType : existingBlockIdsByType.keySet()) {
Set<Long> remainingBlockIds = existingBlockIdsByType.get(blockType);
remainingBlockIds.removeAll(updatedBlockIdsByType.getOrDefault(blockType, Collections.emptySet()));

if (!remainingBlockIds.isEmpty()) {
MoveBlockStrategy moveBlockStrategy = blockStrategyFactory.findMoveBlockStrategy((blockType.getStrategyName()));
moveBlockStrategy.deleteAllById(remainingBlockIds);
}
}
}

private void updateStyleSettingBlock(final DataUpdateRequest data, final MultipartFile styleImgURL, final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter) {

String styleSettingBlockStrategyName = BlockType.STYLE_SETTING.getStrategyName();

FixBlockStrategy styleSettingBlockStrategy = blockStrategyFactory.findFixBlockStrategy(styleSettingBlockStrategyName);

styleSettingBlockStrategy.uploadSingleFile(styleImgURL);

Long styleSettingPosition = blocksPositionCounter.getAndIncrement();

styleSettingBlockStrategy.updateBlocks(data, blockInfoArray, styleSettingPosition);
}
}
25 changes: 13 additions & 12 deletions src/main/java/com/javajober/spaceWall/service/SpaceWallService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public class SpaceWallService {
private final BlockJsonProcessor blockJsonProcessor;

public SpaceWallService(final SpaceWallRepository spaceWallRepository,
final MemberRepository memberRepository,
final AddSpaceRepository addSpaceRepository,
final BlockStrategyFactory blockStrategyFactory,
final BlockJsonProcessor blockJsonProcessor) {
final MemberRepository memberRepository,
final AddSpaceRepository addSpaceRepository,
final BlockStrategyFactory blockStrategyFactory,
final BlockJsonProcessor blockJsonProcessor) {

this.spaceWallRepository = spaceWallRepository;
this.memberRepository = memberRepository;
Expand Down Expand Up @@ -129,9 +129,9 @@ private void processBlocks(final List<BlockSaveRequest<?>> blocks, final ArrayNo
Long position = blocksPositionCounter.getAndIncrement();

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

blockProcessingStrategy.saveStringBlocks(block, blockInfoArray, position);
moveBlockStrategy.saveStringBlocks(block, blockInfoArray, position);
});
}

Expand Down Expand Up @@ -212,11 +212,12 @@ private void updateWallInfoBlock(final DataStringUpdateRequest data, final Array
FixBlockStrategy wallInfoBlockStrategy = blockStrategyFactory.findFixBlockStrategy(wallInfoBlockStrategyName);

Long wallInfoBlockPosition = blocksPositionCounter.getAndIncrement();
wallInfoBlockStrategy.updateBlocks(data, blockInfoArray, wallInfoBlockPosition);
wallInfoBlockStrategy.updateStringBlocks(data, blockInfoArray, wallInfoBlockPosition);
}

private void processBlocks(final List<BlockSaveRequest<?>> blocksRequest, final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter,
final Map<BlockType, Set<Long>> existingBlockIdsByType, final Map<BlockType, Set<Long>> updatedBlockIdsByType, final String existingBlocks) {
final Map<BlockType, Set<Long>> existingBlockIdsByType, final Map<BlockType, Set<Long>> updatedBlockIdsByType, final String existingBlocks) {

blocksRequest.forEach(block -> {
BlockType blockType = BlockType.findBlockTypeByString(block.getBlockType());
Long position = blocksPositionCounter.getAndIncrement();
Expand All @@ -234,7 +235,7 @@ private void processBlock(final BlockSaveRequest<?> block, final ArrayNode block
existingBlockIdsByType.put(blockType, existingBlockIdsForThisType);
}

Set<Long> updatedIds = moveBlockStrategy.updateBlocks(block, blockInfoArray, position);
Set<Long> updatedIds = moveBlockStrategy.updateStringBlocks(block, blockInfoArray, position);

updatedIds.forEach(blockId ->
blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, blockType, blockId, block.getBlockUUID()));
Expand All @@ -250,9 +251,9 @@ private void deleteRemainingBlocks(final Map<BlockType, Set<Long>> existingBlock
Set<Long> remainingBlockIds = existingBlockIdsByType.get(blockType);
remainingBlockIds.removeAll(updatedBlockIdsByType.getOrDefault(blockType, Collections.emptySet()));
if (!remainingBlockIds.isEmpty()) {
MoveBlockStrategy blockProcessingStrategy = blockStrategyFactory.findMoveBlockStrategy(
MoveBlockStrategy moveBlockStrategy = blockStrategyFactory.findMoveBlockStrategy(
(blockType.getStrategyName()));
blockProcessingStrategy.deleteAllById(remainingBlockIds);
moveBlockStrategy.deleteAllById(remainingBlockIds);
}
}
}
Expand All @@ -263,6 +264,6 @@ private void updateStyleSettingBlock(final DataStringUpdateRequest data, final A
FixBlockStrategy styleSettingBlockStrategy = blockStrategyFactory.findFixBlockStrategy(styleSettingBlockStrategyName);

Long styleSettingPosition = blocksPositionCounter.getAndIncrement();
styleSettingBlockStrategy.updateBlocks(data, blockInfoArray, styleSettingPosition);
styleSettingBlockStrategy.updateStringBlocks(data, blockInfoArray, styleSettingPosition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.javajober.spaceWall.dto.request.DataStringSaveRequest;
import com.javajober.spaceWall.dto.request.DataStringUpdateRequest;
import com.javajober.spaceWall.filedto.DataSaveRequest;
import com.javajober.spaceWall.filedto.DataUpdateRequest;

import java.util.List;

Expand All @@ -21,7 +22,9 @@ public interface FixBlockStrategy {

CommonResponse createFixBlockDTO(final List<JsonNode> fixBlocks);

void updateBlocks(final DataStringUpdateRequest data, final ArrayNode blockInfoArray, final Long position);
void updateStringBlocks(final DataStringUpdateRequest data, final ArrayNode blockInfoArray, final Long position);

void updateBlocks(final DataUpdateRequest data, final ArrayNode blockInfoArray, final Long position);

String getStrategyName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface MoveBlockStrategy {

List<CommonResponse> createMoveBlockDTO(List<JsonNode> blocksWithSamePosition);

Set<Long> updateStringBlocks(final BlockSaveRequest<?> blocks, final ArrayNode blockInfoArray, final Long position);

Set<Long> updateBlocks(final BlockSaveRequest<?> blocks, final ArrayNode blockInfoArray, final Long position);

void deleteAllById(final Set<Long> blockIds);
Expand Down
Loading

0 comments on commit 7a97b31

Please sign in to comment.