feat: gallery reorder API, sort_order column, svg support, 50 image limit
- Add PUT /api/event-images/reorder endpoint with transaction support - Update GET /api/event-images/event/:eventId to sort by sort_order ASC - Add sort_order INT column to event_images table schema - Increase multer upload.array limit from 10 to 50 - Add svg to allowed file types in multer fileFilter - Add debug logging to reorder endpoint
This commit is contained in:
parent
dd0701cca4
commit
71a00a578f
51
server.js
51
server.js
@ -389,7 +389,10 @@ app.get('/api/event-images', async (req, res) => {
|
|||||||
// GET event images by event ID
|
// GET event images by event ID
|
||||||
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('SELECT * FROM event_images WHERE eventid = ?', [req.params.eventId]);
|
const [rows] = await pool.execute(
|
||||||
|
'SELECT * FROM event_images WHERE eventid = ? ORDER BY sort_order ASC, id ASC',
|
||||||
|
[req.params.eventId]
|
||||||
|
);
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
data: rows
|
data: rows
|
||||||
@ -469,6 +472,52 @@ app.post('/api/event-images', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Bulk reorder images
|
||||||
|
app.put('/api/event-images/reorder', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { images } = req.body; // Array of { id, sort_order }
|
||||||
|
console.log(`Reorder request received for ${images?.length} images`);
|
||||||
|
|
||||||
|
if (!Array.isArray(images) || images.length === 0) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Images array is required for reordering'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Using a transaction for bulk update
|
||||||
|
const connection = await pool.getConnection();
|
||||||
|
try {
|
||||||
|
await connection.beginTransaction();
|
||||||
|
|
||||||
|
for (const item of images) {
|
||||||
|
await connection.execute(
|
||||||
|
'UPDATE event_images SET sort_order = ? WHERE id = ?',
|
||||||
|
[item.sort_order, item.id]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await connection.commit();
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
message: 'Images reordered successfully'
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Transaction failed, rolling back:', err);
|
||||||
|
await connection.rollback();
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
connection.release();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error reordering images:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Failed to reorder images'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
app.post('/api/event-images/bulk', async (req, res) => {
|
app.post('/api/event-images/bulk', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { eventid, imageurl } = req.body;
|
const { eventid, imageurl } = req.body;
|
||||||
|
|||||||
@ -45,6 +45,7 @@ async function setup() {
|
|||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
eventid INT,
|
eventid INT,
|
||||||
imageurl TEXT NOT NULL,
|
imageurl TEXT NOT NULL,
|
||||||
|
sort_order INT DEFAULT 0,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (eventid) REFERENCES events(id) ON DELETE CASCADE
|
FOREIGN KEY (eventid) REFERENCES events(id) ON DELETE CASCADE
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user