Skip to content

Commit

Permalink
🔨 feat: change String to dto #45
Browse files Browse the repository at this point in the history
  • Loading branch information
noparamin committed Nov 11, 2023
1 parent 1d00359 commit e60fe55
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/main/java/com/smart/watchboard/service/MindmapService.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package com.smart.watchboard.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smart.watchboard.domain.Document;
import com.smart.watchboard.domain.Mindmap;
import com.smart.watchboard.dto.KeywordsDto;
import com.smart.watchboard.dto.MindmapDto;
import com.smart.watchboard.dto.MindmapResponseDto;
import com.smart.watchboard.repository.MindmapRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
Expand All @@ -28,16 +22,16 @@ public class MindmapService {
private final MindmapRepository mindmapRepository;
private final WhiteboardService whiteboardService;

public void createMindmap(ResponseEntity<String> responseEntity, Long documentId) throws JsonProcessingException {
public void createMindmap(ResponseEntity<MindmapResponseDto> responseEntity, Long documentId) throws JsonProcessingException {
Document document = whiteboardService.findDoc(documentId);

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(responseEntity.getBody());
Integer root = jsonNode.get("root").asInt();
JsonNode keywordsNode = jsonNode.get("keywords");
List<String> keywords = objectMapper.convertValue(keywordsNode, new TypeReference<List<String>>() {});
JsonNode graphNode = jsonNode.get("graph");
Map<String, List<Integer>> graphMap = objectMapper.convertValue(graphNode, new TypeReference<Map<String, List<Integer>>>() {});
// ObjectMapper objectMapper = new ObjectMapper();
// JsonNode jsonNode = objectMapper.readTree(responseEntity.getBody());
// Integer root = jsonNode.get("root").asInt();
// JsonNode keywordsNode = jsonNode.get("keywords");
// List<String> keywords = objectMapper.convertValue(keywordsNode, new TypeReference<List<String>>() {});
// JsonNode graphNode = jsonNode.get("graph");
// Map<String, List<Integer>> graphMap = objectMapper.convertValue(graphNode, new TypeReference<Map<String, List<Integer>>>() {});

Optional<Mindmap> formerMindmap = mindmapRepository.findByDocumentId(documentId);
if (formerMindmap.isPresent()) {
Expand All @@ -47,8 +41,8 @@ public void createMindmap(ResponseEntity<String> responseEntity, Long documentId
.documentName(document.getDocumentName())
.createdAt(formerMindmap.get().getCreatedAt())
.modifiedAt(Instant.now())
.root(root)
.graph(graphMap)
.root(responseEntity.getBody().getRoot())
.graph(responseEntity.getBody().getGraph())
.build();
mindmapRepository.save(mindmap);
} else if (!formerMindmap.isPresent()) {
Expand All @@ -57,8 +51,8 @@ public void createMindmap(ResponseEntity<String> responseEntity, Long documentId
.documentName(document.getDocumentName())
.createdAt(Instant.now())
.modifiedAt(Instant.now())
.root(root)
.graph(graphMap)
.root(responseEntity.getBody().getRoot())
.graph(responseEntity.getBody().getGraph())
.build();
mindmapRepository.save(mindmap);
}
Expand Down

0 comments on commit e60fe55

Please sign in to comment.