165 lines
4.9 KiB
JavaScript
165 lines
4.9 KiB
JavaScript
import express from 'express';
|
|
import db from '../database.js';
|
|
import { authenticateToken } from '../middleware/auth.js';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
const router = express.Router();
|
|
|
|
// Bulk tag operations
|
|
router.post('/tag', authenticateToken, (req, res) => {
|
|
const { modelIds, tags } = req.body;
|
|
|
|
if (!modelIds || !Array.isArray(modelIds) || modelIds.length === 0) {
|
|
return res.status(400).json({ error: 'Model IDs array is required' });
|
|
}
|
|
|
|
if (!tags || !Array.isArray(tags) || tags.length === 0) {
|
|
return res.status(400).json({ error: 'Tags array is required' });
|
|
}
|
|
|
|
// Verify user owns all models
|
|
const placeholders = modelIds.map(() => '?').join(',');
|
|
db.all(
|
|
`SELECT id FROM models WHERE id IN (${placeholders}) AND user_id = ?`,
|
|
[...modelIds, req.user.id],
|
|
(err, models) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
if (models.length !== modelIds.length) {
|
|
return res.status(403).json({ error: 'You can only tag your own models' });
|
|
}
|
|
|
|
// Insert tags and create associations
|
|
db.serialize(() => {
|
|
db.run('BEGIN TRANSACTION');
|
|
|
|
tags.forEach(tagName => {
|
|
// Insert tag if it doesn't exist
|
|
db.run(
|
|
'INSERT OR IGNORE INTO tags (name) VALUES (?)',
|
|
[tagName],
|
|
function(err) {
|
|
if (err) {
|
|
console.error('Error inserting tag:', err);
|
|
return;
|
|
}
|
|
|
|
// Get tag ID
|
|
db.get('SELECT id FROM tags WHERE name = ?', [tagName], (err, tag) => {
|
|
if (err || !tag) {
|
|
console.error('Error getting tag:', err);
|
|
return;
|
|
}
|
|
|
|
// Associate tag with all models
|
|
modelIds.forEach(modelId => {
|
|
db.run(
|
|
'INSERT OR IGNORE INTO model_tags (model_id, tag_id) VALUES (?, ?)',
|
|
[modelId, tag.id]
|
|
);
|
|
});
|
|
});
|
|
}
|
|
);
|
|
});
|
|
|
|
db.run('COMMIT', (err) => {
|
|
if (err) {
|
|
db.run('ROLLBACK');
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
res.json({ message: 'Tags added successfully', count: modelIds.length });
|
|
});
|
|
});
|
|
}
|
|
);
|
|
});
|
|
|
|
// Bulk move to collection
|
|
router.post('/move', authenticateToken, (req, res) => {
|
|
const { modelIds, collectionId } = req.body;
|
|
|
|
if (!modelIds || !Array.isArray(modelIds) || modelIds.length === 0) {
|
|
return res.status(400).json({ error: 'Model IDs array is required' });
|
|
}
|
|
|
|
// Verify user owns all models
|
|
const placeholders = modelIds.map(() => '?').join(',');
|
|
db.all(
|
|
`SELECT id FROM models WHERE id IN (${placeholders}) AND user_id = ?`,
|
|
[...modelIds, req.user.id],
|
|
(err, models) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
if (models.length !== modelIds.length) {
|
|
return res.status(403).json({ error: 'You can only move your own models' });
|
|
}
|
|
|
|
// Update collection
|
|
db.run(
|
|
`UPDATE models SET collection_id = ?, updated_at = CURRENT_TIMESTAMP WHERE id IN (${placeholders})`,
|
|
[collectionId || null, ...modelIds],
|
|
function(err) {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
res.json({ message: 'Models moved successfully', count: this.changes });
|
|
}
|
|
);
|
|
}
|
|
);
|
|
});
|
|
|
|
// Bulk delete
|
|
router.post('/delete', authenticateToken, (req, res) => {
|
|
const { modelIds } = req.body;
|
|
|
|
if (!modelIds || !Array.isArray(modelIds) || modelIds.length === 0) {
|
|
return res.status(400).json({ error: 'Model IDs array is required' });
|
|
}
|
|
|
|
// Get models to delete (with file paths)
|
|
const placeholders = modelIds.map(() => '?').join(',');
|
|
db.all(
|
|
`SELECT * FROM models WHERE id IN (${placeholders}) AND user_id = ?`,
|
|
[...modelIds, req.user.id],
|
|
(err, models) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
|
|
if (models.length === 0) {
|
|
return res.status(404).json({ error: 'No models found or you do not own them' });
|
|
}
|
|
|
|
// Delete files
|
|
models.forEach(model => {
|
|
if (model.file_path && fs.existsSync(model.file_path)) {
|
|
fs.unlinkSync(model.file_path);
|
|
}
|
|
if (model.preview_image && fs.existsSync(model.preview_image)) {
|
|
fs.unlinkSync(model.preview_image);
|
|
}
|
|
});
|
|
|
|
// Delete from database
|
|
db.run(
|
|
`DELETE FROM models WHERE id IN (${placeholders}) AND user_id = ?`,
|
|
[...modelIds, req.user.id],
|
|
function(err) {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
res.json({ message: 'Models deleted successfully', count: this.changes });
|
|
}
|
|
);
|
|
}
|
|
);
|
|
});
|
|
|
|
export default router;
|