Ajout de la gestion des images avec scan, suppression et mise à jour des mesures dans la base de données
This commit is contained in:
106
routes/api.js
106
routes/api.js
@@ -12,6 +12,7 @@ const dbTester = require('../test/tester');
|
||||
const cors = require('cors'); // Import the cors package
|
||||
const projectManager = require('../src/project/projectManager.js');
|
||||
const measureManager = require('../src/measure/measureManager.js');
|
||||
const fileWatcher = require('../src/data/filewatcher.js');
|
||||
|
||||
router.use(cors({
|
||||
origin: ['http://127.0.0.1:5500', 'http://localhost:5500', 'http://localhost:3000'],
|
||||
@@ -260,6 +261,20 @@ router.get('/measurements/:id', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/measurements/:projectId/:orderId', async (req, res) => {
|
||||
const projectId = req.params.projectId;
|
||||
const orderId = req.params.orderId;
|
||||
if (!projectId || isNaN(projectId) || !orderId || isNaN(orderId)) {
|
||||
return res.status(400).json({ error: 'Invalid project ID or order ID' });
|
||||
}
|
||||
try {
|
||||
const measurement = await measureManager.getMeasurement(projectId, orderId);
|
||||
res.json(measurement);
|
||||
} catch (error) {
|
||||
serverError.sendError('Error getting measurement:', res, error);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /measurements:
|
||||
@@ -272,12 +287,12 @@ router.get('/measurements/:id', (req, res) => {
|
||||
* description: Internal server error
|
||||
*/
|
||||
router.post('/measurements', (req, res) => {
|
||||
const { project_id, timestamp, image_path, temperature, humidity, completed } = req.body;
|
||||
if (!project_id || !timestamp || !image_path || !temperature || !humidity || !completed) {
|
||||
const { project_id, timestamp, image_path, temperature, humidity} = req.body;
|
||||
if (!project_id || !timestamp || !image_path || !temperature || !humidity) {
|
||||
return res.status(400).json({ error: 'All fields are required' });
|
||||
}
|
||||
const query = 'INSERT INTO public.measurements (project_id, timestamp, image_path, temperature, humidity, completed) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id';
|
||||
db.query(query, [project_id, timestamp, image_path, temperature, humidity, completed], (err, results) => {
|
||||
const query = 'INSERT INTO public.measurements (project_id, timestamp, image_path, temperature, humidity) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id';
|
||||
db.query(query, [project_id, timestamp, image_path, temperature, humidity], (err, results) => {
|
||||
if (err) {
|
||||
serverError.sendError('Erreur lors de l\'ajout de la mesure:', res, err);
|
||||
}
|
||||
@@ -305,21 +320,31 @@ router.post('/measurements', (req, res) => {
|
||||
* 500:
|
||||
* description: Internal server error
|
||||
*/
|
||||
router.delete('/measurements/:id', (req, res) => {
|
||||
router.delete('/measurements/:id', async (req, res) => {
|
||||
const measurementId = req.params.id;
|
||||
if (!measurementId || isNaN(measurementId)) {
|
||||
return res.status(400).json({ error: 'Invalid measurement ID' });
|
||||
}
|
||||
const query = 'DELETE FROM public.measurements WHERE id = $1 RETURNING id';
|
||||
db.query(query, [measurementId], (err, results) => {
|
||||
if (err) {
|
||||
serverError.sendError('Erreur lors de la suppression de la mesure:', res, err);
|
||||
}
|
||||
if (results.rowCount === 0) {
|
||||
return res.status(404).json({ error: 'Aucune mesure trouvée avec cet ID.' });
|
||||
}
|
||||
res.status(200).json({ message: 'Mesure supprimée avec succès', id: measurementId });
|
||||
});
|
||||
try {
|
||||
const measurement = await measureManager.deleteMeasurement(measurementId);
|
||||
res.status(200).json({ message: 'Measurement deleted successfully', id: measurementId });
|
||||
} catch (error) {
|
||||
serverError.sendError('Error deleting measurement:', res, error);
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/measurements/:projectId/:orderId', async (req, res) => {
|
||||
const projectId = req.params.projectId;
|
||||
const orderId = req.params.orderId;
|
||||
if (!projectId || isNaN(projectId) || !orderId || isNaN(orderId)) {
|
||||
return res.status(400).json({ error: 'Invalid project ID or order ID' });
|
||||
}
|
||||
try {
|
||||
const measurement = await measureManager.deleteMeasurementByOrderId(projectId, orderId);
|
||||
res.status(200).json({ message: 'Measurement deleted successfully', id: measurement.id });
|
||||
} catch (error) {
|
||||
serverError.sendError('Error deleting measurement:', res, error);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -561,7 +586,6 @@ const upload = multer({ storage: multer.memoryStorage() });
|
||||
* 500:
|
||||
* description: Internal server error
|
||||
*/
|
||||
|
||||
router.post('/upload', upload.single('image'), async (req, res) => {
|
||||
const { projectId, orderId } = req.body;
|
||||
const image = req.file; // Multer adds the file to req.file
|
||||
@@ -578,4 +602,54 @@ router.post('/upload', upload.single('image'), async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /uploadmeasurement:
|
||||
* post:
|
||||
* description: Use to upload a measurement with an image
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* multipart/form-data:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* image:
|
||||
* type: string
|
||||
* format: binary
|
||||
* projectId:
|
||||
* type: integer
|
||||
* timestamp:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* temperature:
|
||||
* type: number
|
||||
* humidity:
|
||||
* type: number
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Measurement uploaded successfully
|
||||
* 400:
|
||||
* description: All fields are required
|
||||
* 500:
|
||||
* description: Internal server error
|
||||
*/
|
||||
router.post('/uploadmeasurement', upload.single('image'), async (req, res) => {
|
||||
const { projectId, timestamp, temperature, humidity } = req.body;
|
||||
const image = req.file; // Multer adds the file to req.file
|
||||
|
||||
if (!image || !projectId || !timestamp || !temperature || !humidity) {
|
||||
return res.status(400).json({ error: 'All fields are required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const nextOrderId = await measureManager.getNextOrderId(projectId);
|
||||
const imagePath = await measureManager.uploadMeasureImage(image, projectId, nextOrderId);
|
||||
const measurement = await measureManager.addMeasureToProject(projectId, timestamp, imagePath, temperature, humidity, nextOrderId);
|
||||
res.json({ message: 'Measurement uploaded successfully', path: imagePath, id: measurement.id });
|
||||
} catch (error) {
|
||||
serverError.sendError('Error uploading measurement:', res, error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user