Page tree
Skip to end of metadata
Go to start of metadata


DocumentVersion.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Entity
@Table(name = " ") // 테이블 이름
public class DocumentVersion {

    @Id
    private String id;
    private String name;
    private String json;
    @Column(name = "create_at")
    private Date createdAt;
    @Column(name = "doc_id")
    private String docId;
    public String getId() {
        return id;
    }

    public void setId(String _id) {
        this.id = _id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getDocId() {
        return docId;
    }

    public void setDocId(String id) {
        this.docId = id;
    }
}


DocumentVersionController.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import synap.editor.server.model2.DocumentVersion;
import synap.editor.server.repository.DocumentVersionRepository;

import java.util.*;
import java.util.stream.Collectors;

@RestController
public class DocumentVersionController {

    @Autowired
    private DocumentVersionRepository documentVersionRepository;

	/**
     * 문서 버전 목록 가져오기
     */
    @PostMapping("/getDocumentVersionList")
    public ResponseEntity<String> getDocumentVersionList(@RequestBody Map<String, Object> request) {
        String id = (String) request.get("id");

        try {
            List<DocumentVersion> results = documentVersionRepository.findVersionsByDocId(id);

            // 결과를 새로운 형식으로 변환 (id, createdAt, name)
            List<Map<String, Object>> formattedData = results.stream().map(version -> {
                Map<String, Object> versionData = new HashMap<>();
                versionData.put("id", version.getId());
                versionData.put("date", version.getCreatedAt());
                versionData.put("author", version.getName());
                return versionData;
            }).collect(Collectors.toList());

            ObjectMapper objectMapper = new ObjectMapper();
            String jsonResponse = objectMapper.writeValueAsString(formattedData);

            return ResponseEntity.ok(jsonResponse);

        } catch (Exception e) {
            return ResponseEntity.status(500).body("{\"message\": \"Internal server error\", \"error\": \"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 문서 버전 데이터 가져오기
     */
    @PostMapping("/getDocumentVersionData")
    public String getDocumentVersionData(@RequestBody Map<String, Object> request) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String id = (String) request.get("id");
        DocumentVersion documentVersion = documentVersionRepository.findVersionsById(id);
        return objectMapper.writeValueAsString(documentVersion.getJson());
    }

    /**
     * 문서 버전 데이터 삭제
     */
    @DeleteMapping("/getDocumentVersionData")
    public ResponseEntity<Map<String, Object>> deleteDocumentVersionData(@RequestBody Map<String, Object> request) {
        String id = (String) request.get("id");

        try {
            DocumentVersion documentVersion = documentVersionRepository.findVersionsById(id);

            if (documentVersion != null) {
                documentVersionRepository.delete(documentVersion);
                return ResponseEntity.ok(Map.of("message", "Document deleted successfully."));
            } else {
                return ResponseEntity.status(404).body(Map.of("message", "Document not found."));
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).body(Map.of("message", "Internal server error", "error", e.getMessage()));
        }
    }
}


DocumentVersionRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import synap.editor.server.model2.DocumentVersion;

import java.util.List;
import java.util.Optional;

@Repository
public interface DocumentVersionRepository extends JpaRepository<DocumentVersion, String> {
    /**
     * Document ID에 해당하는 모든 DocumentVersion 객체를 조회
     */
    List<DocumentVersion> findVersionsByDocId(String docId);

    /**
     * 특정 문서 버전 ID에 해당하는 DocumentVersion 객체를 조회
     */
    DocumentVersion findVersionsById(String id);
}
  • No labels