changes- sort-order column added
This commit is contained in:
parent
44a90afe19
commit
356ea71fe4
47
server.js
47
server.js
@ -81,6 +81,8 @@ const pool = mysql.createPool({
|
|||||||
charset: 'utf8mb4'
|
charset: 'utf8mb4'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function ensureAdminUsersTable() {
|
async function ensureAdminUsersTable() {
|
||||||
try {
|
try {
|
||||||
await pool.execute(`
|
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
|
// Test database connection on startup
|
||||||
async function testDatabaseConnection() {
|
async function testDatabaseConnection() {
|
||||||
try {
|
try {
|
||||||
@ -133,6 +164,7 @@ async function testDatabaseConnection() {
|
|||||||
await connection.execute('SELECT 1');
|
await connection.execute('SELECT 1');
|
||||||
connection.release();
|
connection.release();
|
||||||
await ensureAdminUsersTable();
|
await ensureAdminUsersTable();
|
||||||
|
await ensureEventImagesSortOrderColumn();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Database connection failed:', error.message);
|
console.error('❌ Database connection failed:', error.message);
|
||||||
process.exit(1);
|
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) => {
|
app.get('/api/event-images/event/:eventId', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const [rows] = await pool.execute(
|
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]
|
[req.params.eventId]
|
||||||
);
|
);
|
||||||
res.json({
|
res.json({
|
||||||
@ -631,6 +663,7 @@ app.post('/api/event-images', async (req, res) => {
|
|||||||
// Bulk reorder images
|
// Bulk reorder images
|
||||||
app.put('/api/event-images/reorder', async (req, res) => {
|
app.put('/api/event-images/reorder', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
console.log("Request body:", req.body);
|
||||||
const { images } = req.body; // Array of { id, sort_order }
|
const { images } = req.body; // Array of { id, sort_order }
|
||||||
console.log(`Reorder request received for ${images?.length} images`);
|
console.log(`Reorder request received for ${images?.length} images`);
|
||||||
|
|
||||||
@ -666,12 +699,18 @@ app.put('/api/event-images/reorder', async (req, res) => {
|
|||||||
connection.release();
|
connection.release();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} 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({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Failed to reorder images'
|
message: error.message,
|
||||||
|
code: error.code
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/api/event-images/bulk', async (req, res) => {
|
app.post('/api/event-images/bulk', async (req, res) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user