'documentComparison'const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql2/promise');
const _ = require('lodash');
const app = express();
const router = express.Router();
// 테이블 이름 설정
const TABLE_NAME = '';
// 데이터베이스 연결 설정
const pool = mysql.createPool({
host: 'localhost',
user: '', // 사용자명
password: '', // 비밀번호
database: '', // 데이터베이스
});
const controller = {
// 문서 버전 목록 가져오기
getDocumentVersionList: async (req, res) => {
const docId = _.get(req, ['body', 'docId']);
const domainId = _.get(req, ['body', 'domainId']);
const userId = _.get(req, ['body', 'userId']);
try {
const [results] = await pool.query(`SELECT * FROM ${TABLE_NAME} WHERE doc_id = ?`, [docId]);
const userName = getUserName(domainId, userId); // domainId, userId 정보로 userName 가져오기. getUserName은 가상의 함수입니다.
const formattedData = results.map(result => ({
id: result.id,
date: result.created_at,
author: userName
}));
res.json(formattedData);
} catch (error) {
res.status(500).json({ message: 'Internal server error', error: error.message });
}
},
// 문서 버전 데이터 가져오기
getDocumentVersionData: async (req, res) => {
const id = _.get(req, ['body', 'id']);
try {
const [result] = await pool.query(`SELECT json FROM ${TABLE_NAME} WHERE id = ?`, [id]);
if (result.length > 0) {
res.json(result[0].json);
} else {
res.status(400).json({ message: 'Document not found.' });
}
} catch (error) {
res.status(500).json({ message: 'Internal server error', error: error.message });
}
},
// 문서 버전 데이터 삭제
deleteDocumentVersionData: async (req, res) => {
const id = _.get(req, ['body', 'id']);
try {
const [result] = await pool.query(`DELETE FROM ${TABLE_NAME} WHERE id = ?`, [id]);
if (result.affectedRows > 0) {
res.status(200).json({ message: 'Document deleted successfully.' });
} else {
res.status(400).json({ message: 'Document not found.' });
}
} catch (error) {
res.status(500).json({ message: 'Internal server error', error: error.message });
}
}
// 문서 버전 저장
saveDocumentVersionData: (req, res) => {
const docId = _.get(req, ['body', 'docId']);
const userId = _.get(req, ['body', 'userId']);
const domainId = _.get(req, ['body', 'domainId']);
const json = _.get(req, ['body', 'json']);
try {
const [result] = await pool.query(`INSERT INTO ${TABLE_NAME} (user_id, domain_id, doc_id, json) VALUES (?, ?, ?, ?)`, [userId, domainId, docId, json]);
if (result.affectedRows > 0) {
res.status(200).json({ message: 'Document deleted successfully.' });
} else {
res.status(400).json({ message: 'No document was inserted.' });
}
} catch (error) {
res.status(500).json({ message: 'Internal server error', error: error.message });
}
},
};
// 라우팅 설정
router.post('/getDocumentVersionList', controller.getDocumentVersionList);
router.post('/getDocumentVersionData', controller.getDocumentVersionData);
router.delete('/getDocumentVersionData', controller.deleteDocumentVersionData);
router.post('/saveDocumentVersionData', controller.saveDocumentVersionData);
app.use(bodyParser.json());
app.use('/', router);
app.listen(8080);
|