diff --git a/src/main/java/com/javajober/spaceWall/strategy/BlockJsonProcessor.java b/src/main/java/com/javajober/spaceWall/strategy/BlockJsonProcessor.java index ac27052..c5c72f2 100644 --- a/src/main/java/com/javajober/spaceWall/strategy/BlockJsonProcessor.java +++ b/src/main/java/com/javajober/spaceWall/strategy/BlockJsonProcessor.java @@ -1,5 +1,9 @@ package com.javajober.spaceWall.strategy; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.javajober.core.exception.ApiStatus; +import com.javajober.core.exception.ApplicationException; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.ObjectMapper; @@ -7,6 +11,12 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.javajober.spaceWall.dto.request.BlockSaveRequest; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + @Component public class BlockJsonProcessor { private final ObjectMapper jsonMapper; @@ -33,7 +43,7 @@ public void addBlockInfoToArray(final ArrayNode blockInfoArray, final Long posit blockInfoObject.put("position", position); blockInfoObject.put("block_type", currentBlockTypeTitle); blockInfoObject.put("block_id", blockId); - blockInfoObject.put("block_uuid", blockUUID); + blockInfoObject.put("block_UUID", blockUUID); blockInfoArray.add(blockInfoObject); } @@ -46,8 +56,19 @@ public void addBlockToJsonArray(final ArrayNode blockInfoArray, final Long posit blockInfoObject.put("position", position); blockInfoObject.put("block_type", blockType); blockInfoObject.put("block_id", blockId); - blockInfoObject.put("block_uuid", ""); + blockInfoObject.put("block_UUID", ""); blockInfoArray.add(blockInfoObject); } + + public Map> toJsonNode(String blocks) { + try { + JsonNode jsonNode = jsonMapper.readTree(blocks); + return StreamSupport.stream(jsonNode.spliterator(), false) + .sorted(Comparator.comparingInt(a -> a.get("position").asInt())) + .collect(Collectors.groupingBy(node -> (long) node.get("position").asInt())); + } catch (JsonProcessingException e) { + throw new ApplicationException(ApiStatus.FAIL, "제이슨 변환 실패"); + } + } }