주의사항
Controller에 기재된대로 api의 request / response 형식을 지켜주세요.
(DB는 예시로 제공될 뿐 원하는 형태로 구성하는 것을 권장드립니다.)
DocumentVersionController.java
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) throws JsonProcessingException {
String id = (String) request.get("id");
List<DocumentVersion> documentVersionList = documentVersionService.getDocumentVersions(id);
List<Map<String, Object>> formattedData = documentVersionList.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()
.header("Content-Type", "application/json; charset=UTF-8")
.body(jsonResponse);
}
/**
* 문서 버전 데이터 가져오기
*/
@PostMapping("/getDocumentVersionData")
public String getDocumentVersionData(@RequestBody Map<String, Object> request) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String id = (String) request.get("id");
String json = documentVersionService.getDocumentVersionJson(id);
return ResponseEntity.ok()
.header("Content-Type", "application/json; charset=UTF-8")
.body(json);
}
/**
* 문서 버전 데이터 삭제
*/
@DeleteMapping("/getDocumentVersionData")
public ResponseEntity<Map<String, Object>> deleteDocumentVersionData(@RequestBody Map<String, Object> request) {
String id = (String) request.get("id");
snoteService.deleteDocumentVersion(id);
return ResponseEntity.ok(Map.of("message", "Document deleted successfully."));
}
}
DocumentVersionService.java
import java.io.*;
import java.nio.charset.Charset;
import java.util.Comparator;
import java.util.List;
@Slf4j
@Service
public class DocumentVersionService {
@Autowired
DocumentVersionModelDao documentVersionModelDao;
@Autowired
SynapUserInfoDao synapUserInfoDao;
public void insertDocumentVersion(String userId, int domainId, String docId, String json) {
documentVersionModelDao.insert(new DocumentVersionModel(userId, domainId, docId, json));
}
public String getDocumentVersionJson(String id) {
return documentVersionModelDao.getDocumentVersionById(id).getJson();
}
public List<DocumentVersionModel> getDocumentVersions(String docId) {
List<DocumentVersionModel> documentVersionModelList = documentVersionModelDao.getDocumentVersionByDocId(docId);
documentVersionModelList.sort(Comparator.comparing(DocumentVersionModel::getUserId));
String currentUserId = "";
String targetUsername = "";
for (DocumentVersionModel model : documentVersionModelList) {
if (!model.getUserId().equals(currentUserId)) {
currentUserId = model.getUserId();
targetUsername = synapUserInfoDao.getUserInfoByUserIdAndDomain(currentUserId, model.getDomainId()).getUserName();
}
model.setUsername(targetUsername);
}
documentVersionModelList.sort(Comparator.comparing(DocumentVersionModel::getId).reversed());
return documentVersionModelList;
}
public void deleteDocumentVersion(String id) {
documentVersionModelDao.delete(id);
}
}
DocumentVersionModelDao.java
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;
}
}
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);
}
DocumentVersion.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="DocumentVersion"> <resultMap id="documentVersion" class="synap.editor.server.model2.DocumentVersion"> <result property="id" column="id" columnIndex="1"/> <result property="userId" column="user_id" columnIndex="2"/> <result property="docId" column="doc_id" columnIndex="3"/> <result property="json" column="json" columnIndex="4"/> <result property="createdAt" column="created_at" columnIndex="5"/> </resultMap> <resultMap id="documentVersionList" class="synap.editor.server.model2.DocumentVersion"> <result property="id" column="id" columnIndex="1"/> <result property="userId" column="user_id" columnIndex="2"/> <result property="docId" column="doc_id" columnIndex="3"/> <result property="createdAt" column="created_at" columnIndex="4"/> </resultMap> <insert id="insert"> INSERT INTO revision_note_version(user_id, domain_id, doc_id, json, created_at) VALUES (#userId#, #domainId#, #docId# , #json#, SYSDATE()) </insert> <update id="remove"> UPDATE revision_note_version SET removed = true WHERE id = #id# </update> <update id="updateRemovedForOverflow" parameterClass="java.lang.Long"> UPDATE revision_note_version rv JOIN ( SELECT id FROM revision_note_version WHERE doc_id = #docId# AND removed = false ORDER BY id DESC LIMIT 1 OFFSET 500 ) AS subquery ON rv.id = subquery.id SET rv.removed = true; </update> <select id="getNoteVersionById" resultMap="revisionNoteVersion"> SELECT id, user_id, domain_id, doc_id, json, created_at, removed FROM revision_note_version WHERE id = #id# </select> <select id="getNoteVersionByDocId" resultClass="java.util.List" resultMap="revisionNoteVersionList"> SELECT id, user_id, domain_id, doc_id, created_at, removed FROM revision_note_version WHERE doc_id = #docId# AND removed = false ORDER BY id DESC LIMIT 500 </select> </sqlMap>