Back/apps/user.js
seonkyu.kim 4dc0105060
All checks were successful
JJ_Back/pipeline/head This commit looks good
[] 결과값 전체 출력으로 변경
2024-08-30 09:11:15 +09:00

58 lines
2.2 KiB
JavaScript

import express from 'express';
import { createDatabaseConnection } from '../db/database.js'; // db 연결 파일을 import
import { dbConfig } from '../private/config.js';
export const HandleUser = async (req, res) => {
const actionPath = req.params[0]; // 'userRead/sns'와 같은 나머지 경로를 잡아냄
const actionParts = actionPath.split('/'); // 슬래시로 나누어 배열로 변환
const action = actionParts[0]; // 첫 번째 부분 ('userRead')
if (action === 'userRead') {
// res.send(`Action: ${action}, ${actionParts}`); // 이 줄은 테스트용이므로 실제 응답 전에 사용 금지
let query;
let queryParams;
try {
const connection = await createDatabaseConnection(dbConfig);
if (actionParts[1] === 'sns') {
const { sns_id } = req.query; // 쿼리 파라미터에서 sns_id를 받아옴
if (!sns_id) {
await connection.end();
return res.status(400).json({ error: 'sns_id is required' });
}
query = 'SELECT * FROM user_info WHERE sns_id = ?';
queryParams = [sns_id];
}
else if (actionParts[1] === 'app') {
const { app_id } = req.query;
if (!app_id) {
await connection.end();
return res.status(400).json({ error: 'app_id is required' });
}
query = 'SELECT * FROM user_info WHERE app_id = ?';
queryParams = [app_id];
}
else {
await connection.end();
return res.status(400).json({ error: 'Invalid sub-action' });
}
const [rows] = await connection.query(query, queryParams);
await connection.end();
if (rows.length === 0) {
return res.status(404).json({ message: 'User not found' });
}
return res.json(rows); // 사용자의 첫 번째 정보를 반환
} catch (err) {
return res.status(500).json({ error: err.message });
}
} else {
return res.status(400).send('Invalid action');
}
};