changes- sort-order column added
This commit is contained in:
parent
44a90afe19
commit
356ea71fe4
45
server.js
45
server.js
@ -81,6 +81,8 @@ const pool = mysql.createPool({
|
||||
charset: 'utf8mb4'
|
||||
});
|
||||
|
||||
|
||||
|
||||
async function ensureAdminUsersTable() {
|
||||
try {
|
||||
await pool.execute(`
|
||||
@ -125,6 +127,35 @@ async function ensureAdminUsersTable() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function ensureEventImagesSortOrderColumn() {
|
||||
try {
|
||||
const [columns] = await pool.execute("SHOW COLUMNS FROM event_images");
|
||||
const columnNames = columns.map(col => col.Field);
|
||||
|
||||
if (!columnNames.includes("sort_order")) {
|
||||
console.log("Adding sort_order column to event_images...");
|
||||
|
||||
await pool.execute(`
|
||||
ALTER TABLE event_images
|
||||
ADD COLUMN sort_order INT NOT NULL DEFAULT 0
|
||||
`);
|
||||
|
||||
await pool.execute(`
|
||||
UPDATE event_images
|
||||
SET sort_order = id
|
||||
`);
|
||||
|
||||
console.log("sort_order column added successfully.");
|
||||
} else {
|
||||
console.log("sort_order column already exists.");
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error("Failed to ensure sort_order column:", error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
// Test database connection on startup
|
||||
async function testDatabaseConnection() {
|
||||
try {
|
||||
@ -133,6 +164,7 @@ async function testDatabaseConnection() {
|
||||
await connection.execute('SELECT 1');
|
||||
connection.release();
|
||||
await ensureAdminUsersTable();
|
||||
await ensureEventImagesSortOrderColumn();
|
||||
} catch (error) {
|
||||
console.error('❌ Database connection failed:', error.message);
|
||||
process.exit(1);
|
||||
@ -546,7 +578,7 @@ app.get('/api/event-images', async (req, res) => {
|
||||
app.get('/api/event-images/event/:eventId', async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.execute(
|
||||
'SELECT * FROM event_images WHERE eventid = ? ORDER BY id ASC',
|
||||
'SELECT * FROM event_images WHERE eventid = ? ORDER BY sort_order ASC, id ASC',
|
||||
[req.params.eventId]
|
||||
);
|
||||
res.json({
|
||||
@ -631,6 +663,7 @@ app.post('/api/event-images', async (req, res) => {
|
||||
// Bulk reorder images
|
||||
app.put('/api/event-images/reorder', async (req, res) => {
|
||||
try {
|
||||
console.log("Request body:", req.body);
|
||||
const { images } = req.body; // Array of { id, sort_order }
|
||||
console.log(`Reorder request received for ${images?.length} images`);
|
||||
|
||||
@ -666,10 +699,16 @@ app.put('/api/event-images/reorder', async (req, res) => {
|
||||
connection.release();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error reordering images:', error);
|
||||
console.error("===== REORDER ERROR =====");
|
||||
console.error(error);
|
||||
console.error("Message:", error.message);
|
||||
console.error("Code:", error.code);
|
||||
console.error("=========================");
|
||||
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to reorder images'
|
||||
message: error.message,
|
||||
code: error.code
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user