import express, { query } from 'express'; import { createDatabaseConnection } from '../db/database.js'; // db 연결 파일 import { dbConfig } from '../private/config.js'; import { HandlePush } from './push.js'; import { HandleUser } from './user.js'; const router = express.Router(); router.get('/version', async (req, res) => { const {os_type} = req.query; try { const connection = await createDatabaseConnection(dbConfig); let query = 'SELECT * FROM version'; const queryParams = []; if (os_type) { query += ' '; query += 'WHERE os_type = ?'; queryParams.push(os_type); } // const [rows] = await connection.query('SELECT * FROM version'); const [rows] = await connection.query(query, queryParams); await connection.end(); res.json(rows); } catch (err) { res.status(500).json({ error: err.message }); } }); router.get('/push', HandlePush); router.get('/user/*', HandleUser); export default router; // const getQueryModule = async (type, className) => { // if (type === 'create') { // const module = await import('../../db/queries/create_query.js'); // return module[className]; // } // else if (type === 'read') { // const module = await import('../../db/queries/read_query.js'); // return module[className]; // } // else if (type === 'update') { // const module = await import('../../db/queries/update_query.js'); // return module[className]; // } // else if (type === 'delete') { // const module = await import('../../db/queries/update_query.js'); // return module[className]; // } // else if (type === 'function') { // const module = await import('../../db/queries/functions_query.js'); // return module[className]; // } // return '' // }; // app.get(`${cmcd}/push`, async (req, res) => { // // HandlePush(req, res); // //3138117983JJ20240611 // // const { cid, title, body, cd } = req.query; // const { cid, title, body, badge, parameter } = req.query; // if (!cid || !title || !body) { // return res.status(400).json({ error: 'All parameters (cid, title, body) are required' }); // } // let query; // query = await getQueryModule('function', 'Push'); // if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); } // try { // const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db'}); // const [rows] = await connection.query(query.loadFCM_token, [cid]); // await connection.end(); // if (rows.length === 0) { // return res.status(404).json({ error: 'No fcm_token found for the provided cid' }); // } // const fcmToken = rows[0].fcm_token; // console.log(fcmToken); // // HandlePush 호출 시 필요한 req.body에 fcmToken, title, body 설정 // req.body = { fcmToken, title, body, badge, parameter }; // HandlePush(req, res); // } catch (err) { // console.error(err); // res.status(500).json({ error: 'Internal server error' }); // } // }); // DB 연결 테스트 // app.get(`${db}/test`, async (req, res) => { // try { // const connection = await createDatabaseConnection(dbConfig); // const [rows] = await connection.query('SELECT 1 + 1 AS solution'); // await connection.end(); // res.json({ result: rows[0].solution }); // } catch (err) { // res.status(500).json({ error: err.message }); // } // }); // /////////////////////////////////////////////////////////////// // // GET // /////////////////////////////////////////////////////////////// // app.get(`${db}/version/read`, async (req, res) => { // const { os_type } = req.query; // if (!os_type) { return res.status(400).json({ error: 'ostype parameter is required' }) } // let query; // query = await getQueryModule('read', 'Read'); // if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); } // try { // const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db' }); // const [rows] = await connection.query(query.version, [os_type]); // await connection.end(); // if (rows.length === 0) { // return res.status(404).json({ error: 'No data found' }); // } // res.json(rows); // } catch (err) { // console.error(err); // res.status(500).json({ error: 'Internal server error' }); // } // }); // app.get(`${db}/userInfo/read`, async (req, res) => { // const { id, type } = req.query; // if (!id || !type) { return res.status(400).json({ error: 'ostype parameter is required' }) } // let query; // query = await getQueryModule('read', 'Read'); // if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); } // try { // const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db' }); // const [rows] = await connection.query(query.login_code, [id, type]); // await connection.end(); // if (rows.length === 0) { // return res.status(404).json({ error: 'No data found' }); // } // res.json(rows); // } catch (err) { // console.error(err); // res.status(500).json({ error: 'Internal server error' }); // } // }); // /////////////////////////////////////////////////////////////// // // POST // /////////////////////////////////////////////////////////////// // async function insertLog(table, connection, query, body) { // if (table === 'version') { // const { date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log } = body; // await connection.query( query, [date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log] ); // } // } // app.post(`${db}/version/update`, async (req, res) => { // const { date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log } = req.body; // try { // const connection = await createDatabaseConnection({ ...dbConfig}); // let query; // // 버전 정보 업데이트 // query = await getQueryModule('update', 'Update'); // if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); } // await connection.query( query.version,[final_ver, force_ver, update_ver, update_choice, os_type]); // // 로그 생성 // query = await getQueryModule('create', 'Log'); // if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); } // await insertLog('version', connection, query.version_Log, req.body); // await connection.end(); // res.json({ message: 'Update successful' }); // } catch (err) { // res.status(500).json({ error: err.message }); // } // }); // /////////////////////////////////////////////////////////////// // // LOG 관련 // // DB Log 읽어들이기 // app.get(`${db}/log/read`, async (req, res) => { // console.log('[JJ]Log: Version'); // const { param } = req.query; // try { // const connection = await createDatabaseConnection({ ...dbConfig }); // let query, rows; // // let rows; // if (param === 'version') { // query = await getQueryModule('read', 'Log'); // if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); } // [rows] = await connection.query(query.version_Log); // } // await connection.end(); // res.json(rows); // } catch (err) { // console.error(err); // res.status(500).json({ error: 'Internal server error' }); // } // }); // export default app;