feat: Enhance path and task management APIs; add detailed responses and optional filters
This commit is contained in:
116
server/server.js
116
server/server.js
@@ -421,19 +421,66 @@ app.post('/api/turtle/:id/command', (req, res) => {
|
|||||||
app.post('/api/paths', (req, res) => {
|
app.post('/api/paths', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { turtleId, pathName, pathData } = req.body;
|
const { turtleId, pathName, pathData } = req.body;
|
||||||
db.savePath(turtleId, pathName, pathData);
|
const pathId = db.savePath(turtleId, pathName, pathData || []);
|
||||||
res.json({ success: true, message: 'Path saved' });
|
res.json({ success: true, pathId, message: 'Path saved' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get all paths for a turtle
|
// Get all paths (optionally filtered by turtleId)
|
||||||
app.get('/api/paths/:turtleId', (req, res) => {
|
app.get('/api/paths', (req, res) => {
|
||||||
|
try {
|
||||||
|
const paths = db.getPaths();
|
||||||
|
res.json(paths.map(p => ({
|
||||||
|
pathId: p.id,
|
||||||
|
turtleId: p.turtle_id,
|
||||||
|
name: p.path_name,
|
||||||
|
pathData: p.path_data,
|
||||||
|
waypointCount: Array.isArray(p.path_data) ? p.path_data.length : 0,
|
||||||
|
createdAt: p.created_at,
|
||||||
|
updatedAt: p.updated_at
|
||||||
|
})));
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get paths for a specific turtle
|
||||||
|
app.get('/api/paths/turtle/:turtleId', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const turtleId = parseInt(req.params.turtleId);
|
const turtleId = parseInt(req.params.turtleId);
|
||||||
const paths = db.getPaths(turtleId);
|
const paths = db.getPaths(turtleId);
|
||||||
res.json({ paths });
|
res.json(paths.map(p => ({
|
||||||
|
pathId: p.id,
|
||||||
|
turtleId: p.turtle_id,
|
||||||
|
name: p.path_name,
|
||||||
|
pathData: p.path_data,
|
||||||
|
waypointCount: Array.isArray(p.path_data) ? p.path_data.length : 0,
|
||||||
|
createdAt: p.created_at,
|
||||||
|
updatedAt: p.updated_at
|
||||||
|
})));
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get a specific path with full details
|
||||||
|
app.get('/api/paths/:pathId', (req, res) => {
|
||||||
|
try {
|
||||||
|
const pathId = parseInt(req.params.pathId);
|
||||||
|
const path = db.getPath(pathId);
|
||||||
|
if (!path) {
|
||||||
|
return res.status(404).json({ error: 'Path not found' });
|
||||||
|
}
|
||||||
|
res.json({
|
||||||
|
pathId: path.id,
|
||||||
|
turtleId: path.turtle_id,
|
||||||
|
name: path.path_name,
|
||||||
|
waypoints: path.path_data,
|
||||||
|
createdAt: path.created_at,
|
||||||
|
updatedAt: path.updated_at
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
@@ -455,8 +502,9 @@ app.delete('/api/paths/:pathId', (req, res) => {
|
|||||||
// Create a new task
|
// Create a new task
|
||||||
app.post('/api/tasks', (req, res) => {
|
app.post('/api/tasks', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { taskType, taskData, priority } = req.body;
|
const { taskType, priority, assignedTurtleId, parameters } = req.body;
|
||||||
const taskId = db.createTask(taskType, taskData, priority || 0);
|
const taskData = parameters || {};
|
||||||
|
const taskId = db.createTask(taskType, taskData, priority || 0, assignedTurtleId || null);
|
||||||
|
|
||||||
broadcastToClients({
|
broadcastToClients({
|
||||||
type: 'task_created',
|
type: 'task_created',
|
||||||
@@ -471,11 +519,23 @@ app.post('/api/tasks', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get all tasks
|
// Get all tasks (with optional status filter)
|
||||||
app.get('/api/tasks', (req, res) => {
|
app.get('/api/tasks', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const tasks = db.getAllTasks();
|
const status = req.query.status || null;
|
||||||
res.json({ tasks });
|
const rawTasks = db.getAllTasks(status);
|
||||||
|
const tasks = rawTasks.map(t => ({
|
||||||
|
taskId: t.id,
|
||||||
|
taskType: t.task_type,
|
||||||
|
priority: t.priority,
|
||||||
|
status: t.status,
|
||||||
|
assignedTurtleId: t.assigned_turtle_id,
|
||||||
|
parameters: t.task_data,
|
||||||
|
result: t.task_data?.result || null,
|
||||||
|
createdAt: t.created_at,
|
||||||
|
updatedAt: t.updated_at
|
||||||
|
}));
|
||||||
|
res.json(tasks);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
@@ -491,6 +551,42 @@ app.get('/api/tasks/next', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Update task status
|
||||||
|
app.put('/api/tasks/:taskId', (req, res) => {
|
||||||
|
try {
|
||||||
|
const taskId = parseInt(req.params.taskId);
|
||||||
|
const { status, result } = req.body;
|
||||||
|
db.updateTaskStatus(taskId, status, result);
|
||||||
|
|
||||||
|
broadcastToClients({
|
||||||
|
type: 'task_updated',
|
||||||
|
taskId,
|
||||||
|
status
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Delete a task
|
||||||
|
app.delete('/api/tasks/:taskId', (req, res) => {
|
||||||
|
try {
|
||||||
|
const taskId = parseInt(req.params.taskId);
|
||||||
|
db.deleteTask(taskId);
|
||||||
|
|
||||||
|
broadcastToClients({
|
||||||
|
type: 'task_deleted',
|
||||||
|
taskId
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Assign task to turtle
|
// Assign task to turtle
|
||||||
app.post('/api/tasks/:taskId/assign', (req, res) => {
|
app.post('/api/tasks/:taskId/assign', (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user