From a207ed7296942c1d7d7b148a698227903201e91c Mon Sep 17 00:00:00 2001 From: dayoung030303 Date: Thu, 5 Jun 2025 17:51:30 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8A=A4=ED=8E=98=EC=9D=B4=EC=8A=A4=20?= =?UTF-8?q?=EB=8B=B9=20=ED=95=98=EB=82=98=EC=9D=98=20=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=ED=94=84=EB=A7=8C=20=EC=83=9D=EC=84=B1=EB=90=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95=20->=20db?= =?UTF-8?q?=EC=97=90=20unique=20=EC=A0=9C=EC=95=BD=20=EC=A1=B0=EA=B1=B4=20?= =?UTF-8?q?(=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EA=B7=B8=EB=9E=98=ED=94=84?= =?UTF-8?q?=EA=B0=80=20=EC=97=86=EB=8A=94=20=EA=B2=BD=EC=9A=B0=EB=8A=94=20?= =?UTF-8?q?=EC=83=88=20=EA=B7=B8=EB=9E=98=ED=94=84=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EB=90=98=EB=8F=84=EB=A1=9D=20=ED=95=A8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../graph/domain/entity/Graph.java | 4 +- .../graph/service/GraphService.java | 45 ++++++++++++++----- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/gp_backend_data/graph/domain/entity/Graph.java b/src/main/java/com/example/gp_backend_data/graph/domain/entity/Graph.java index 053b0e5..8291106 100644 --- a/src/main/java/com/example/gp_backend_data/graph/domain/entity/Graph.java +++ b/src/main/java/com/example/gp_backend_data/graph/domain/entity/Graph.java @@ -12,7 +12,9 @@ @NoArgsConstructor @AllArgsConstructor @Builder -@Table(name = "Graph", schema = "gp_data") +@Table(name = "Graph", schema = "gp_data", uniqueConstraints = { + @UniqueConstraint(columnNames = "space_id") +}) public class Graph { @Id diff --git a/src/main/java/com/example/gp_backend_data/graph/service/GraphService.java b/src/main/java/com/example/gp_backend_data/graph/service/GraphService.java index 919e8bc..1009dff 100644 --- a/src/main/java/com/example/gp_backend_data/graph/service/GraphService.java +++ b/src/main/java/com/example/gp_backend_data/graph/service/GraphService.java @@ -7,6 +7,7 @@ import com.example.gp_backend_data.utils.UUIDHelper; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -22,22 +23,42 @@ public class GraphService { private final UUIDHelper uuidHelper; //특정 스페이스의 그래프 조회 + @Transactional public GraphResponse getGraphBySpaceId(UUID spaceId) { - Optional graphOptional = graphRepository.findBySpaceId(uuidHelper.convertUUIDToByteArray(spaceId)); + byte[] spaceIdBytes = uuidHelper.convertUUIDToByteArray(spaceId); - return graphOptional.map(graph -> { - try { - return new GraphResponse( - uuidHelper.convertByteArrayToUUID(graph.getGraphId()), - uuidHelper.convertByteArrayToUUID(graph.getSpaceId()), - objectMapper.readTree(graph.getData()) - ); - } catch (Exception e) { - throw new RuntimeException("Error parsing graph data", e); - } - }).orElseThrow(() -> new IllegalArgumentException("Graph not found for spaceId: " + spaceId)); + Graph graph = graphRepository.findBySpaceId(spaceIdBytes) + .orElseGet(() -> createNewGraphForSpace(spaceIdBytes)); + + try { + return new GraphResponse( + uuidHelper.convertByteArrayToUUID(graph.getGraphId()), + uuidHelper.convertByteArrayToUUID(graph.getSpaceId()), + objectMapper.readTree(graph.getData()) + ); + } catch (Exception e) { + throw new RuntimeException("Error parsing graph data", e); + } } + private Graph createNewGraphForSpace(byte[] spaceIdBytes) { + try { + UUID newGraphId = UUID.randomUUID(); + + Graph graph = new Graph(); + graph.setGraphId(uuidHelper.convertUUIDToByteArray(newGraphId)); + graph.setSpaceId(spaceIdBytes); + graph.setData("{}"); // 또는 AI summary 생성 로직 + + return graphRepository.save(graph); + } catch (DataIntegrityViolationException e) { + // 동시성 문제로 인한 중복 생성 방지 + return graphRepository.findBySpaceId(spaceIdBytes) + .orElseThrow(() -> new IllegalStateException("Graph creation failed due to race condition")); + } + } + + //그래프 세부 정보 조회 public GraphResponse getGraphById(UUID graphId) { Optional graphOptional = graphRepository.findByGraphId(uuidHelper.convertUUIDToByteArray(graphId));