module.exports = [
"[project]/.next-internal/server/app/api/room/delete/route/actions.js [app-rsc] (server actions loader, ecmascript)", ((__turbopack_context__, module, exports) => {

}),
"[externals]/next/dist/compiled/next-server/app-route-turbo.runtime.dev.js [external] (next/dist/compiled/next-server/app-route-turbo.runtime.dev.js, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("next/dist/compiled/next-server/app-route-turbo.runtime.dev.js", () => require("next/dist/compiled/next-server/app-route-turbo.runtime.dev.js"));

module.exports = mod;
}),
"[externals]/next/dist/compiled/@opentelemetry/api [external] (next/dist/compiled/@opentelemetry/api, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("next/dist/compiled/@opentelemetry/api", () => require("next/dist/compiled/@opentelemetry/api"));

module.exports = mod;
}),
"[externals]/next/dist/compiled/next-server/app-page-turbo.runtime.dev.js [external] (next/dist/compiled/next-server/app-page-turbo.runtime.dev.js, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("next/dist/compiled/next-server/app-page-turbo.runtime.dev.js", () => require("next/dist/compiled/next-server/app-page-turbo.runtime.dev.js"));

module.exports = mod;
}),
"[externals]/next/dist/server/app-render/work-unit-async-storage.external.js [external] (next/dist/server/app-render/work-unit-async-storage.external.js, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("next/dist/server/app-render/work-unit-async-storage.external.js", () => require("next/dist/server/app-render/work-unit-async-storage.external.js"));

module.exports = mod;
}),
"[externals]/next/dist/server/app-render/work-async-storage.external.js [external] (next/dist/server/app-render/work-async-storage.external.js, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("next/dist/server/app-render/work-async-storage.external.js", () => require("next/dist/server/app-render/work-async-storage.external.js"));

module.exports = mod;
}),
"[externals]/next/dist/shared/lib/no-fallback-error.external.js [external] (next/dist/shared/lib/no-fallback-error.external.js, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("next/dist/shared/lib/no-fallback-error.external.js", () => require("next/dist/shared/lib/no-fallback-error.external.js"));

module.exports = mod;
}),
"[externals]/next/dist/server/app-render/after-task-async-storage.external.js [external] (next/dist/server/app-render/after-task-async-storage.external.js, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("next/dist/server/app-render/after-task-async-storage.external.js", () => require("next/dist/server/app-render/after-task-async-storage.external.js"));

module.exports = mod;
}),
"[externals]/next/dist/server/app-render/action-async-storage.external.js [external] (next/dist/server/app-render/action-async-storage.external.js, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("next/dist/server/app-render/action-async-storage.external.js", () => require("next/dist/server/app-render/action-async-storage.external.js"));

module.exports = mod;
}),
"[externals]/events [external] (events, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("events", () => require("events"));

module.exports = mod;
}),
"[externals]/process [external] (process, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("process", () => require("process"));

module.exports = mod;
}),
"[externals]/net [external] (net, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("net", () => require("net"));

module.exports = mod;
}),
"[externals]/tls [external] (tls, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("tls", () => require("tls"));

module.exports = mod;
}),
"[externals]/timers [external] (timers, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("timers", () => require("timers"));

module.exports = mod;
}),
"[externals]/stream [external] (stream, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("stream", () => require("stream"));

module.exports = mod;
}),
"[externals]/buffer [external] (buffer, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("buffer", () => require("buffer"));

module.exports = mod;
}),
"[externals]/string_decoder [external] (string_decoder, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("string_decoder", () => require("string_decoder"));

module.exports = mod;
}),
"[externals]/crypto [external] (crypto, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("crypto", () => require("crypto"));

module.exports = mod;
}),
"[externals]/zlib [external] (zlib, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("zlib", () => require("zlib"));

module.exports = mod;
}),
"[externals]/util [external] (util, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("util", () => require("util"));

module.exports = mod;
}),
"[externals]/url [external] (url, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("url", () => require("url"));

