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

주의사항

server.js에 기재된대로 api의 request / response 형식을 지켜주세요.

(DB는 예시로 제공될 뿐 원하는 형태로 구성하는 것을 권장드립니다.)

server.js
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);
테이블 구조 (예시)
CREATE TABLE TABLE_NAME (
  `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;

  • No labels