179 lines
4.9 KiB
JavaScript
179 lines
4.9 KiB
JavaScript
import express from 'express';
|
|
import db from '../database.js';
|
|
import { authenticateToken } from '../middleware/auth.js';
|
|
|
|
const router = express.Router();
|
|
|
|
// Get print queue
|
|
router.get('/', authenticateToken, (req, res) => {
|
|
const query = `
|
|
SELECT pq.*,
|
|
m.name as model_name,
|
|
m.file_type,
|
|
m.preview_image,
|
|
m.file_path
|
|
FROM print_queue pq
|
|
JOIN models m ON pq.model_id = m.id
|
|
WHERE pq.user_id = ? AND pq.status = 'pending'
|
|
ORDER BY pq.priority DESC, pq.added_at ASC
|
|
`;
|
|
|
|
db.all(query, [req.user.id], (err, queue) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
res.json({ queue });
|
|
});
|
|
});
|
|
|
|
// Add to print queue
|
|
router.post('/', authenticateToken, (req, res) => {
|
|
const {
|
|
modelId,
|
|
priority = 0,
|
|
notes,
|
|
quantity = 1,
|
|
filament_type,
|
|
color,
|
|
print_temp,
|
|
bed_temp,
|
|
print_speed = 'normal',
|
|
support_structure = 'none',
|
|
infill_density = 20,
|
|
layer_height = 0.2,
|
|
special_instructions
|
|
} = req.body;
|
|
|
|
if (!modelId) {
|
|
return res.status(400).json({ error: 'Model ID is required' });
|
|
}
|
|
|
|
// Check if model exists
|
|
db.get('SELECT id FROM models WHERE id = ?', [modelId], (err, model) => {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
if (!model) {
|
|
return res.status(404).json({ error: 'Model not found' });
|
|
}
|
|
|
|
// Add to queue with all print specifications
|
|
db.run(
|
|
`INSERT INTO print_queue (
|
|
model_id, user_id, priority, notes, quantity, filament_type, color,
|
|
print_temp, bed_temp, print_speed, support_structure, infill_density,
|
|
layer_height, special_instructions
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
modelId, req.user.id, priority, notes, quantity, filament_type, color,
|
|
print_temp, bed_temp, print_speed, support_structure, infill_density,
|
|
layer_height, special_instructions
|
|
],
|
|
function(err) {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
res.json({ message: 'Added to print queue', id: this.lastID });
|
|
}
|
|
);
|
|
});
|
|
});
|
|
|
|
// Update queue item
|
|
router.put('/:id', authenticateToken, (req, res) => {
|
|
const {
|
|
priority,
|
|
notes,
|
|
status,
|
|
quantity,
|
|
filament_type,
|
|
color,
|
|
print_temp,
|
|
bed_temp,
|
|
print_speed,
|
|
support_structure,
|
|
infill_density,
|
|
layer_height,
|
|
special_instructions
|
|
} = req.body;
|
|
|
|
db.run(
|
|
`UPDATE print_queue
|
|
SET priority = COALESCE(?, priority),
|
|
notes = COALESCE(?, notes),
|
|
status = COALESCE(?, status),
|
|
quantity = COALESCE(?, quantity),
|
|
filament_type = COALESCE(?, filament_type),
|
|
color = COALESCE(?, color),
|
|
print_temp = COALESCE(?, print_temp),
|
|
bed_temp = COALESCE(?, bed_temp),
|
|
print_speed = COALESCE(?, print_speed),
|
|
support_structure = COALESCE(?, support_structure),
|
|
infill_density = COALESCE(?, infill_density),
|
|
layer_height = COALESCE(?, layer_height),
|
|
special_instructions = COALESCE(?, special_instructions),
|
|
completed_at = CASE WHEN ? = 'completed' THEN CURRENT_TIMESTAMP ELSE completed_at END
|
|
WHERE id = ? AND user_id = ?`,
|
|
[
|
|
priority, notes, status, quantity, filament_type, color, print_temp,
|
|
bed_temp, print_speed, support_structure, infill_density, layer_height,
|
|
special_instructions, status, req.params.id, req.user.id
|
|
],
|
|
function(err) {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
if (this.changes === 0) {
|
|
return res.status(404).json({ error: 'Queue item not found' });
|
|
}
|
|
res.json({ message: 'Queue item updated' });
|
|
}
|
|
);
|
|
});
|
|
|
|
// Remove from queue
|
|
router.delete('/:id', authenticateToken, (req, res) => {
|
|
db.run(
|
|
'DELETE FROM print_queue WHERE id = ? AND user_id = ?',
|
|
[req.params.id, req.user.id],
|
|
function(err) {
|
|
if (err) {
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
if (this.changes === 0) {
|
|
return res.status(404).json({ error: 'Queue item not found' });
|
|
}
|
|
res.json({ message: 'Removed from queue' });
|
|
}
|
|
);
|
|
});
|
|
|
|
// Reorder queue
|
|
router.post('/reorder', authenticateToken, (req, res) => {
|
|
const { queueOrder } = req.body; // Array of { id, priority }
|
|
|
|
if (!Array.isArray(queueOrder)) {
|
|
return res.status(400).json({ error: 'Queue order array is required' });
|
|
}
|
|
|
|
db.serialize(() => {
|
|
db.run('BEGIN TRANSACTION');
|
|
|
|
queueOrder.forEach(item => {
|
|
db.run(
|
|
'UPDATE print_queue SET priority = ? WHERE id = ? AND user_id = ?',
|
|
[item.priority, item.id, req.user.id]
|
|
);
|
|
});
|
|
|
|
db.run('COMMIT', (err) => {
|
|
if (err) {
|
|
db.run('ROLLBACK');
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
res.json({ message: 'Queue reordered successfully' });
|
|
});
|
|
});
|
|
});
|
|
|
|
export default router;
|