Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
themeEmacs
titleDocumentVersionController.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

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

@Slf4j
@RequiredArgsConstructor
@Controller
public class DocumentVersionController {
	private final DocumentVersionService documentVersionService;

    /**
     * 문서 버전 데이터 저장하기
     */
    @PostMapping("/saveDocumentVersionData")
    public String getDocumentVersionData(
			UserInfo userInfo, 
			@RequestParam("docId") final String docId,
			@RequestParam("jsonData") final String jsonData
	) {
        documentVersionService.insertDocumentVersion(userInfo.getUserId(), domainService.getDomainInfo(userInfo).getId(), Long.parseLong(docId), jsonData);
 		return ResponseEntity.ok(Map.of("message", "Document saved successfully."));  
    }

	/**
     * 문서 버전 목록 가져오기
     */
    @PostMapping("/getDocumentVersionList")
    public ResponseEntity<String> getDocumentVersionList(@RequestBody Map<String, Object> request) throws JsonProcessingException {
        String id		long docId = Long.parseLong((String) request.get("iddocId"));
        
		List<DocumentVersion>List<DocumentVersionModel> documentVersionListdocumentVersionModelList = documentVersionService.getDocumentVersions(iddocId);
        List<Map<String, Object>> formattedData = documentVersionListdocumentVersionModelList.stream().map(version -> {
            Map<String, Object> versionData = new HashMap<>();
            versionData.put("id", version.getId());
            versionData.put("date", version.getCreatedAt());
            versionData.put("author", version.getUsername());
            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) {
        ObjectMapper objectMapper = new ObjectMapper();
        Stringlong id = Long.parseLong((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) {
        Stringlong id = Long.parseLong((String) request.get("id"));
        documentVersionService.deleteDocumentVersion(id);
        return ResponseEntity.ok(Map.of("message", "Document deleted successfully."));  
    }
}

...

Code Block
languagejava
themeEmacs
titleDocumentVersionService.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, Stringlong docId, String json) {
		documentVersionModelDao.insert(new DocumentVersionModel(userId, domainId, docId, json));
	}

	public String getDocumentVersionJson(Stringlong id) {
		return documentVersionModelDao.getDocumentVersionById(id).getJson();
	}

	public List<DocumentVersionModel> getDocumentVersions(Stringlong 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(Stringlong id) {
		documentVersionModelDao.delete(id);
	}
}

...

Code Block
languagejava
themeEmacs
titleDocumentVersionModelDao.java
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Repository
public class DocumentVersionModelDao extends AbstractDao {
    public void insert(DocumentVersionModel documentVersionModel) {
        insert("DocumentVersion.insert", documentVersionModel);
    }

    public void delete(Stringlong id) { delete("DocumentVersion.remove", id); }

    public DocumentVersionModel getDocumentVersionById(Stringlong id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        return (DocumentVersionModel) select("DocumentVersion.getDocumentVersionById", params);
    }

    public List<DocumentVersionModel> getDocumentVersionByDocId(Stringlong docId) {
        return list("DocumentVersion.getDocumentVersionByDocId", docId);
    }
}

...

Code Block
languagejava
themeEmacs
titleDocumentVersionModel.java
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.sql.Date;

@Data
@NoArgsConstructor
public class DocumentVersionModel implements Serializable {
    private long id;
	private String userId;
	private int domainId;
	private String username;
	private Stringlong docId;
	private String json;
	private Date createdAt;

	DocumentVersionModel(String userId, int domainId, Stringlong docId, String json) {
		this.userId = userId;
		this.domainId = domainId;
		this.docId = docId;
		this.json = json;
	}
}

...

Code Block
languagejava
themeEmacs
titledocument_version DDL테이블 구조 (예시)
CREATE TABLE `document_version` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `domain_id` int(11) NOT NULL DEFAULT 1,
  `doc_id` bigint(20) NOT NULL,
  `json` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=671 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

...