Skip to content

Commit

Permalink
🔨 fix: change logic for update #43
Browse files Browse the repository at this point in the history
  • Loading branch information
noparamin committed Nov 9, 2023
1 parent 11b5656 commit f2d11ee
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions src/main/java/com/smart/watchboard/service/MindmapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class MindmapService {

private final MindmapRepository mindmapRepository;
private final WhiteboardService whiteboardService;
//private final RequestService requestService;

public void createMindmap(ResponseEntity<String> responseEntity, Long documentId) throws JsonProcessingException {
Document document = whiteboardService.findDoc(documentId);
Expand All @@ -40,22 +39,34 @@ public void createMindmap(ResponseEntity<String> responseEntity, Long documentId
JsonNode graphNode = jsonNode.get("graph");
Map<String, List<Integer>> graphMap = objectMapper.convertValue(graphNode, new TypeReference<Map<String, List<Integer>>>() {});

Mindmap mindmap = Mindmap.builder()
.documentId(documentId)
.documentName(document.getDocumentName())
.createdAt(Instant.now())
.modifiedAt(Instant.now())
.root(root)
.keywords(keywords)
.graph(graphMap)
.build();

mindmapRepository.save(mindmap);
Optional<Mindmap> formerMindmap = mindmapRepository.findByDocumentId(documentId);
if (formerMindmap.isPresent()) {
Mindmap mindmap = Mindmap.builder()
.objectId(formerMindmap.get().getObjectId())
.documentId(documentId)
.documentName(document.getDocumentName())
.createdAt(formerMindmap.get().getCreatedAt())
.modifiedAt(Instant.now())
.root(root)
.graph(graphMap)
.build();
mindmapRepository.save(mindmap);
} else if (!formerMindmap.isPresent()) {
Mindmap mindmap = Mindmap.builder()
.documentId(documentId)
.documentName(document.getDocumentName())
.createdAt(Instant.now())
.modifiedAt(Instant.now())
.root(root)
.graph(graphMap)
.build();
mindmapRepository.save(mindmap);
}
}

public MindmapDto getMindmap(Long documentId) {
Optional<Mindmap> mindmap = mindmapRepository.findByDocumentId(documentId);
MindmapDto mindmapDto = new MindmapDto(mindmap.get().getRoot(), mindmap.get().getKeywords(), mindmap.get().getGraph());
MindmapDto mindmapDto = new MindmapDto(mindmap.get().getRoot(), mindmap.get().getGraph());

return mindmapDto;
}
Expand All @@ -64,21 +75,21 @@ public void deleteMindmap(Long documentId) {
mindmapRepository.deleteByDocumentId(documentId);
}

public void updateKeywords(KeywordsDto keywordsDto, Long documentId) {
Optional<Mindmap> mindmap = mindmapRepository.findByDocumentId(documentId);
List<String> keywords = mindmap.get().getKeywords();
List<String> addKeywords = keywordsDto.getAdd();
List<String> deleteKeywords = keywordsDto.getDelete();

List<String> newKeywords = new ArrayList<>(keywords);
newKeywords.removeAll(deleteKeywords);
newKeywords.addAll(addKeywords);

Mindmap updatedMindmap = mindmap.orElse(null);
updatedMindmap.setKeywords(newKeywords);

mindmapRepository.save(updatedMindmap);
}
// public void updateKeywords(KeywordsDto keywordsDto, Long documentId) {
// Optional<Mindmap> mindmap = mindmapRepository.findByDocumentId(documentId);
// List<String> keywords = mindmap.get().getKeywords();
// List<String> addKeywords = keywordsDto.getAdd();
// List<String> deleteKeywords = keywordsDto.getDelete();
//
// List<String> newKeywords = new ArrayList<>(keywords);
// newKeywords.removeAll(deleteKeywords);
// newKeywords.addAll(addKeywords);
//
// Mindmap updatedMindmap = mindmap.orElse(null);
// updatedMindmap.setKeywords(newKeywords);
//
// mindmapRepository.save(updatedMindmap);
// }

// public ResponseEntity<String> getKeywordAnswer(Long documentId, String keywordLabel) throws JsonProcessingException {
// return requestService.requestAnswer(documentId, keywordLabel);
Expand Down

0 comments on commit f2d11ee

Please sign in to comment.