module.exports = mod;
}),
"[project]/src/server/db/index.ts [app-route] (ecmascript)", ((__turbopack_context__) => {
"use strict";

// server/db/index.ts
__turbopack_context__.s([
    "addMessage",
    ()=>addMessage,
    "addRoom",
    ()=>addRoom,
    "createUser",
    ()=>createUser,
    "deleteRoom",
    ()=>deleteRoom,
    "findUserByUsername",
    ()=>findUserByUsername,
    "getRoomMessages",
    ()=>getRoomMessages,
    "getUpdatedMessages",
    ()=>getUpdatedMessages,
    "listRoom",
    ()=>listRoom
]);
var __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$mysql2$40$3$2e$14$2e$3$2f$node_modules$2f$mysql2$2f$promise$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/node_modules/.pnpm/mysql2@3.14.3/node_modules/mysql2/promise.js [app-route] (ecmascript)");
;
// ------------------- Database Connection Setup -------------------
// Create a connection pool. Using a pool is more efficient than creating
// a new connection for every query.
const pool = __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$mysql2$40$3$2e$14$2e$3$2f$node_modules$2f$mysql2$2f$promise$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__["default"].createPool({
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || 'password',
    database: process.env.DB_DATABASE || 'my_chat_app',
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
});
// Test the connection
pool.getConnection().then((connection)=>{
    console.log("Successfully connected to the MySQL database.");
    connection.release();
}).catch((err)=>{
    console.error("Error connecting to the MySQL database:", err);
});
const addRoom = async (roomName, user)=>{
    try {
        const [result] = await pool.execute('INSERT INTO rooms (roomName, creater) VALUES (?, ?)', [
            roomName,
            user
        ]);
        console.log(`房间 '${roomName}' 添加成功，ID: ${result.insertId}`);
        return result.insertId;
    } catch (error) {
        console.error(`添加房间 '${roomName}' 失败:`, error);
        throw error;
    }
};
const listRoom = async ()=>{
    // This is a more advanced query to get each room and its LATEST message in one go.
    const query = `
    SELECT
        r.roomId,
        r.roomName,
        m.messageId,
        m.sender,
        m.content,
        m.time
    FROM rooms r
    LEFT JOIN messages m ON m.messageId = (
        SELECT MAX(messageId)
        FROM messages
        WHERE roomId = r.roomId
    )
    ORDER BY r.roomId DESC;
  `;
    const [rows] = await pool.query(query);
    // Map the raw database rows to our desired RoomPreviewInfo structure
    console.log(rows);
    return rows.map((row)=>({
            roomId: row.roomId,
            roomName: row.roomName,
            lastMessage: row.messageId ? {
                messageId: row.messageId,
                roomId: row.roomId,
                sender: row.sender,
                content: row.content,
                time: Number(row.time) // `time` is BIGINT, convert it to number
            } : null
        }));
};
const deleteRoom = async (roomId, user)=>{
    const connection = await pool.getConnection();
    try {
        await connection.beginTransaction();
        // 1. 查找房间并验证创建者
        const [rows] = await connection.execute('SELECT creater FROM rooms WHERE roomId = ?', [
            roomId
        ]);
        if (rows.length === 0) {
            throw new Error(`房间 ${roomId} 不存在`);
        }
        const room = rows[0];
        if (room.creater !== user) {
            throw new Error(`用户 ${user} 无权删除房间 ${roomId}`);
        }
        // 2. 删除房间 (messages will be deleted automatically due to ON DELETE CASCADE)
        await connection.execute('DELETE FROM rooms WHERE roomId = ?', [
            roomId
        ]);
        await connection.commit();
        console.log(`房间 ${roomId} 删除成功`);
    } catch (error) {
        await connection.rollback();
        console.error(`删除房间 ${roomId} 失败:`, error);
        throw error;
    } finally{
        connection.release();
    }
};
const getRoomMessages = async (roomId)=>{
    // 1. 验证房间是否存在
    const [roomRows] = await pool.execute('SELECT 1 FROM rooms WHERE roomId = ?', [
        roomId
    ]);
    if (roomRows.length === 0) {
        throw new Error(`房间 ${roomId} 不存在`);
    }
    // 2. 获取该房间的消息，并按时间排序
    const [messageRows] = await pool.execute('SELECT * FROM messages WHERE roomId = ? ORDER BY messageId ASC', [
        roomId
    ]);
    // Ensure 'time' is a number
    return messageRows.map((msg)=>({
            ...msg,
            time: Number(msg.time)
        }));
};
const addMessage = async (roomId, content, sender)=>{
    // 1. 验证房间是否存在 (optional but good practice)
    const [roomRows] = await pool.execute('SELECT 1 FROM rooms WHERE roomId = ?', [
        roomId
    ]);
    if (roomRows.length === 0) {
        throw new Error(`房间 ${roomId} 不存在`);
    }
    // 2. 创建消息
    const timestamp = Date.now();
    const [result] = await pool.execute('INSERT INTO messages (roomId, sender, content, time) VALUES (?, ?, ?, ?)', [
        roomId,
        sender,
        content,
        timestamp
    ]);
    const newMessage = {
        messageId: result.insertId,
        roomId,
        sender,
        content,
        time: timestamp
    };
    console.log(`消息添加到房间 ${roomId} 成功`);
    return newMessage;
};
const getUpdatedMessages = async (roomId, sinceMessageId)=>{
    const [rows] = await pool.execute('SELECT * FROM messages WHERE roomId = ? AND messageId > ? ORDER BY messageId ASC', [
        roomId,
        sinceMessageId
    ]);
    console.log(`在房间 ${roomId} 找到 ${rows.length} 条新消息 (since ID: ${sinceMessageId})`);
    // Ensure 'time' is a number
    return rows.map((msg)=>({
            ...msg,
            time: Number(msg.time)
        }));
};
const findUserByUsername = async (username)=>{
    try {
        const [rows] = await pool.execute('SELECT * FROM users WHERE username = ?', [
            username
        ]);
        if (rows.length === 0) {
            return null; // 用户不存在
        }
        return rows[0];
    } catch (error) {
        console.error(`Error finding user '${username}':`, error);
        throw error;
    }
};
const createUser = async (username, passwordHash)=>{
    try {
        const [result] = await pool.execute('INSERT INTO users (username, passwordHash) VALUES (?, ?)', [
            username,
            passwordHash
        ]);
        console.log(`User '${username}' created successfully with ID: ${result.insertId}`);
        return {
            userId: result.insertId,
            username: username,
            createdAt: new Date() // 返回当前时间作为创建时间
        };
    } catch (error) {
        console.error(`Error creating user '${username}':`, error);
        // 检查是否是唯一键冲突错误
        if (error instanceof Error && 'code' in error && error.code === 'ER_DUP_ENTRY') {
            throw new Error(`Username '${username}' already exists.`);
        }
        throw error;
    }
};
}),
"[project]/src/app/api/room/delete/route.ts [app-route] (ecmascript)", ((__turbopack_context__) => {
"use strict";

__turbopack_context__.s([
    "DELETE",
    ()=>DELETE,
    "GET",
    ()=>GET,
    "POST",
    ()=>POST,
    "PUT",
    ()=>PUT
]);
var __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$next$40$15$2e$5$2e$0_react$2d$dom$40$19$2e$1$2e$0_react$40$19$2e$1$2e$0_$5f$react$40$19$2e$1$2e$0$2f$node_modules$2f$next$2f$server$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/node_modules/.pnpm/next@15.5.0_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/next/server.js [app-route] (ecmascript)");
var __TURBOPACK__imported__module__$5b$project$5d2f$src$2f$server$2f$db$2f$index$2e$ts__$5b$app$2d$route$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/src/server/db/index.ts [app-route] (ecmascript)");
;
;
async function POST(request) {
    try {
        // 解析请求体
        const body = await request.json();
        const { user, roomId } = body;
        // 验证参数
        if (!user || roomId === undefined || roomId === null) {
            return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$next$40$15$2e$5$2e$0_react$2d$dom$40$19$2e$1$2e$0_react$40$19$2e$1$2e$0_$5f$react$40$19$2e$1$2e$0$2f$node_modules$2f$next$2f$server$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__["NextResponse"].json({
                code: 2,
                message: '缺少必要参数: user 和 roomId 是必需的'
            }, {
                status: 400
            });
        }
        // 删除房间
        await (0, __TURBOPACK__imported__module__$5b$project$5d2f$src$2f$server$2f$db$2f$index$2e$ts__$5b$app$2d$route$5d$__$28$ecmascript$29$__["deleteRoom"])(roomId, user);
        // 返回成功响应（无数据，code 0 表示成功）
        return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$next$40$15$2e$5$2e$0_react$2d$dom$40$19$2e$1$2e$0_react$40$19$2e$1$2e$0_$5f$react$40$19$2e$1$2e$0$2f$node_modules$2f$next$2f$server$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__["NextResponse"].json({
            code: 0
        }, {
            status: 200
        });
    } catch (error) {
        console.error('删除房间失败:', error);
        // 根据错误类型返回不同的错误码
        let statusCode = 500;
        let errorCode = 3;
        let errorMessage = '内部服务器错误';
        if (error.message.includes('不存在')) {
            statusCode = 404;
            errorCode = 4;
            errorMessage = error.message;
        } else if (error.message.includes('无权')) {
            statusCode = 403;
            errorCode = 5;
            errorMessage = error.message;
        }
        return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$next$40$15$2e$5$2e$0_react$2d$dom$40$19$2e$1$2e$0_react$40$19$2e$1$2e$0_$5f$react$40$19$2e$1$2e$0$2f$node_modules$2f$next$2f$server$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__["NextResponse"].json({
            code: errorCode,
            message: errorMessage
        }, {
            status: statusCode
        });
    }
}
async function GET() {
    return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$next$40$15$2e$5$2e$0_react$2d$dom$40$19$2e$1$2e$0_react$40$19$2e$1$2e$0_$5f$react$40$19$2e$1$2e$0$2f$node_modules$2f$next$2f$server$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__["NextResponse"].json({
        code: 1,
        message: 'Method not allowed. Use POST instead'
    }, {
        status: 405
    });
}
async function DELETE() {
    return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$next$40$15$2e$5$2e$0_react$2d$dom$40$19$2e$1$2e$0_react$40$19$2e$1$2e$0_$5f$react$40$19$2e$1$2e$0$2f$node_modules$2f$next$2f$server$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__["NextResponse"].json({
        code: 1,
        message: 'Method not allowed. Use POST instead'
    }, {
        status: 405
    });
}
async function PUT() {
    return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f2e$pnpm$2f$next$40$15$2e$5$2e$0_react$2d$dom$40$19$2e$1$2e$0_react$40$19$2e$1$2e$0_$5f$react$40$19$2e$1$2e$0$2f$node_modules$2f$next$2f$server$2e$js__$5b$app$2d$route$5d$__$28$ecmascript$29$__["NextResponse"].json({
        code: 1,
        message: 'Method not allowed'
    }, {
        status: 405
    });
}
}),
];

//# sourceMappingURL=%5Broot-of-the-server%5D__4ba0ddda._.js.map