Fix jwt-token
This commit is contained in:
151
server/routes/detections.js
Normal file
151
server/routes/detections.js
Normal file
@@ -0,0 +1,151 @@
|
||||
const express = require('express');
|
||||
const DroneDetection = require('../models/DroneDetection');
|
||||
const Device = require('../models/Device');
|
||||
const { authenticateToken } = require('../middleware/auth');
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* GET /api/detections
|
||||
* Get all drone detections with filtering and pagination
|
||||
*/
|
||||
router.get('/', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
device_id,
|
||||
drone_id,
|
||||
start_date,
|
||||
end_date,
|
||||
page = 1,
|
||||
limit = 50,
|
||||
sort = 'timestamp',
|
||||
order = 'desc'
|
||||
} = req.query;
|
||||
|
||||
// Build where clause for filtering
|
||||
const whereClause = {};
|
||||
|
||||
if (device_id) {
|
||||
whereClause.device_id = device_id;
|
||||
}
|
||||
|
||||
if (drone_id) {
|
||||
whereClause.drone_id = drone_id;
|
||||
}
|
||||
|
||||
if (start_date) {
|
||||
whereClause.timestamp = { ...whereClause.timestamp, gte: new Date(start_date) };
|
||||
}
|
||||
|
||||
if (end_date) {
|
||||
whereClause.timestamp = { ...whereClause.timestamp, lte: new Date(end_date) };
|
||||
}
|
||||
|
||||
// Calculate offset for pagination
|
||||
const offset = (parseInt(page) - 1) * parseInt(limit);
|
||||
|
||||
// Query detections with device information
|
||||
const detections = await DroneDetection.findAll({
|
||||
where: whereClause,
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
attributes: ['id', 'device_name', 'device_type', 'location', 'latitude', 'longitude']
|
||||
}],
|
||||
order: [[sort, order.toUpperCase()]],
|
||||
limit: parseInt(limit),
|
||||
offset: offset
|
||||
});
|
||||
|
||||
// Get total count for pagination
|
||||
const totalCount = await DroneDetection.count({
|
||||
where: whereClause
|
||||
});
|
||||
|
||||
// Calculate pagination info
|
||||
const totalPages = Math.ceil(totalCount / parseInt(limit));
|
||||
const hasNextPage = parseInt(page) < totalPages;
|
||||
const hasPrevPage = parseInt(page) > 1;
|
||||
|
||||
res.json({
|
||||
detections,
|
||||
pagination: {
|
||||
currentPage: parseInt(page),
|
||||
totalPages,
|
||||
totalCount,
|
||||
limit: parseInt(limit),
|
||||
hasNextPage,
|
||||
hasPrevPage
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching detections:', error);
|
||||
res.status(500).json({
|
||||
error: 'Failed to fetch detections',
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/detections/:id
|
||||
* Get a specific detection by ID
|
||||
*/
|
||||
router.get('/:id', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const detection = await DroneDetection.findByPk(id, {
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
attributes: ['id', 'device_name', 'device_type', 'location', 'latitude', 'longitude']
|
||||
}]
|
||||
});
|
||||
|
||||
if (!detection) {
|
||||
return res.status(404).json({ error: 'Detection not found' });
|
||||
}
|
||||
|
||||
res.json(detection);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching detection:', error);
|
||||
res.status(500).json({
|
||||
error: 'Failed to fetch detection',
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* DELETE /api/detections/:id
|
||||
* Delete a specific detection (admin only)
|
||||
*/
|
||||
router.delete('/:id', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
// Check if user is admin
|
||||
if (req.user.role !== 'admin') {
|
||||
return res.status(403).json({ error: 'Admin access required' });
|
||||
}
|
||||
|
||||
const { id } = req.params;
|
||||
|
||||
const detection = await DroneDetection.findByPk(id);
|
||||
if (!detection) {
|
||||
return res.status(404).json({ error: 'Detection not found' });
|
||||
}
|
||||
|
||||
await detection.destroy();
|
||||
res.json({ message: 'Detection deleted successfully' });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error deleting detection:', error);
|
||||
res.status(500).json({
|
||||
error: 'Failed to delete detection',
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -9,6 +9,7 @@ const dashboardRoutes = require('./dashboard');
|
||||
const healthRoutes = require('./health');
|
||||
const debugRoutes = require('./debug');
|
||||
const detectorsRoutes = require('./detectors');
|
||||
const detectionsRoutes = require('./detections');
|
||||
|
||||
// API versioning
|
||||
router.use('/v1/devices', deviceRoutes);
|
||||
@@ -17,6 +18,7 @@ router.use('/v1/alerts', alertRoutes);
|
||||
router.use('/v1/dashboard', dashboardRoutes);
|
||||
router.use('/v1/health', healthRoutes);
|
||||
router.use('/v1/detectors', detectorsRoutes);
|
||||
router.use('/v1/detections', detectionsRoutes);
|
||||
|
||||
// Default routes (no version prefix for backward compatibility)
|
||||
router.use('/devices', deviceRoutes);
|
||||
@@ -26,6 +28,7 @@ router.use('/dashboard', dashboardRoutes);
|
||||
router.use('/health', healthRoutes);
|
||||
router.use('/debug', debugRoutes);
|
||||
router.use('/detectors', detectorsRoutes);
|
||||
router.use('/detections', detectionsRoutes);
|
||||
|
||||
// API documentation endpoint
|
||||
router.get('/', (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user