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

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

주의사항

Controller에 기재된대로 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 가져오기
            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(404).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(404).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 {
            await pool.query(`INSERT INTO ${TABLE_NAME} (user_id, domain_id, doc_id, json) VALUES (?, ?, ?, ?)`, [userId, domainId, docId, json]);
        } 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 your_table_name (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    domain_id INT NOT NULL,
    doc_id INT NOT NULL,
    json JSON NOT NULL,
    create_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

  • No labels