From 7cfa292235df2fe33e67e74e5ae3539822338911 Mon Sep 17 00:00:00 2001 From: YeEun Sun Date: Fri, 27 Oct 2023 10:18:23 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B3=B5=EC=9C=A0=ED=8E=98=EC=9D=B4=EC=A7=80?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20API=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81=20(#215)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: SNSBlock 엔티티 변환하는 toEntity 메서드 추가 (#214) * feat: TemplateBlock 엔티티 변환하는 toEntity 메서드 추가 (#214) * refactor: TemplateBlock 'update' 메서드를 TemplateBlockUpdateRequest로 수정 (#214) - TemplateBlock 'update' 메서드가 TemplateBlockUpdateRequest를 사용하도록 수정함 * feat : MoveBlockStrategy 인터페이스에 updateBlocks 메서드 추가 (#214) * refactor: 전략 클래스에 updateBlocks 메서드 추가 (#214) - 엔티티를 업데이트하는 기능을 수행하는 메서드를 각 전략 클래스에 추가하였습니다. - 이 메서드는 BlockSaveRequest를 통해 업데이트할 데이터를 받아와서 해당 데이터를 처리하고, 업데이트된 엔티티의 ID를 반환합니다. - 업데이트할 엔티티가 존재하지 않을 경우에는 새로운 엔티티를 생성합니다. * refactor: SpaceWall 업데이트 메서드 리팩토링 (#214) - 각 블록의 유형에 따라 적절한 전략을 사용하여 블록을 업데이트합니다. - 업데이트된 블록의 정보를 JSON 배열에 추가하고, 엔터티를 업데이트한 후에 저장합니다. * refactor: 리팩토링으로 인해 불필요해진 메서드 및 import 코드 제거(#214) * refactor: 전략 클래스들의 updateBlocks 메서드 리팩토링 (#214) - 각 전략 클래스들의 updateBlocks 메서드에서 saveAll()을 사용하여 데이터베이스 트랜잭션이 한번에 처리되도록 코드를 수정하였습니다. * refactor: 블록 updateBlocks 메서드 로직을 LinkedHashSet 사용으로 수정 (#214) - 업데이트 된 블록들의 아이디가 중복되지 않고 순서 유지되야 하므로 LinkedSet으로 변경 * refactor: 'updateBlocks' 메서드에 블록 정보들을 blockInfoArray에 추가 (#214) * refactor: MoveBlockStrategy 인터페이스에 updateBlocks 메서드 매개변수 수정 (#214) * feat: MoveBlockStrategy 인터페이스에 deleteAllById 메서드 추가 (#214) * feat: 각 블록 repository에 deleteAllById 메서드 추가 (#214) * feat: 전략 클래스에 deleteAllById 메서드 추가 (#214) - 'deleteAllById' 메서드를 통해 입력된 블록들을 한꺼번에 삭제합니다. * fix : 'updateBlocks' 메서드에 블록 정보들을 담았던 blockInfoArray 로직 삭제 (#214) - 여러번 저장이 돼서 로직 제거 했습니다. * feat: 블록 타입에 따른 기존 블록 id 추출 기능 추가 (#214) * refactor : moveBlocks 업데이트 및 삭제 로직 리팩토링 (#214) BlockType 별로 기존 블록 id와 업데이트된 블록 id를 추적하기 위해 existingBlockIdsByType과 updatedBlockIdsByType 맵을 사용했습니다. * feat : FixBlockStrategy 인터페이스에 updateBlocks 메서드 추가 (#214) * refactor: BackgroundSetting 'update' 메서드를 BackgroundStringUpdateRequest 수정 (#214) - BackgroundSetting 'update' 메서드가 BackgroundStringUpdateRequest 사용하도록 수정함 * refactor: ThemeSetting 'update' 메서드를 ThemeSettingUpdateRequest로 수정 (#214) - ThemeSetting 'update' 메서드가 ThemeSettingUpdateRequest로 사용하도록 수정함 * refactor: StyleSetting 'update' 메서드 매개변수 수정 (#214) * refactor: WallInfoBlockStrategy 클래스의 'updateBlocks' 메서드 리팩토링 (#214) * refactor: StyleSettingStrategy 클래스의 'updateBlocks' 메서드 리팩토링 (#214) * refactor : fixBlocks 업데이트 로직 리팩토링 (#214) - wallInfoBlock 메서드 분리 - styleSetting 메서드 분리 * refactor : 불필요한 코드 및 import 제거 (#214) * refactor : 'update' 메서드 로직 분리 (#214) - 'processBlocks', 'processBlock', 'deleteRemainingBlocks' 메서드를 추가하여 블록 처리 부분을 분리 - (블록 처리, 블록 단위 처리, 남은 블록 삭제) * refactor : 매개변수에 final 추가 (#214) --- .../repository/FileBlockRepository.java | 2 + .../repository/FreeBlockRepository.java | 2 + .../repository/ListBlockRepository.java | 2 + .../dto/request/SNSBlockUpdateRequest.java | 10 + .../repository/SNSBlockRepository.java | 2 + .../domain/BackgroundSetting.java | 10 +- .../styleSetting/domain/StyleSetting.java | 8 +- .../themeSetting/domain/ThemeSetting.java | 6 +- .../templateBlock/domain/TemplateBlock.java | 10 +- .../request/TemplateBlockUpdateRequest.java | 11 + .../repository/TemplateBlockRepository.java | 2 + .../spaceWall/service/FileUploadService.java | 377 +----------------- .../spaceWall/service/SpaceWallService.java | 314 ++++----------- .../strategy/BlockJsonProcessor.java | 20 + .../spaceWall/strategy/FixBlockStrategy.java | 3 + .../spaceWall/strategy/MoveBlockStrategy.java | 6 +- .../strategy/impl/FileBlockStrategy.java | 39 +- .../strategy/impl/FreeBlockStrategy.java | 38 +- .../strategy/impl/ListBlockStrategy.java | 37 +- .../strategy/impl/SNSBlockStrategy.java | 39 +- .../strategy/impl/StyleSettingStrategy.java | 47 ++- .../strategy/impl/TemplateBlockStrategy.java | 40 +- .../strategy/impl/WallInfoBlockStrategy.java | 25 +- 23 files changed, 404 insertions(+), 646 deletions(-) diff --git a/src/main/java/com/javajober/blocks/fileBlock/repository/FileBlockRepository.java b/src/main/java/com/javajober/blocks/fileBlock/repository/FileBlockRepository.java index d8c084c..9833f27 100644 --- a/src/main/java/com/javajober/blocks/fileBlock/repository/FileBlockRepository.java +++ b/src/main/java/com/javajober/blocks/fileBlock/repository/FileBlockRepository.java @@ -14,6 +14,8 @@ public interface FileBlockRepository extends Repository { List saveAll(final Iterable fileBlocks); + void deleteAllById(final Iterable id); + Optional findByIdAndDeletedAtIsNull(final Long id); default FileBlock findFileBlock(final Long id) { diff --git a/src/main/java/com/javajober/blocks/freeBlock/repository/FreeBlockRepository.java b/src/main/java/com/javajober/blocks/freeBlock/repository/FreeBlockRepository.java index b8f9f60..9200226 100644 --- a/src/main/java/com/javajober/blocks/freeBlock/repository/FreeBlockRepository.java +++ b/src/main/java/com/javajober/blocks/freeBlock/repository/FreeBlockRepository.java @@ -14,6 +14,8 @@ public interface FreeBlockRepository extends Repository { List saveAll(final Iterable freeBlocks); + void deleteAllById(final Iterable id); + Optional findByIdAndDeletedAtIsNull(final Long freeId); default FreeBlock findFreeBlock(final Long freeId) { diff --git a/src/main/java/com/javajober/blocks/listBlock/repository/ListBlockRepository.java b/src/main/java/com/javajober/blocks/listBlock/repository/ListBlockRepository.java index 1e7354b..0df2b11 100644 --- a/src/main/java/com/javajober/blocks/listBlock/repository/ListBlockRepository.java +++ b/src/main/java/com/javajober/blocks/listBlock/repository/ListBlockRepository.java @@ -15,6 +15,8 @@ public interface ListBlockRepository extends Repository { List saveAll(final Iterable listBlocks); + void deleteAllById(final Iterable id); + Optional findByIdAndDeletedAtIsNull(final Long id); default ListBlock findListBlock(final Long id) { diff --git a/src/main/java/com/javajober/blocks/snsBlock/dto/request/SNSBlockUpdateRequest.java b/src/main/java/com/javajober/blocks/snsBlock/dto/request/SNSBlockUpdateRequest.java index 2b74f41..cd735a8 100644 --- a/src/main/java/com/javajober/blocks/snsBlock/dto/request/SNSBlockUpdateRequest.java +++ b/src/main/java/com/javajober/blocks/snsBlock/dto/request/SNSBlockUpdateRequest.java @@ -1,5 +1,8 @@ package com.javajober.blocks.snsBlock.dto.request; +import com.javajober.blocks.snsBlock.domain.SNSBlock; +import com.javajober.blocks.snsBlock.domain.SNSType; + import lombok.Getter; @Getter @@ -13,4 +16,11 @@ public class SNSBlockUpdateRequest { private SNSBlockUpdateRequest() { } + public static SNSBlock toEntity(SNSBlockUpdateRequest request) { + return SNSBlock.builder() + .snsUUID(request.getSnsUUID()) + .snsType(SNSType.findSNSTypeByString(request.getSnsType())) + .snsURL(request.getSnsURL()) + .build(); + } } \ No newline at end of file diff --git a/src/main/java/com/javajober/blocks/snsBlock/repository/SNSBlockRepository.java b/src/main/java/com/javajober/blocks/snsBlock/repository/SNSBlockRepository.java index d41da8d..a2d6fa6 100644 --- a/src/main/java/com/javajober/blocks/snsBlock/repository/SNSBlockRepository.java +++ b/src/main/java/com/javajober/blocks/snsBlock/repository/SNSBlockRepository.java @@ -16,6 +16,8 @@ public interface SNSBlockRepository extends Repository { List findAllById(final Iterable id); + void deleteAllById(final Iterable id); + Optional findById(final Long id); default SNSBlock findSNSBlock(final Long id) { diff --git a/src/main/java/com/javajober/blocks/styleSetting/backgroundSetting/domain/BackgroundSetting.java b/src/main/java/com/javajober/blocks/styleSetting/backgroundSetting/domain/BackgroundSetting.java index 4628256..9864293 100644 --- a/src/main/java/com/javajober/blocks/styleSetting/backgroundSetting/domain/BackgroundSetting.java +++ b/src/main/java/com/javajober/blocks/styleSetting/backgroundSetting/domain/BackgroundSetting.java @@ -9,6 +9,8 @@ import javax.persistence.*; import java.time.LocalDateTime; +import com.javajober.blocks.styleSetting.backgroundSetting.dto.request.BackgroundStringUpdateRequest; + @Getter @Table(name = "background_setting") @EntityListeners(AuditingEntityListener.class) @@ -50,9 +52,9 @@ public BackgroundSetting(final String solidColor, final Boolean gradation, final this.styleImageURL = styleImageURL; } - public void update(final String solidColor, final Boolean gradation, final String styleImageURL) { - this.solidColor = solidColor; - this.gradation = gradation; - this.styleImageURL = styleImageURL; + public void update(final BackgroundStringUpdateRequest request) { + this.solidColor = request.getSolidColor(); + this.gradation = request.getGradation(); + this.styleImageURL = request.getStyleImgURL(); } } \ No newline at end of file diff --git a/src/main/java/com/javajober/blocks/styleSetting/domain/StyleSetting.java b/src/main/java/com/javajober/blocks/styleSetting/domain/StyleSetting.java index 77ea0cc..65fa76d 100644 --- a/src/main/java/com/javajober/blocks/styleSetting/domain/StyleSetting.java +++ b/src/main/java/com/javajober/blocks/styleSetting/domain/StyleSetting.java @@ -56,9 +56,9 @@ public StyleSetting(final BackgroundSetting backgroundSetting, final BlockSettin this.blockSetting = blockSetting; this.themeSetting = themeSetting; } - public void update(final StyleSetting styleSetting){ - this.backgroundSetting = styleSetting.backgroundSetting; - this.blockSetting = styleSetting.blockSetting; - this.themeSetting = styleSetting.themeSetting; + public void update(final BackgroundSetting backgroundSetting, final BlockSetting blockSetting, final ThemeSetting themeSetting){ + this.backgroundSetting = backgroundSetting; + this.blockSetting = blockSetting; + this.themeSetting = themeSetting; } } \ No newline at end of file diff --git a/src/main/java/com/javajober/blocks/styleSetting/themeSetting/domain/ThemeSetting.java b/src/main/java/com/javajober/blocks/styleSetting/themeSetting/domain/ThemeSetting.java index d1fdca0..909da72 100644 --- a/src/main/java/com/javajober/blocks/styleSetting/themeSetting/domain/ThemeSetting.java +++ b/src/main/java/com/javajober/blocks/styleSetting/themeSetting/domain/ThemeSetting.java @@ -9,6 +9,8 @@ import javax.persistence.*; import java.time.LocalDateTime; +import com.javajober.blocks.styleSetting.themeSetting.dto.request.ThemeSettingUpdateRequest; + @Getter @Table (name = "theme_setting") @EntityListeners(AuditingEntityListener.class) @@ -42,7 +44,7 @@ public ThemeSetting(final String theme) { this.theme = theme; } - public void update(final String theme) { - this.theme = theme; + public void update(final ThemeSettingUpdateRequest request) { + this.theme = request.getTheme(); } } \ No newline at end of file diff --git a/src/main/java/com/javajober/blocks/templateBlock/domain/TemplateBlock.java b/src/main/java/com/javajober/blocks/templateBlock/domain/TemplateBlock.java index b94a979..a53599f 100644 --- a/src/main/java/com/javajober/blocks/templateBlock/domain/TemplateBlock.java +++ b/src/main/java/com/javajober/blocks/templateBlock/domain/TemplateBlock.java @@ -9,6 +9,8 @@ import javax.persistence.*; import java.time.LocalDateTime; +import com.javajober.blocks.templateBlock.dto.request.TemplateBlockUpdateRequest; + @Getter @Table(name = "template_block") @EntityListeners(AuditingEntityListener.class) @@ -49,10 +51,10 @@ public TemplateBlock(final String templateUUID, final String templateTitle, fina this.templateDescription = templateDescription; } - public void update(final String templateUUID, final String templateTitle, final String templateDescription) { - this.templateUUID = templateUUID; - this.templateTitle = templateTitle; - this.templateDescription = templateDescription; + public void update(final TemplateBlockUpdateRequest request) { + this.templateUUID = request.getTemplateUUID(); + this.templateTitle = request.getTemplateTitle(); + this.templateDescription = request.getTemplateDescription(); } public void setDeletedAt(){ diff --git a/src/main/java/com/javajober/blocks/templateBlock/dto/request/TemplateBlockUpdateRequest.java b/src/main/java/com/javajober/blocks/templateBlock/dto/request/TemplateBlockUpdateRequest.java index cee26df..4056b66 100644 --- a/src/main/java/com/javajober/blocks/templateBlock/dto/request/TemplateBlockUpdateRequest.java +++ b/src/main/java/com/javajober/blocks/templateBlock/dto/request/TemplateBlockUpdateRequest.java @@ -2,6 +2,8 @@ import java.util.List; +import com.javajober.blocks.templateBlock.domain.TemplateBlock; + import lombok.Getter; @Getter @@ -17,4 +19,13 @@ public class TemplateBlockUpdateRequest { private TemplateBlockUpdateRequest() { } + + public static TemplateBlock toEntity(final TemplateBlockUpdateRequest templateBlockUpdateRequest) { + return TemplateBlock.builder() + .templateUUID(templateBlockUpdateRequest.getTemplateUUID()) + .templateTitle(templateBlockUpdateRequest.getTemplateTitle()) + .templateDescription(templateBlockUpdateRequest.getTemplateDescription()) + .build(); + + } } \ No newline at end of file diff --git a/src/main/java/com/javajober/blocks/templateBlock/repository/TemplateBlockRepository.java b/src/main/java/com/javajober/blocks/templateBlock/repository/TemplateBlockRepository.java index a19002e..202f368 100644 --- a/src/main/java/com/javajober/blocks/templateBlock/repository/TemplateBlockRepository.java +++ b/src/main/java/com/javajober/blocks/templateBlock/repository/TemplateBlockRepository.java @@ -17,6 +17,8 @@ public interface TemplateBlockRepository extends Repository List findAllById(final Iterable id); + void deleteAllById(final Iterable id); + Optional findById(final Long id); default TemplateBlock findTemplateBlock(final Long id){ diff --git a/src/main/java/com/javajober/spaceWall/service/FileUploadService.java b/src/main/java/com/javajober/spaceWall/service/FileUploadService.java index 98cf19b..e69d224 100644 --- a/src/main/java/com/javajober/spaceWall/service/FileUploadService.java +++ b/src/main/java/com/javajober/spaceWall/service/FileUploadService.java @@ -1,38 +1,10 @@ package com.javajober.spaceWall.service; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.javajober.blocks.styleSetting.backgroundSetting.domain.BackgroundSetting; -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.themeSetting.dto.request.ThemeSettingSaveRequest; import com.javajober.core.exception.ApiStatus; import com.javajober.core.exception.ApplicationException; -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.dto.request.FreeBlockUpdateRequest; -import com.javajober.blocks.freeBlock.repository.FreeBlockRepository; -import com.javajober.blocks.listBlock.domain.ListBlock; -import com.javajober.blocks.listBlock.dto.request.ListBlockSaveRequest; -import com.javajober.blocks.listBlock.dto.request.ListBlockUpdateRequest; -import com.javajober.blocks.listBlock.repository.ListBlockRepository; import com.javajober.member.domain.Member; import com.javajober.member.repository.MemberRepository; -import com.javajober.blocks.snsBlock.domain.SNSBlock; -import com.javajober.blocks.snsBlock.domain.SNSType; -import com.javajober.blocks.snsBlock.dto.request.SNSBlockSaveRequest; -import com.javajober.blocks.snsBlock.dto.request.SNSBlockUpdateRequest; -import com.javajober.blocks.snsBlock.repository.SNSBlockRepository; import com.javajober.space.domain.AddSpace; import com.javajober.space.repository.AddSpaceRepository; import com.javajober.spaceWall.domain.BlockType; @@ -40,27 +12,12 @@ 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; import com.javajober.spaceWall.repository.SpaceWallRepository; import com.javajober.spaceWall.spaceWallCategory.domain.SpaceWallCategoryType; -import com.javajober.blocks.styleSetting.domain.StyleSetting; -import com.javajober.blocks.styleSetting.filedto.StyleSettingSaveRequest; -import com.javajober.blocks.styleSetting.filedto.StyleSettingUpdateRequest; -import com.javajober.blocks.styleSetting.repository.StyleSettingRepository; -import com.javajober.blocks.templateBlock.domain.TemplateBlock; -import com.javajober.blocks.templateBlock.dto.request.TemplateBlockSaveRequest; -import com.javajober.blocks.templateBlock.dto.request.TemplateBlockUpdateRequest; -import com.javajober.blocks.templateBlock.repository.TemplateBlockRepository; -import com.javajober.blocks.styleSetting.themeSetting.domain.ThemeSetting; -import com.javajober.blocks.styleSetting.themeSetting.dto.request.ThemeSettingUpdateRequest; -import com.javajober.blocks.styleSetting.themeSetting.repository.ThemeSettingRepository; -import com.javajober.blocks.wallInfoBlock.domain.WallInfoBlock; -import com.javajober.blocks.wallInfoBlock.filedto.WallInfoBlockSaveRequest; -import com.javajober.blocks.wallInfoBlock.filedto.WallInfoBlockUpdateRequest; -import com.javajober.blocks.wallInfoBlock.repository.WallInfoBlockRepository; + import com.javajober.spaceWall.strategy.BlockJsonProcessor; import com.javajober.spaceWall.strategy.BlockStrategyFactory; import com.javajober.spaceWall.strategy.BlockStrategyName; @@ -71,7 +28,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; @@ -80,45 +36,20 @@ public class FileUploadService { private static final Long INITIAL_POSITION = 1L; private final SpaceWallRepository spaceWallRepository; - private final SNSBlockRepository snsBlockRepository; - private final FreeBlockRepository freeBlockRepository; - private final TemplateBlockRepository templateBlockRepository; - private final WallInfoBlockRepository wallInfoBlockRepository; - private final FileBlockRepository fileBlockRepository; - private final ListBlockRepository listBlockRepository; - private final StyleSettingRepository styleSettingRepository; - private final BackgroundSettingRepository backgroundSettingRepository; - private final BlockSettingRepository blockSettingRepository; - private final ThemeSettingRepository themeSettingRepository; private final MemberRepository memberRepository; private final AddSpaceRepository addSpaceRepository; - private final FileImageService fileImageService; private final BlockStrategyFactory blockStrategyFactory; private final BlockJsonProcessor blockJsonProcessor; - public FileUploadService(final SpaceWallRepository spaceWallRepository, final SNSBlockRepository snsBlockRepository, - final FreeBlockRepository freeBlockRepository, final TemplateBlockRepository templateBlockRepository, - final WallInfoBlockRepository wallInfoBlockRepository, final FileBlockRepository fileBlockRepository, - final ListBlockRepository listBlockRepository, final FileImageService fileImageService, - final StyleSettingRepository styleSettingRepository, final BackgroundSettingRepository backgroundSettingRepository, - final BlockSettingRepository blockSettingRepository, final ThemeSettingRepository themeSettingRepository, - final MemberRepository memberRepository, final AddSpaceRepository addSpaceRepository, - BlockStrategyFactory blockStrategyFactory, BlockJsonProcessor blockJsonProcessor) { + public FileUploadService(final SpaceWallRepository spaceWallRepository, + final MemberRepository memberRepository, + final AddSpaceRepository addSpaceRepository, + final BlockStrategyFactory blockStrategyFactory, + final BlockJsonProcessor blockJsonProcessor) { this.spaceWallRepository = spaceWallRepository; - this.snsBlockRepository = snsBlockRepository; - this.freeBlockRepository = freeBlockRepository; - this.templateBlockRepository = templateBlockRepository; - this.wallInfoBlockRepository = wallInfoBlockRepository; - this.fileBlockRepository = fileBlockRepository; - this.listBlockRepository = listBlockRepository; - this.styleSettingRepository = styleSettingRepository; - this.backgroundSettingRepository = backgroundSettingRepository; - this.blockSettingRepository = blockSettingRepository; - this.themeSettingRepository = themeSettingRepository; this.memberRepository = memberRepository; this.addSpaceRepository = addSpaceRepository; - this.fileImageService = fileImageService; this.blockStrategyFactory = blockStrategyFactory; this.blockJsonProcessor = blockJsonProcessor; } @@ -246,301 +177,7 @@ public SpaceWallSaveResponse update(final Long memberId, final SpaceWallUpdateRe final List files, final MultipartFile backgroundImgURL, final MultipartFile wallInfoImgURL, final MultipartFile styleImgURL){ - DataUpdateRequest dataUpdateRequest = spaceWallRequest.getData(); - - Long spaceWallId = dataUpdateRequest.getSpaceWallId(); - Long addSpaceId = dataUpdateRequest.getSpaceId(); - - memberRepository.findMember(memberId); - addSpaceRepository.findAddSpace(addSpaceId); - - SpaceWall spaceWall = spaceWallRepository.findSpaceWall(spaceWallId, addSpaceId, memberId, flagType); - - Long blocksPosition = 2L; - AtomicLong blocksPositionCounter = new AtomicLong(blocksPosition); - ObjectMapper jsonMapper = new ObjectMapper(); - ArrayNode blockInfoArray = jsonMapper.createArrayNode(); - AtomicInteger i = new AtomicInteger(); - - WallInfoBlockUpdateRequest wallInfoBlockRequest = spaceWallRequest.getData().getWallInfoBlock(); - Long wallInfoBlock = updateWallInfoBlock(wallInfoBlockRequest, backgroundImgURL, wallInfoImgURL); - String wallInfoBlockType = BlockType.WALL_INFO_BLOCK.getEngTitle(); - Long blockStartPosition = 1L; - addBlockToJsonArray(blockInfoArray, jsonMapper, blockStartPosition, wallInfoBlockType, wallInfoBlock); - - spaceWallRequest.getData().getBlocks().forEach(block -> { - BlockType blockType = BlockType.findBlockTypeByString(block.getBlockType()); - Long position = blocksPositionCounter.getAndIncrement(); - switch (blockType) { - case FREE_BLOCK: - List freeBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateFreeBlockIds = updateFreeBlocks(freeBlockRequests); - updateFreeBlockIds.forEach(freeBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, freeBlockId, block)); - break; - case SNS_BLOCK: - List snsBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateSnsBlockIds = updateSnsBlocks(snsBlockRequests); - updateSnsBlockIds.forEach(snsBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, snsBlockId, block)); - break; - case TEMPLATE_BLOCK: - List templateBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateTemplateBlockIds = updateTemplateBlocks(templateBlockRequests); - updateTemplateBlockIds.forEach(templateBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, templateBlockId, block)); - break; - case FILE_BLOCK: - List fileBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateFileBlockIds = updateFileBlocks(fileBlockRequests, files.get(i.getAndIncrement())); - updateFileBlockIds.forEach(fileBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, fileBlockId, block)); - break; - case LIST_BLOCK: - List listBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateListBlockIds = updateListBlock(listBlockRequests); - updateListBlockIds.forEach(listBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, listBlockId, block)); - } - }); - - StyleSettingUpdateRequest styleSettingUpdateRequest = dataUpdateRequest.getStyleSetting(); - Long styleSetting = updateStyleSetting(styleSettingUpdateRequest, styleImgURL); - String styleSettingString = "styleSetting"; - Long stylePosition = blocksPositionCounter.getAndIncrement(); - - addBlockToJsonArray(blockInfoArray, jsonMapper, stylePosition, styleSettingString, styleSetting); - String blocks = blockInfoArray.toString(); - - spaceWall.fileUpdate(dataUpdateRequest, flagType, blocks); - spaceWallId = spaceWallRepository.save(spaceWall).getId(); - - return new SpaceWallSaveResponse(spaceWallId); - } - - private Long saveWallInfoBlock(final WallInfoBlockSaveRequest wallInfoBlockSaveRequest, MultipartFile backgroundImgURL, MultipartFile wallInfoImgURL) { - - String backgroundImgName = fileImageService.uploadFile(backgroundImgURL); - String wallInfoImgName = fileImageService.uploadFile(wallInfoImgURL); - WallInfoBlock wallInfoBlock = WallInfoBlockSaveRequest.toEntity(wallInfoBlockSaveRequest, backgroundImgName, wallInfoImgName); - - return wallInfoBlockRepository.save(wallInfoBlock).getId(); - } - - private List saveFreeBlocks(final List subData) { - - List freeBlockIds = new ArrayList<>(); - subData.forEach(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; - } - - private List saveSnsBlocks(final List subData) { - - List snsBlockIds = new ArrayList<>(); - - subData.forEach(block -> { - SNSBlock snsBlock = SNSBlockSaveRequest.toEntity(block); - snsBlockIds.add(snsBlockRepository.save(snsBlock).getId()); - }); - return snsBlockIds; - } - - private List saveTemplateBlocks(final List subData) { - - List templateBlockIds = new ArrayList<>(); - - subData.forEach(block -> { - TemplateBlock templateBlock = TemplateBlockSaveRequest.toEntity(block); - templateBlockIds.add(templateBlockRepository.save(templateBlock).getId()); - }); - return templateBlockIds; - } - - private List saveFileBlocks(final List subData, MultipartFile file) { - - String fileName = fileImageService.uploadFile(file); - List fileBlockIds = new ArrayList<>(); - subData.forEach(block -> { - FileBlock fileBlock = FileBlockSaveRequest.toEntity(block, fileName); - fileBlockIds.add(fileBlockRepository.save(fileBlock).getId()); - }); - return fileBlockIds; - } - - private List saveListBlocks(final List subData) { - - List listBlockIds = new ArrayList<>(); - subData.forEach(block -> { - ListBlock listBlock = ListBlockSaveRequest.toEntity(block); - listBlockIds.add(listBlockRepository.save(listBlock).getId()); - }); - return listBlockIds; - } - - private Long saveStyleSetting(final StyleSettingSaveRequest request, MultipartFile styleImgURL){ - - BackgroundSettingSaveRequest backgroundRequest = request.getBackgroundSetting(); - String styleImgName = fileImageService.uploadFile(styleImgURL); - BackgroundSetting backgroundSetting = backgroundSettingRepository.save(BackgroundSettingSaveRequest.toEntity(backgroundRequest, styleImgName)); - - BlockSettingSaveRequest blockSettingRequest = request.getBlockSetting(); - BlockSetting blockSetting = blockSettingRepository.save(BlockSettingSaveRequest.toEntity(blockSettingRequest)); - - ThemeSettingSaveRequest themeSettingRequest = request.getThemeSetting(); - ThemeSetting themeSetting = themeSettingRepository.save(ThemeSettingSaveRequest.toEntity(themeSettingRequest)); - - StyleSetting styleSetting = request.toEntity(backgroundSetting, blockSetting, themeSetting); - - return styleSettingRepository.save(styleSetting).getId(); - } - - private Long updateWallInfoBlock(final WallInfoBlockUpdateRequest wallInfoBlockRequest, MultipartFile backgroundImgURL, MultipartFile wallInfoImgURL) { - - WallInfoBlock wallInfoBlock = wallInfoBlockRepository.findWallInfoBlock(wallInfoBlockRequest.getWallInfoBlockId()); - String backgroundImgName = fileImageService.uploadFile(backgroundImgURL); - String wallInfoImgName = fileImageService.uploadFile(wallInfoImgURL); - wallInfoBlock.update(wallInfoBlockRequest,backgroundImgName, wallInfoImgName); - - return wallInfoBlockRepository.save(wallInfoBlock).getId(); - } - - private List updateFreeBlocks(final List subData) { - - List updatedFreeBlockIds = new ArrayList<>(); - for (FreeBlockUpdateRequest updateRequest : subData) { - if(updateRequest.getFreeBlockId() == null ){ - 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 { - com.javajober.blocks.freeBlock.domain.FreeBlock freeBlock = freeBlockRepository.findFreeBlock(updateRequest.getFreeBlockId()); - freeBlock.update(updateRequest); - updatedFreeBlockIds.add(freeBlockRepository.save(freeBlock).getId()); - } - } - return updatedFreeBlockIds; - } - - private List updateSnsBlocks(final List subData){ - - List updateSnsBlockIds = new ArrayList<>(); - subData.forEach(snsBlockRequest -> { - if(snsBlockRequest.getSnsBlockId() ==null){ - SNSType snsType = SNSType.findSNSTypeByString(snsBlockRequest.getSnsType()); - SNSBlock snsBlock = new SNSBlock(snsBlockRequest.getSnsUUID(),snsType,snsBlockRequest.getSnsURL()); - updateSnsBlockIds.add(snsBlockRepository.save(snsBlock).getId()); - }else { - SNSBlock snsBlock = snsBlockRepository.findSNSBlock(snsBlockRequest.getSnsBlockId()); - snsBlock.update(snsBlockRequest); - updateSnsBlockIds.add(snsBlockRepository.save(snsBlock).getId()); - } - }); - - return updateSnsBlockIds; - } - - private List updateTemplateBlocks(final List subData) { - - List updateTemplateBlockIds = new ArrayList<>(); - for(TemplateBlockUpdateRequest updateRequest : subData) { - if (updateRequest.getTemplateBlockId() == null) { - TemplateBlock templateBlock = new TemplateBlock(updateRequest.getTemplateUUID(),updateRequest.getTemplateTitle(),updateRequest.getTemplateDescription()); - updateTemplateBlockIds.add(templateBlockRepository.save(templateBlock).getId()); - } else { - TemplateBlock templateBlock = templateBlockRepository.findTemplateBlock(updateRequest.getTemplateBlockId()); - templateBlock.update(updateRequest.getTemplateUUID(), updateRequest.getTemplateTitle(), updateRequest.getTemplateDescription()); - updateTemplateBlockIds.add(templateBlockRepository.save(templateBlock).getId()); - } - } - return updateTemplateBlockIds; - } - - private List updateFileBlocks(final List subData, MultipartFile file) { - - String fileName = fileImageService.uploadFile(file); - List updateFileBlockIds = new ArrayList<>(); - for (FileBlockUpdateRequest updateRequest : subData) { - if(updateRequest.getFileBlockId() == null){ - FileBlock fileBlock = new FileBlock(updateRequest.getFileTitle(), updateRequest.getFileDescription(),fileName); - updateFileBlockIds.add(fileBlockRepository.save(fileBlock).getId()); - }else{ - FileBlock fileBlock = fileBlockRepository.findFileBlock(updateRequest.getFileBlockId()); - fileBlock.fileUpdate(updateRequest, fileName); - updateFileBlockIds.add(fileBlockRepository.save(fileBlock).getId()); - } - } - return updateFileBlockIds; - } - - private List updateListBlock(final List subData){ - - List updateListBlockIds = new ArrayList<>(); - for(ListBlockUpdateRequest updateRequest : subData){ - if(updateRequest.getListBlockId() == null){ - ListBlock listBlock = new ListBlock(updateRequest.getListUUID(),updateRequest.getListLabel(),updateRequest.getListTitle(),updateRequest.getListDescription(),updateRequest.getIsLink()); - updateListBlockIds.add(listBlockRepository.save(listBlock).getId()); - }else{ - ListBlock listBlock = listBlockRepository.findListBlock(updateRequest.getListBlockId()); - listBlock.update(updateRequest); - updateListBlockIds.add(listBlockRepository.save(listBlock).getId()); - } - } - return updateListBlockIds; - } - - private Long updateStyleSetting(final StyleSettingUpdateRequest updateRequest, MultipartFile styleImgURL){ - - StyleSetting styleSetting = styleSettingRepository.findStyleBlock(updateRequest.getStyleSettingBlockId()); - updateBackgroundSetting(updateRequest.getBackgroundSetting(), styleImgURL); - updateBlockSetting(updateRequest.getBlockSetting()); - updateThemeSetting(updateRequest.getThemeSetting()); - styleSetting.update(styleSetting); - return styleSettingRepository.save(styleSetting).getId(); - } - - private Long updateBackgroundSetting(final BackgroundSettingUpdateRequest updateRequest, MultipartFile styleImgURL){ - - String styleImgName = fileImageService.uploadFile(styleImgURL); - BackgroundSetting backgroundSetting = backgroundSettingRepository.getById(updateRequest.getBackgroundSettingBlockId()); - backgroundSetting.update(updateRequest.getSolidColor(), updateRequest.getGradation(), styleImgName); - return backgroundSettingRepository.save(backgroundSetting).getId(); - } - - private Long updateBlockSetting(final BlockSettingUpdateRequest updateRequest){ - - BlockSetting blockSetting = blockSettingRepository.getById(updateRequest.getBlockSettingBlockId()); - blockSetting.update(updateRequest); - return blockSettingRepository.save(blockSetting).getId(); - } - - private Long updateThemeSetting(final ThemeSettingUpdateRequest updateRequest){ - - ThemeSetting themeSetting = themeSettingRepository.getById(updateRequest.getThemeSettingBlockId()); - themeSetting.update(updateRequest.getTheme()); - return themeSettingRepository.save(themeSetting).getId(); - } - - private void addBlockInfoToArray(final ArrayNode blockInfoArray, final ObjectMapper jsonMapper, final BlockType blockType, final Long position, final Long blockId, final BlockSaveRequest block) { - - String currentBlockTypeTitle = blockType.getEngTitle(); - String blockUUID = block.getBlockUUID(); - - ObjectNode blockInfoObject = jsonMapper.createObjectNode(); - - blockInfoObject.put("position", position); - blockInfoObject.put("blockType", currentBlockTypeTitle); - blockInfoObject.put("blockId", blockId); - blockInfoObject.put("blockUUID", blockUUID); - - blockInfoArray.add(blockInfoObject); - } - - private void addBlockToJsonArray(final ArrayNode jsonArray, final ObjectMapper mapper, final Long position, final String blockType, final Long blockId) { - - ObjectNode blockInfoObject = mapper.createObjectNode(); - - blockInfoObject.put("position", position); - blockInfoObject.put("blockType", blockType); - blockInfoObject.put("blockId", blockId); - blockInfoObject.put("blockUUID", (String) null); - jsonArray.add(blockInfoObject); + return new SpaceWallSaveResponse(1L); } } \ No newline at end of file diff --git a/src/main/java/com/javajober/spaceWall/service/SpaceWallService.java b/src/main/java/com/javajober/spaceWall/service/SpaceWallService.java index e711344..7725bfa 100644 --- a/src/main/java/com/javajober/spaceWall/service/SpaceWallService.java +++ b/src/main/java/com/javajober/spaceWall/service/SpaceWallService.java @@ -1,55 +1,21 @@ package com.javajober.spaceWall.service; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.javajober.blocks.styleSetting.backgroundSetting.dto.request.BackgroundStringUpdateRequest; -import com.javajober.blocks.styleSetting.blockSetting.dto.request.BlockSettingUpdateRequest; -import com.javajober.blocks.fileBlock.dto.request.FileBlockStringUpdateRequest; -import com.javajober.blocks.freeBlock.dto.request.FreeBlockUpdateRequest; -import com.javajober.blocks.listBlock.dto.request.ListBlockUpdateRequest; -import com.javajober.blocks.snsBlock.domain.SNSType; -import com.javajober.blocks.snsBlock.dto.request.SNSBlockUpdateRequest; import com.javajober.core.exception.ApiStatus; import com.javajober.core.exception.ApplicationException; import com.javajober.space.repository.AddSpaceRepository; 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.repository.FreeBlockRepository; -import com.javajober.blocks.listBlock.domain.ListBlock; -import com.javajober.blocks.listBlock.repository.ListBlockRepository; import com.javajober.member.domain.Member; import com.javajober.member.repository.MemberRepository; -import com.javajober.blocks.styleSetting.backgroundSetting.domain.BackgroundSetting; -import com.javajober.blocks.styleSetting.blockSetting.domain.BlockSetting; import com.javajober.spaceWall.dto.request.*; import com.javajober.spaceWall.dto.response.SpaceWallSaveResponse; import com.javajober.spaceWall.filedto.SpaceWallSaveRequest; -import com.javajober.blocks.styleSetting.domain.StyleSetting; -import com.javajober.blocks.styleSetting.dto.request.StyleSettingStringUpdateRequest; -import com.javajober.blocks.templateBlock.dto.request.TemplateBlockUpdateRequest; -import com.javajober.blocks.styleSetting.themeSetting.domain.ThemeSetting; -import com.javajober.blocks.styleSetting.backgroundSetting.repository.BackgroundSettingRepository; -import com.javajober.blocks.styleSetting.blockSetting.repository.BlockSettingRepository; -import com.javajober.blocks.styleSetting.repository.StyleSettingRepository; -import com.javajober.blocks.styleSetting.themeSetting.dto.request.ThemeSettingUpdateRequest; -import com.javajober.blocks.styleSetting.themeSetting.repository.ThemeSettingRepository; -import com.javajober.blocks.snsBlock.domain.SNSBlock; -import com.javajober.blocks.snsBlock.repository.SNSBlockRepository; import com.javajober.spaceWall.domain.BlockType; import com.javajober.spaceWall.domain.FlagType; import com.javajober.spaceWall.domain.SpaceWall; import com.javajober.spaceWall.spaceWallCategory.domain.SpaceWallCategoryType; import com.javajober.spaceWall.repository.SpaceWallRepository; -import com.javajober.blocks.templateBlock.domain.TemplateBlock; -import com.javajober.blocks.templateBlock.repository.TemplateBlockRepository; -import com.javajober.blocks.wallInfoBlock.domain.WallInfoBlock; -import com.javajober.blocks.wallInfoBlock.dto.request.WallInfoBlockStringUpdateRequest; -import com.javajober.blocks.wallInfoBlock.repository.WallInfoBlockRepository; import com.javajober.spaceWall.strategy.BlockJsonProcessor; import com.javajober.spaceWall.strategy.BlockStrategyFactory; import com.javajober.spaceWall.strategy.FixBlockStrategy; @@ -58,8 +24,12 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; +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.AtomicLong; @Service @@ -67,41 +37,18 @@ public class SpaceWallService { private static final Long INITIAL_POSITION = 1L; private final SpaceWallRepository spaceWallRepository; - private final SNSBlockRepository snsBlockRepository; - private final FreeBlockRepository freeBlockRepository; - private final TemplateBlockRepository templateBlockRepository; - private final WallInfoBlockRepository wallInfoBlockRepository; - private final FileBlockRepository fileBlockRepository; - private final ListBlockRepository listBlockRepository; - private final StyleSettingRepository styleSettingRepository; - private final BackgroundSettingRepository backgroundSettingRepository; - private final BlockSettingRepository blockSettingRepository; - private final ThemeSettingRepository themeSettingRepository; private final MemberRepository memberRepository; private final AddSpaceRepository addSpaceRepository; private final BlockStrategyFactory blockStrategyFactory; private final BlockJsonProcessor blockJsonProcessor; - public SpaceWallService(final SpaceWallRepository spaceWallRepository, final SNSBlockRepository snsBlockRepository, - final FreeBlockRepository freeBlockRepository, final TemplateBlockRepository templateBlockRepository, - final WallInfoBlockRepository wallInfoBlockRepository, final FileBlockRepository fileBlockRepository, - final ListBlockRepository listBlockRepository, final StyleSettingRepository styleSettingRepository, - final BackgroundSettingRepository backgroundSettingRepository, final BlockSettingRepository blockSettingRepository, - final ThemeSettingRepository themeSettingRepository, final MemberRepository memberRepository, - final AddSpaceRepository addSpaceRepository, final BlockStrategyFactory blockStrategyFactory, + public SpaceWallService(final SpaceWallRepository spaceWallRepository, + final MemberRepository memberRepository, + final AddSpaceRepository addSpaceRepository, + final BlockStrategyFactory blockStrategyFactory, final BlockJsonProcessor blockJsonProcessor) { this.spaceWallRepository = spaceWallRepository; - this.snsBlockRepository = snsBlockRepository; - this.freeBlockRepository = freeBlockRepository; - this.templateBlockRepository = templateBlockRepository; - this.wallInfoBlockRepository = wallInfoBlockRepository; - this.fileBlockRepository = fileBlockRepository; - this.listBlockRepository = listBlockRepository; - this.styleSettingRepository = styleSettingRepository; - this.backgroundSettingRepository = backgroundSettingRepository; - this.blockSettingRepository = blockSettingRepository; - this.themeSettingRepository = themeSettingRepository; this.memberRepository = memberRepository; this.addSpaceRepository = addSpaceRepository; this.blockStrategyFactory = blockStrategyFactory; @@ -219,222 +166,103 @@ public void updateIsPublic(IsPublicUpdateRequest publicUpdateRequest, Long membe @Transactional public SpaceWallSaveResponse update(final Long memberId, final SpaceWallStringUpdateRequest spaceWallUpdateRequest, final FlagType flagType) { - DataStringUpdateRequest dataUpdateRequest = spaceWallUpdateRequest.getData(); + DataStringUpdateRequest data = spaceWallUpdateRequest.getData(); - Long spaceWallId = dataUpdateRequest.getSpaceWallId(); - Long addSpaceId = dataUpdateRequest.getSpaceId(); + Long spaceWallId = data.getSpaceWallId(); - memberRepository.findMember(memberId); - addSpaceRepository.findAddSpace(addSpaceId); + Member member = memberRepository.findMember(memberId); - SpaceWall spaceWall = spaceWallRepository.findSpaceWall(spaceWallId, addSpaceId, memberId, flagType); + AddSpace addSpace = addSpaceRepository.findAddSpace(data.getSpaceId()); - Long blocksPosition = 2L; - AtomicLong blocksPositionCounter = new AtomicLong(blocksPosition); - ObjectMapper jsonMapper = new ObjectMapper(); - ArrayNode blockInfoArray = jsonMapper.createArrayNode(); + validateSpaceOwnership(member, addSpace); - WallInfoBlockStringUpdateRequest wallInfoBlockRequest = spaceWallUpdateRequest.getData().getWallInfoBlock(); - Long wallInfoBlock = updateWallInfoBlock(wallInfoBlockRequest); - String wallInfoBlockType = BlockType.WALL_INFO_BLOCK.getEngTitle(); - Long blockStartPosition = 1L; - addBlockToJsonArray(blockInfoArray, jsonMapper, blockStartPosition, wallInfoBlockType, wallInfoBlock); + SpaceWall spaceWall = spaceWallRepository.findSpaceWall(spaceWallId, addSpace.getId(), memberId, flagType); - spaceWallUpdateRequest.getData().getBlocks().forEach(block -> { - BlockType blockType = BlockType.findBlockTypeByString(block.getBlockType()); - Long position = blocksPositionCounter.getAndIncrement(); - switch (blockType) { - case FREE_BLOCK: - List freeBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateFreeBlockIds = updateFreeBlocks(freeBlockRequests); - updateFreeBlockIds.forEach(freeBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, freeBlockId, block)); - break; - case SNS_BLOCK: - List snsBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateSnsBlockIds = updateSnsBlocks(snsBlockRequests); - updateSnsBlockIds.forEach(snsBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, snsBlockId, block)); - break; - case TEMPLATE_BLOCK: - List templateBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateTemplateBlockIds = updateTemplateBlocks(templateBlockRequests); - updateTemplateBlockIds.forEach(templateBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, templateBlockId, block)); - break; - case FILE_BLOCK: - List fileBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateFileBlockIds = updateFileBlocks(fileBlockRequests); - updateFileBlockIds.forEach(fileBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, fileBlockId, block)); - break; - case LIST_BLOCK: - List listBlockRequests = jsonMapper.convertValue(block.getSubData(), new TypeReference>() {}); - List updateListBlockIds = updateListBlock(listBlockRequests); - updateListBlockIds.forEach(listBlockId -> addBlockInfoToArray(blockInfoArray, jsonMapper, blockType, position, listBlockId, block)); - } - }); + ArrayNode blockInfoArray = blockJsonProcessor.createArrayNode(); - StyleSettingStringUpdateRequest styleSettingUpdateRequest = dataUpdateRequest.getStyleSetting(); - Long styleSetting = updateStyleSetting(styleSettingUpdateRequest); - String styleSettingString = "styleSetting"; - Long stylePosition = blocksPositionCounter.getAndIncrement(); + List> blocksRequest = data.getBlocks(); - addBlockToJsonArray(blockInfoArray, jsonMapper, stylePosition, styleSettingString, styleSetting); - String blocks = blockInfoArray.toString(); + AtomicLong blocksPositionCounter = new AtomicLong(INITIAL_POSITION); - spaceWall.update(dataUpdateRequest, flagType, blocks); - spaceWallId = spaceWallRepository.save(spaceWall).getId(); + updateWallInfoBlock(data, blockInfoArray, blocksPositionCounter); - return new SpaceWallSaveResponse(spaceWallId); - } + Map> existingBlockIdsByType = new HashMap<>(); + Map> updatedBlockIdsByType = new HashMap<>(); - private Long updateWallInfoBlock(final WallInfoBlockStringUpdateRequest wallInfoBlockRequest) { + String existingBlocks = spaceWall.getBlocks(); - WallInfoBlock wallInfoBlock = wallInfoBlockRepository.findWallInfoBlock(wallInfoBlockRequest.getWallInfoBlockId()); - wallInfoBlock.update(wallInfoBlockRequest); + processBlocks(blocksRequest, blockInfoArray, blocksPositionCounter, existingBlockIdsByType, updatedBlockIdsByType, existingBlocks); - return wallInfoBlockRepository.save(wallInfoBlock).getId(); - } + deleteRemainingBlocks(existingBlockIdsByType, updatedBlockIdsByType); - private List updateFreeBlocks(final List subData) { - - List updatedFreeBlockIds = new ArrayList<>(); - for (FreeBlockUpdateRequest updateRequest : subData) { - if(updateRequest.getFreeBlockId() == null ){ - 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 { - com.javajober.blocks.freeBlock.domain.FreeBlock freeBlock = freeBlockRepository.findFreeBlock(updateRequest.getFreeBlockId()); - freeBlock.update(updateRequest); - updatedFreeBlockIds.add(freeBlockRepository.save(freeBlock).getId()); - } - } - return updatedFreeBlockIds; - } + updateStyleSettingBlock(data, blockInfoArray, blocksPositionCounter); - private List updateSnsBlocks(final List subData){ - - List updateSnsBlockIds = new ArrayList<>(); - subData.forEach(snsBlockRequest -> { - if(snsBlockRequest.getSnsBlockId() ==null){ - SNSType snsType = SNSType.findSNSTypeByString(snsBlockRequest.getSnsType()); - SNSBlock snsBlock = new SNSBlock(snsBlockRequest.getSnsUUID(),snsType,snsBlockRequest.getSnsURL()); - updateSnsBlockIds.add(snsBlockRepository.save(snsBlock).getId()); - }else { - SNSBlock snsBlock = snsBlockRepository.findSNSBlock(snsBlockRequest.getSnsBlockId()); - snsBlock.update(snsBlockRequest); - updateSnsBlockIds.add(snsBlockRepository.save(snsBlock).getId()); - } - }); - - return updateSnsBlockIds; - } + String blocks = blockInfoArray.toString(); - private List updateTemplateBlocks(final List subData) { - - List updateTemplateBlockIds = new ArrayList<>(); - for(TemplateBlockUpdateRequest updateRequest : subData) { - if (updateRequest.getTemplateBlockId() == null) { - TemplateBlock templateBlock = new TemplateBlock(updateRequest.getTemplateUUID(),updateRequest.getTemplateTitle(),updateRequest.getTemplateDescription()); - updateTemplateBlockIds.add(templateBlockRepository.save(templateBlock).getId()); - } else { - TemplateBlock templateBlock = templateBlockRepository.findTemplateBlock(updateRequest.getTemplateBlockId()); - templateBlock.update(updateRequest.getTemplateUUID(), updateRequest.getTemplateTitle(), updateRequest.getTemplateDescription()); - updateTemplateBlockIds.add(templateBlockRepository.save(templateBlock).getId()); - } - } - return updateTemplateBlockIds; - } + spaceWall.update(data, flagType, blocks); - private List updateFileBlocks(final List subData) { - - List updateFileBlockIds = new ArrayList<>(); - for (FileBlockStringUpdateRequest updateRequest : subData) { - if(updateRequest.getFileBlockId() == null){ - FileBlock fileBlock = new FileBlock(updateRequest.getFileTitle(), updateRequest.getFileDescription(),updateRequest.getFileName(),updateRequest.getFile()); - updateFileBlockIds.add(fileBlockRepository.save(fileBlock).getId()); - }else{ - FileBlock fileBlock = fileBlockRepository.findFileBlock(updateRequest.getFileBlockId()); - fileBlock.update(updateRequest); - updateFileBlockIds.add(fileBlockRepository.save(fileBlock).getId()); - } - } - return updateFileBlockIds; - } + spaceWallId = spaceWallRepository.save(spaceWall).getId(); - private List updateListBlock(final List subData){ - - List updateListBlockIds = new ArrayList<>(); - for(ListBlockUpdateRequest updateRequest : subData){ - if(updateRequest.getListBlockId() == null){ - ListBlock listBlock = new ListBlock(updateRequest.getListUUID(),updateRequest.getListLabel(),updateRequest.getListTitle(),updateRequest.getListDescription(),updateRequest.getIsLink()); - updateListBlockIds.add(listBlockRepository.save(listBlock).getId()); - }else{ - ListBlock listBlock = listBlockRepository.findListBlock(updateRequest.getListBlockId()); - listBlock.update(updateRequest); - updateListBlockIds.add(listBlockRepository.save(listBlock).getId()); - } - } - return updateListBlockIds; + return new SpaceWallSaveResponse(spaceWallId); } - private Long updateStyleSetting(final StyleSettingStringUpdateRequest updateRequest){ - - StyleSetting styleSetting = styleSettingRepository.findStyleBlock(updateRequest.getStyleSettingBlockId()); - updateBackgroundSetting(updateRequest.getBackgroundSetting()); - updateBlockSetting(updateRequest.getBlockSetting()); - updateThemeSetting(updateRequest.getThemeSetting()); - styleSetting.update(styleSetting); + private void updateWallInfoBlock(final DataStringUpdateRequest data, final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter) { + String wallInfoBlockStrategyName = BlockType.WALL_INFO_BLOCK.getStrategyName(); + FixBlockStrategy wallInfoBlockStrategy = blockStrategyFactory.findFixBlockStrategy(wallInfoBlockStrategyName); - return styleSettingRepository.save(styleSetting).getId(); + Long wallInfoBlockPosition = blocksPositionCounter.getAndIncrement(); + wallInfoBlockStrategy.updateBlocks(data, blockInfoArray, wallInfoBlockPosition); } - private Long updateBackgroundSetting(final BackgroundStringUpdateRequest updateRequest){ - - BackgroundSetting backgroundSetting = backgroundSettingRepository.getById(updateRequest.getBackgroundSettingBlockId()); - backgroundSetting.update(updateRequest.getSolidColor(), updateRequest.getGradation(), updateRequest.getStyleImgURL()); - - return backgroundSettingRepository.save(backgroundSetting).getId(); + private void processBlocks(final List> blocksRequest, final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter, + final Map> existingBlockIdsByType, final Map> updatedBlockIdsByType, final String existingBlocks) { + blocksRequest.forEach(block -> { + BlockType blockType = BlockType.findBlockTypeByString(block.getBlockType()); + Long position = blocksPositionCounter.getAndIncrement(); + String strategyName = blockType.getStrategyName(); + MoveBlockStrategy moveBlockStrategy = blockStrategyFactory.findMoveBlockStrategy(strategyName); + processBlock(block, blockInfoArray, position, existingBlockIdsByType, updatedBlockIdsByType, existingBlocks, blockType, moveBlockStrategy); + }); } - private Long updateBlockSetting(final BlockSettingUpdateRequest updateRequest){ + private void processBlock(final BlockSaveRequest block, final ArrayNode blockInfoArray, final Long position, + final Map> existingBlockIdsByType, final Map> updatedBlockIdsByType, final String existingBlocks, final BlockType blockType, MoveBlockStrategy moveBlockStrategy) { - BlockSetting blockSetting = blockSettingRepository.getById(updateRequest.getBlockSettingBlockId()); - blockSetting.update(updateRequest); - - return blockSettingRepository.save(blockSetting).getId(); - } + if (!existingBlockIdsByType.containsKey(blockType)) { + Set existingBlockIdsForThisType = blockJsonProcessor.existingBlockIds(existingBlocks, blockType); + existingBlockIdsByType.put(blockType, existingBlockIdsForThisType); + } - private Long updateThemeSetting(final ThemeSettingUpdateRequest updateRequest){ + Set updatedIds = moveBlockStrategy.updateBlocks(block, blockInfoArray, position); - ThemeSetting themeSetting = themeSettingRepository.getById(updateRequest.getThemeSettingBlockId()); - themeSetting.update(updateRequest.getTheme()); + updatedIds.forEach(blockId -> + blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, blockType, blockId, block.getBlockUUID())); - return themeSettingRepository.save(themeSetting).getId(); + if (!updatedBlockIdsByType.containsKey(blockType)) { + updatedBlockIdsByType.put(blockType, new HashSet<>()); + } + updatedBlockIdsByType.get(blockType).addAll(updatedIds); } - private void addBlockInfoToArray(final ArrayNode blockInfoArray, final ObjectMapper jsonMapper, final BlockType blockType, final Long position, final Long blockId, final BlockSaveRequest block) { - - String currentBlockTypeTitle = blockType.getEngTitle(); - String blockUUID = block.getBlockUUID(); - - ObjectNode blockInfoObject = jsonMapper.createObjectNode(); - - blockInfoObject.put("position", position); - blockInfoObject.put("blockType", currentBlockTypeTitle); - blockInfoObject.put("blockId", blockId); - blockInfoObject.put("blockUUID", blockUUID); - - blockInfoArray.add(blockInfoObject); + private void deleteRemainingBlocks(final Map> existingBlockIdsByType, final Map> updatedBlockIdsByType) { + for (BlockType blockType : existingBlockIdsByType.keySet()) { + Set remainingBlockIds = existingBlockIdsByType.get(blockType); + remainingBlockIds.removeAll(updatedBlockIdsByType.getOrDefault(blockType, Collections.emptySet())); + if (!remainingBlockIds.isEmpty()) { + MoveBlockStrategy blockProcessingStrategy = blockStrategyFactory.findMoveBlockStrategy( + (blockType.getStrategyName())); + blockProcessingStrategy.deleteAllById(remainingBlockIds); + } + } } - private void addBlockToJsonArray(final ArrayNode jsonArray, final ObjectMapper mapper, final Long position, final String blockType, final Long blockId) { - - ObjectNode blockInfoObject = mapper.createObjectNode(); + private void updateStyleSettingBlock(final DataStringUpdateRequest data, final ArrayNode blockInfoArray, final AtomicLong blocksPositionCounter) { - blockInfoObject.put("position", position); - blockInfoObject.put("blockType", blockType); - blockInfoObject.put("blockId", blockId); - blockInfoObject.put("blockUUID", (String)null); + String styleSettingBlockStrategyName = BlockType.STYLE_SETTING.getStrategyName(); + FixBlockStrategy styleSettingBlockStrategy = blockStrategyFactory.findFixBlockStrategy(styleSettingBlockStrategyName); - jsonArray.add(blockInfoObject); + Long styleSettingPosition = blocksPositionCounter.getAndIncrement(); + styleSettingBlockStrategy.updateBlocks(data, blockInfoArray, styleSettingPosition); } } \ No newline at end of file diff --git a/src/main/java/com/javajober/spaceWall/strategy/BlockJsonProcessor.java b/src/main/java/com/javajober/spaceWall/strategy/BlockJsonProcessor.java index ce1101e..7074d43 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/BlockJsonProcessor.java +++ b/src/main/java/com/javajober/spaceWall/strategy/BlockJsonProcessor.java @@ -12,8 +12,10 @@ import com.javajober.spaceWall.domain.BlockType; import java.util.Comparator; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -64,4 +66,22 @@ public Map> toJsonNode(String blocks) { throw new ApplicationException(ApiStatus.FAIL, "제이슨 변환 중 실패하였습니다."); } } + + public Set existingBlockIds (final String existingBlocks, final BlockType blockType) { + + Set existingBlockIds; + + try { + JsonNode blocksArrayNode = jsonMapper.readTree(existingBlocks); + existingBlockIds = new HashSet<>(); + for (JsonNode blockNode : blocksArrayNode) { + if (blockNode.get("block_type").asText().equals(blockType.name())) { + existingBlockIds.add(blockNode.get("block_id").asLong()); + } + } + } catch (JsonProcessingException e) { + throw new ApplicationException(ApiStatus.FAIL, "제이슨 변환 중 실패하였습니다."); + } + return existingBlockIds; + } } diff --git a/src/main/java/com/javajober/spaceWall/strategy/FixBlockStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/FixBlockStrategy.java index 8103a53..345365f 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/FixBlockStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/FixBlockStrategy.java @@ -7,6 +7,7 @@ import com.javajober.core.exception.ApplicationException; import com.javajober.core.util.response.CommonResponse; import com.javajober.spaceWall.dto.request.DataStringSaveRequest; +import com.javajober.spaceWall.dto.request.DataStringUpdateRequest; import com.javajober.spaceWall.filedto.DataSaveRequest; import java.util.List; @@ -20,6 +21,8 @@ public interface FixBlockStrategy { CommonResponse createFixBlockDTO(final List fixBlocks); + void updateBlocks(final DataStringUpdateRequest data, final ArrayNode blockInfoArray, final Long position); + String getStrategyName(); default void uploadTwoFiles(final MultipartFile firstFile, final MultipartFile secondFile) { diff --git a/src/main/java/com/javajober/spaceWall/strategy/MoveBlockStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/MoveBlockStrategy.java index f65c1b6..c5ca447 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/MoveBlockStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/MoveBlockStrategy.java @@ -9,6 +9,7 @@ import com.javajober.spaceWall.dto.request.BlockSaveRequest; import java.util.List; +import java.util.Set; import org.springframework.web.multipart.MultipartFile; @@ -17,9 +18,12 @@ public interface MoveBlockStrategy { void saveBlocks(final BlockSaveRequest block, final ArrayNode blockInfoArray, final Long position); - List createMoveBlockDTO(List blocksWithSamePosition); + Set updateBlocks(final BlockSaveRequest blocks, final ArrayNode blockInfoArray, final Long position); + + void deleteAllById(final Set blockIds); + String getStrategyName(); default void uploadFile(final MultipartFile file) { diff --git a/src/main/java/com/javajober/spaceWall/strategy/impl/FileBlockStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/impl/FileBlockStrategy.java index b27c382..3d7b3c3 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/impl/FileBlockStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/impl/FileBlockStrategy.java @@ -1,12 +1,15 @@ package com.javajober.spaceWall.strategy.impl; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.javajober.blocks.fileBlock.dto.request.FileBlockStringUpdateRequest; import com.javajober.blocks.fileBlock.dto.response.FileBlockResponse; import com.javajober.blocks.fileBlock.filedto.FileBlockSaveRequest; import com.javajober.core.util.file.FileImageService; @@ -30,7 +33,7 @@ public class FileBlockStrategy implements MoveBlockStrategy { private final FileImageService fileImageService; private final AtomicReference currentFileName = new AtomicReference<>(); - public FileBlockStrategy(BlockJsonProcessor blockJsonProcessor, FileBlockRepository fileBlockRepository, + public FileBlockStrategy(final BlockJsonProcessor blockJsonProcessor, final FileBlockRepository fileBlockRepository, FileImageService fileImageService) { this.blockJsonProcessor = blockJsonProcessor; this.fileBlockRepository = fileBlockRepository; @@ -80,7 +83,6 @@ public void saveStringBlocks(final BlockSaveRequest block, final ArrayNode bl List stringSavedFileBlocks = saveAllFileBlock(stringFileBlocks); addToFileBlockInfoArray(stringSavedFileBlocks, blockInfoArray, position, block.getBlockType()); - } private List convertSubDataToFileBlockStringSaveRequests(final List subData) { @@ -120,6 +122,39 @@ public List createMoveBlockDTO(final List blocksWithSa return subData; } + @Override + public Set updateBlocks(final BlockSaveRequest blocks, final ArrayNode blockInfoArray, final Long position) { + + List fileBlocks = new ArrayList<>(); + + blocks.getSubData().forEach(block -> { + FileBlockStringUpdateRequest request = blockJsonProcessor.convertValue(block, FileBlockStringUpdateRequest.class); + FileBlock fileBlock = saveOrUpdateFileBlock(request); + fileBlocks.add(fileBlock); + }); + + List updatedFileBlocks = fileBlockRepository.saveAll(fileBlocks); + + return updatedFileBlocks.stream().map(FileBlock::getId).collect(Collectors.toCollection(LinkedHashSet::new)); + } + + private FileBlock saveOrUpdateFileBlock(final FileBlockStringUpdateRequest request) { + + if (request.getFileBlockId() == null) { + return FileBlockStringUpdateRequest.toEntity(request); + } + + FileBlock fileBlock = fileBlockRepository.findFileBlock(request.getFileBlockId()); + fileBlock.update(request); + + return fileBlock; + } + + @Override + public void deleteAllById(final Set blockIds) { + fileBlockRepository.deleteAllById(blockIds); + } + @Override public String getStrategyName() { return BlockStrategyName.FileBlockStrategy.name(); diff --git a/src/main/java/com/javajober/spaceWall/strategy/impl/FreeBlockStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/impl/FreeBlockStrategy.java index 0d19cc3..ab8d881 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/impl/FreeBlockStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/impl/FreeBlockStrategy.java @@ -1,13 +1,16 @@ package com.javajober.spaceWall.strategy.impl; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; 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.request.FreeBlockUpdateRequest; import com.javajober.blocks.freeBlock.dto.response.FreeBlockResponse; import com.javajober.core.util.response.CommonResponse; import org.springframework.stereotype.Component; @@ -42,7 +45,7 @@ public void saveStringBlocks(final BlockSaveRequest block, final ArrayNode bl } @Override - public void saveBlocks(BlockSaveRequest block, ArrayNode blockInfoArray, Long position) { + public void saveBlocks(final BlockSaveRequest block, final ArrayNode blockInfoArray, final Long position) { List freeBlockRequests = convertSubDataToFreeBlockSaveRequests(block.getSubData()); List freeBlocks = convertToFreeBlocks(freeBlockRequests); @@ -89,6 +92,39 @@ public List createMoveBlockDTO(final List blocksWithSa return subData; } + @Override + public Set updateBlocks(final BlockSaveRequest blocks, final ArrayNode blockInfoArray, final Long position) { + + List freeBlocks = new ArrayList<>(); + + blocks.getSubData().forEach(block -> { + FreeBlockUpdateRequest request = blockJsonProcessor.convertValue(block, FreeBlockUpdateRequest.class); + FreeBlock freeBlock = saveOrUpdateFreeBlock(request); + freeBlocks.add(freeBlock); + }); + + List updatedFreeBlocks = freeBlockRepository.saveAll(freeBlocks); + + return updatedFreeBlocks.stream().map(FreeBlock::getId).collect(Collectors.toCollection(LinkedHashSet::new)); + } + + private FreeBlock saveOrUpdateFreeBlock(final FreeBlockUpdateRequest request) { + + if(request.getFreeBlockId() == null) { + return FreeBlockUpdateRequest.toEntity(request); + } + + FreeBlock freeblock = freeBlockRepository.findFreeBlock(request.getFreeBlockId()); + freeblock.update(request); + + return freeblock; + } + + @Override + public void deleteAllById(Set blockIds) { + freeBlockRepository.deleteAllById(blockIds); + } + @Override public String getStrategyName() { return BlockStrategyName.FreeBlockStrategy.name(); diff --git a/src/main/java/com/javajober/spaceWall/strategy/impl/ListBlockStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/impl/ListBlockStrategy.java index 1911df3..1e22872 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/impl/ListBlockStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/impl/ListBlockStrategy.java @@ -1,11 +1,14 @@ package com.javajober.spaceWall.strategy.impl; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.javajober.blocks.listBlock.dto.request.ListBlockUpdateRequest; import com.javajober.blocks.listBlock.dto.response.ListBlockResponse; import com.javajober.core.util.response.CommonResponse; import org.springframework.stereotype.Component; @@ -43,7 +46,7 @@ public void saveStringBlocks(final BlockSaveRequest block, final ArrayNode bl } @Override - public void saveBlocks(BlockSaveRequest block, ArrayNode blockInfoArray, Long position) { + public void saveBlocks(final BlockSaveRequest block, final ArrayNode blockInfoArray, final Long position) { List listBlockRequests = convertSubDataToListBlockSaveRequests(block.getSubData()); List listBlocks = convertToListBlocks(listBlockRequests); @@ -90,6 +93,38 @@ public List createMoveBlockDTO(final List blocksWithSa return subData; } + @Override + public Set updateBlocks(final BlockSaveRequest blocks, final ArrayNode blockInfoArray, final Long position) { + + List listBlocks = new ArrayList<>(); + + blocks.getSubData().forEach(block -> { + ListBlockUpdateRequest request = blockJsonProcessor.convertValue(block, ListBlockUpdateRequest.class); + ListBlock listBlock = saveOrUpdateListBlock(request); + listBlocks.add(listBlock); + }); + + List updatedListBlocks = listBlockRepository.saveAll(listBlocks); + + return updatedListBlocks.stream().map(ListBlock::getId).collect(Collectors.toCollection(LinkedHashSet::new)); + } + + private ListBlock saveOrUpdateListBlock (final ListBlockUpdateRequest request) { + + if(request.getListBlockId() == null) { + return ListBlockUpdateRequest.toEntity(request); + } + + ListBlock listBlock = listBlockRepository.findListBlock(request.getListBlockId()); + listBlock.update(request); + return listBlock; + } + + @Override + public void deleteAllById(final Set blockIds) { + listBlockRepository.deleteAllById(blockIds); + } + @Override public String getStrategyName() { return BlockStrategyName.ListBlockStrategy.name(); diff --git a/src/main/java/com/javajober/spaceWall/strategy/impl/SNSBlockStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/impl/SNSBlockStrategy.java index a01b0ef..5b0ebc2 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/impl/SNSBlockStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/impl/SNSBlockStrategy.java @@ -1,11 +1,14 @@ package com.javajober.spaceWall.strategy.impl; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.javajober.blocks.snsBlock.dto.request.SNSBlockUpdateRequest; import com.javajober.blocks.snsBlock.dto.response.SNSBlockResponse; import com.javajober.core.util.response.CommonResponse; @@ -46,7 +49,7 @@ public void saveStringBlocks(final BlockSaveRequest block, final ArrayNode bl } @Override - public void saveBlocks(BlockSaveRequest block, ArrayNode blockInfoArray, Long position) { + public void saveBlocks(final BlockSaveRequest block, final ArrayNode blockInfoArray, final Long position) { List snsBlockRequests = convertSubDataToSNSBlockSaveRequests(block.getSubData()); List snsBlocks = convertToSNSBlocks(snsBlockRequests); @@ -93,6 +96,40 @@ public List createMoveBlockDTO(final List blocksWithSa return subData; } + @Override + public Set updateBlocks(final BlockSaveRequest blocks, final ArrayNode blockInfoArray, final Long position) { + + List snsBlocks = new ArrayList<>(); + + blocks.getSubData().forEach(block -> { + SNSBlockUpdateRequest request = blockJsonProcessor.convertValue(block, SNSBlockUpdateRequest.class); + SNSBlock snsBlock = saveOrUpdateSNSBlock(request); + snsBlocks.add(snsBlock); + }); + + List updatedSNSBlocks = snsBlockRepository.saveAll(snsBlocks); + + return updatedSNSBlocks.stream().map(SNSBlock::getId).collect(Collectors.toCollection(LinkedHashSet::new)); + + } + + private SNSBlock saveOrUpdateSNSBlock(final SNSBlockUpdateRequest request) { + + if(request.getSnsBlockId() == null) { + return SNSBlockUpdateRequest.toEntity(request); + } + + SNSBlock snsBlock = snsBlockRepository.findSNSBlock(request.getSnsBlockId()); + snsBlock.update(request); + + return snsBlock; + } + + @Override + public void deleteAllById(final Set blockIds) { + snsBlockRepository.deleteAllById(blockIds); + } + @Override public String getStrategyName() { return BlockStrategyName.SNSBlockStrategy.name(); diff --git a/src/main/java/com/javajober/spaceWall/strategy/impl/StyleSettingStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/impl/StyleSettingStrategy.java index 3e7e281..08acf2c 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/impl/StyleSettingStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/impl/StyleSettingStrategy.java @@ -2,11 +2,15 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.javajober.blocks.styleSetting.backgroundSetting.dto.request.BackgroundStringUpdateRequest; import com.javajober.blocks.styleSetting.backgroundSetting.dto.response.BackgroundSettingResponse; import com.javajober.blocks.styleSetting.backgroundSetting.filedto.BackgroundSettingSaveRequest; +import com.javajober.blocks.styleSetting.blockSetting.dto.request.BlockSettingUpdateRequest; import com.javajober.blocks.styleSetting.blockSetting.dto.response.BlockSettingResponse; +import com.javajober.blocks.styleSetting.dto.request.StyleSettingStringUpdateRequest; import com.javajober.blocks.styleSetting.dto.response.StyleSettingResponse; import com.javajober.blocks.styleSetting.filedto.StyleSettingSaveRequest; +import com.javajober.blocks.styleSetting.themeSetting.dto.request.ThemeSettingUpdateRequest; import com.javajober.blocks.styleSetting.themeSetting.dto.response.ThemeSettingResponse; import com.javajober.core.util.file.FileImageService; import com.javajober.core.util.response.CommonResponse; @@ -27,6 +31,7 @@ import com.javajober.blocks.styleSetting.themeSetting.repository.ThemeSettingRepository; import com.javajober.spaceWall.domain.BlockType; import com.javajober.spaceWall.dto.request.DataStringSaveRequest; +import com.javajober.spaceWall.dto.request.DataStringUpdateRequest; import com.javajober.spaceWall.filedto.DataSaveRequest; import com.javajober.spaceWall.strategy.BlockJsonProcessor; import com.javajober.spaceWall.strategy.BlockStrategyName; @@ -58,7 +63,7 @@ public StyleSettingStrategy(final BlockJsonProcessor blockJsonProcessor, final S } @Override - public void saveBlocks(final DataSaveRequest data, ArrayNode blockInfoArray, Long position) { + public void saveBlocks(final DataSaveRequest data, final ArrayNode blockInfoArray, final Long position) { StyleSettingSaveRequest request = data.getStyleSetting(); Long styleSettingId = saveStyleSetting(request); @@ -70,7 +75,7 @@ public void uploadSingleFile(final MultipartFile styleImgURL) { uploadedStyleImageURL.set(fileImageService.uploadFile(styleImgURL)); } - private Long saveStyleSetting(final StyleSettingSaveRequest request){ + private Long saveStyleSetting(final StyleSettingSaveRequest request) { BackgroundSettingSaveRequest backgroundRequest = request.getBackgroundSetting(); BackgroundSetting backgroundSetting = backgroundSettingRepository.save(BackgroundSettingSaveRequest.toEntity(backgroundRequest, uploadedStyleImageURL.get())); @@ -87,14 +92,14 @@ private Long saveStyleSetting(final StyleSettingSaveRequest request){ } @Override - public void saveStringBlocks(DataStringSaveRequest data, ArrayNode blockInfoArray, Long position) { + public void saveStringBlocks(final DataStringSaveRequest data, final ArrayNode blockInfoArray, final Long position) { StyleSettingStringSaveRequest request = data.getStyleSetting(); Long styleSettingId = saveStringStyleSetting(request); blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, BlockType.STYLE_SETTING, styleSettingId, ""); } - private Long saveStringStyleSetting(final StyleSettingStringSaveRequest request){ + private Long saveStringStyleSetting(final StyleSettingStringSaveRequest request) { BackgroundSettingStringSaveRequest backgroundRequest = request.getBackgroundSetting(); BackgroundSetting backgroundSetting = backgroundSettingRepository.save(BackgroundSettingStringSaveRequest.toEntity(backgroundRequest)); @@ -111,7 +116,7 @@ private Long saveStringStyleSetting(final StyleSettingStringSaveRequest request) } @Override - public CommonResponse createFixBlockDTO(List fixBlocks) { + public CommonResponse createFixBlockDTO(final List fixBlocks) { Long blockId = fixBlocks.get(0).path("block_id").asLong(); StyleSetting styleSetting = styleSettingRepository.findStyleBlock(blockId); @@ -122,6 +127,38 @@ public CommonResponse createFixBlockDTO(List fixBlocks) { return new StyleSettingResponse(blockId, backgroundSettingResponse, blockSettingResponse, themeSettingResponse); } + @Override + public void updateBlocks(final DataStringUpdateRequest data, final ArrayNode blockInfoArray, final Long position) { + StyleSettingStringUpdateRequest request = data.getStyleSetting(); + + Long styleSettingId = updateStringStyleSetting(request); + + blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, BlockType.STYLE_SETTING, styleSettingId, ""); + } + + private Long updateStringStyleSetting(final StyleSettingStringUpdateRequest request){ + + BackgroundStringUpdateRequest backgroundRequest = request.getBackgroundSetting(); + BackgroundSetting backgroundSetting = backgroundSettingRepository.getById(backgroundRequest.getBackgroundSettingBlockId()); + backgroundSetting.update(backgroundRequest); + backgroundSettingRepository.save(backgroundSetting); + + BlockSettingUpdateRequest blockSettingRequest = request.getBlockSetting(); + BlockSetting blockSetting = blockSettingRepository.getById(blockSettingRequest.getBlockSettingBlockId()); + blockSetting.update(blockSettingRequest); + blockSettingRepository.save(blockSetting); + + ThemeSettingUpdateRequest themeSettingRequest = request.getThemeSetting(); + ThemeSetting themeSetting = themeSettingRepository.getById(themeSettingRequest.getThemeSettingBlockId()); + themeSetting.update(themeSettingRequest); + themeSettingRepository.save(themeSetting); + + StyleSetting styleSetting = styleSettingRepository.findStyleBlock(request.getStyleSettingBlockId()); + styleSetting.update(backgroundSetting, blockSetting, themeSetting); + + return styleSettingRepository.save(styleSetting).getId(); + } + @Override public String getStrategyName() { return BlockStrategyName.StyleSettingStrategy.name(); diff --git a/src/main/java/com/javajober/spaceWall/strategy/impl/TemplateBlockStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/impl/TemplateBlockStrategy.java index fa5a0a2..6cbed30 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/impl/TemplateBlockStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/impl/TemplateBlockStrategy.java @@ -1,12 +1,15 @@ package com.javajober.spaceWall.strategy.impl; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; import java.util.Collections; +import java.util.Set; import java.util.stream.Collectors; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.javajober.blocks.templateBlock.dto.request.TemplateBlockUpdateRequest; import com.javajober.blocks.templateBlock.dto.response.TemplateBlockResponse; import com.javajober.core.util.response.CommonResponse; import org.springframework.stereotype.Component; @@ -43,7 +46,7 @@ public void saveStringBlocks(final BlockSaveRequest block, final ArrayNode bl } @Override - public void saveBlocks(BlockSaveRequest block, ArrayNode blockInfoArray, Long position) { + public void saveBlocks(final BlockSaveRequest block, final ArrayNode blockInfoArray, final Long position) { List templateBlockRequests = convertSubDataToTemplateBlockSaveRequests(block.getSubData()); List templateBlocks = convertToTemplateBlocks(templateBlockRequests); @@ -80,7 +83,7 @@ private void addToTemplateBlockInfoArray (final List savedTemplat } @Override - public List createMoveBlockDTO(List blocksWithSamePosition) { + public List createMoveBlockDTO(final List blocksWithSamePosition) { List subData = new ArrayList<>(); for (JsonNode block : blocksWithSamePosition) { long blockId = block.path("block_id").asLong(); @@ -90,6 +93,39 @@ public List createMoveBlockDTO(List blocksWithSamePosi return subData; } + @Override + public Set updateBlocks(final BlockSaveRequest blocks, final ArrayNode blockInfoArray, final Long position) { + + List templateBlocks = new ArrayList<>(); + + blocks.getSubData().forEach(block -> { + TemplateBlockUpdateRequest request = blockJsonProcessor.convertValue(block, TemplateBlockUpdateRequest.class); + TemplateBlock templateBlock = saveOrUpdateTemplateBlock(request); + templateBlocks.add(templateBlock); + }); + + List updateTemplateBlocks = templateBlockRepository.saveAll(templateBlocks); + + return updateTemplateBlocks.stream().map(TemplateBlock::getId).collect(Collectors.toCollection(LinkedHashSet::new)); + } + + private TemplateBlock saveOrUpdateTemplateBlock(final TemplateBlockUpdateRequest request) { + + if (request.getTemplateBlockId() == null) { + return TemplateBlockUpdateRequest.toEntity(request); + } + + TemplateBlock templateBlock = templateBlockRepository.findTemplateBlock(request.getTemplateBlockId()); + templateBlock.update(request); + + return templateBlock; + } + + @Override + public void deleteAllById(final Set blockIds) { + templateBlockRepository.deleteAllById(blockIds); + } + @Override public String getStrategyName() { return BlockStrategyName.TemplateBlockStrategy.name(); diff --git a/src/main/java/com/javajober/spaceWall/strategy/impl/WallInfoBlockStrategy.java b/src/main/java/com/javajober/spaceWall/strategy/impl/WallInfoBlockStrategy.java index b35f982..72af555 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/impl/WallInfoBlockStrategy.java +++ b/src/main/java/com/javajober/spaceWall/strategy/impl/WallInfoBlockStrategy.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.javajober.blocks.wallInfoBlock.dto.request.WallInfoBlockStringUpdateRequest; import com.javajober.blocks.wallInfoBlock.dto.response.WallInfoBlockResponse; import com.javajober.blocks.wallInfoBlock.filedto.WallInfoBlockSaveRequest; import com.javajober.core.util.file.FileImageService; @@ -14,6 +15,7 @@ import com.javajober.blocks.wallInfoBlock.repository.WallInfoBlockRepository; import com.javajober.spaceWall.domain.BlockType; import com.javajober.spaceWall.dto.request.DataStringSaveRequest; +import com.javajober.spaceWall.dto.request.DataStringUpdateRequest; import com.javajober.spaceWall.filedto.DataSaveRequest; import com.javajober.spaceWall.strategy.BlockJsonProcessor; import com.javajober.spaceWall.strategy.BlockStrategyName; @@ -30,7 +32,7 @@ public class WallInfoBlockStrategy implements FixBlockStrategy { private final AtomicReference uploadedBackgroundImgURL = new AtomicReference<>(); private final AtomicReference uploadedWallInfoImgURL = new AtomicReference<>(); - public WallInfoBlockStrategy(BlockJsonProcessor blockJsonProcessor, WallInfoBlockRepository wallInfoBlockRepository, + public WallInfoBlockStrategy(final BlockJsonProcessor blockJsonProcessor, final WallInfoBlockRepository wallInfoBlockRepository, FileImageService fileImageService) { this.blockJsonProcessor = blockJsonProcessor; this.wallInfoBlockRepository = wallInfoBlockRepository; @@ -38,7 +40,7 @@ public WallInfoBlockStrategy(BlockJsonProcessor blockJsonProcessor, WallInfoBloc } @Override - public void saveBlocks(DataSaveRequest data, ArrayNode blockInfoArray, Long position) { + public void saveBlocks(final DataSaveRequest data, final ArrayNode blockInfoArray, final Long position) { WallInfoBlockSaveRequest request = data.getWallInfoBlock(); Long wallInfoBlockId = saveWallInfoBlock(request); @@ -51,20 +53,20 @@ public void uploadTwoFiles(final MultipartFile backgroundImgURL, final Multipart uploadedWallInfoImgURL.set(fileImageService.uploadFile(wallInfoImgURL)); } - private Long saveWallInfoBlock(WallInfoBlockSaveRequest request) { + private Long saveWallInfoBlock(final WallInfoBlockSaveRequest request) { WallInfoBlock wallInfoBlock = WallInfoBlockSaveRequest.toEntity(request, uploadedBackgroundImgURL.get(), uploadedWallInfoImgURL.get()); return wallInfoBlockRepository.save(wallInfoBlock).getId(); } @Override - public void saveStringBlocks(DataStringSaveRequest data, ArrayNode blockInfoArray, Long position) { + public void saveStringBlocks(final DataStringSaveRequest data, final ArrayNode blockInfoArray, final Long position) { WallInfoBlockStringSaveRequest request = data.getWallInfoBlock(); Long wallInfoBlockId = saveWallInfoBlock(request); blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, BlockType.WALL_INFO_BLOCK, wallInfoBlockId,""); } - private Long saveWallInfoBlock(WallInfoBlockStringSaveRequest request) { + private Long saveWallInfoBlock(final WallInfoBlockStringSaveRequest request) { WallInfoBlock wallInfoBlock = WallInfoBlockStringSaveRequest.toEntity(request); return wallInfoBlockRepository.save(wallInfoBlock).getId(); @@ -72,13 +74,24 @@ private Long saveWallInfoBlock(WallInfoBlockStringSaveRequest request) { @Override - public CommonResponse createFixBlockDTO(List fixBlocks) { + public CommonResponse createFixBlockDTO(final List fixBlocks) { long blockId = fixBlocks.get(0).path("block_id").asLong(); WallInfoBlock wallInfoBlock = wallInfoBlockRepository.findWallInfoBlock(blockId); return WallInfoBlockResponse.from(wallInfoBlock); } + @Override + public void updateBlocks(final DataStringUpdateRequest data, final ArrayNode blockInfoArray, final Long position) { + WallInfoBlockStringUpdateRequest wallInfoBlockRequest = data.getWallInfoBlock(); + WallInfoBlock wallInfoBlock = wallInfoBlockRepository.findWallInfoBlock(wallInfoBlockRequest.getWallInfoBlockId()); + wallInfoBlock.update(wallInfoBlockRequest); + + Long wallInfoBlockId = wallInfoBlockRepository.save(wallInfoBlock).getId(); + + blockJsonProcessor.addBlockInfoToArray(blockInfoArray, position, BlockType.WALL_INFO_BLOCK, wallInfoBlockId, ""); + } + @Override public String getStrategyName() { return BlockStrategyName.WallInfoBlockStrategy.name();