seedroute and vlidate
This commit is contained in:
@ -22,11 +22,12 @@ router.get("/", async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
//add location
|
||||
//add location
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
const { name, added_by, description } = req.body;
|
||||
await addLocation(name, added_by, description);
|
||||
res.sendStatus(200)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
@ -38,6 +39,7 @@ router.put("/update", async (req, res, next) => {
|
||||
try {
|
||||
const { id, name, added_by, description } = req.body;
|
||||
await updateLocation(id, name, added_by, description);
|
||||
res.status(200).json({ message: "Location " + id + " updated" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
@ -49,6 +51,7 @@ router.delete("/delete", async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.body;
|
||||
await deleteLocation(id);
|
||||
res.status(200).json({ message: "Location " + id + " deleted" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
|
42
Web-Server/routes/SeedLocationAndSensor.js
Normal file
42
Web-Server/routes/SeedLocationAndSensor.js
Normal file
@ -0,0 +1,42 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { locationModel } = require("../../Database/model/locationModel.js");
|
||||
const { sensorModel } = require("../../Database/model/sensorModel.js");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
let mockLocation = []
|
||||
|
||||
//add seed
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
console.log(mockLocation)
|
||||
|
||||
for(let locationName of req.body.mockLocation){
|
||||
//create location and create sensor
|
||||
let location = await locationModel.create({
|
||||
name: locationName,
|
||||
added_by: "system",
|
||||
description: "system generated location",
|
||||
});
|
||||
await sensorModel.create({
|
||||
sensorname: `AQI-${Math.floor(Math.random()*898)+101}`,
|
||||
added_by: "system",
|
||||
//random mac address
|
||||
mac_address: `${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}`,
|
||||
description: "system generated sensor",
|
||||
location: location.id
|
||||
|
||||
});
|
||||
}
|
||||
res.sendStatus(200).json({message: "seeded"})
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = router;
|
0
Web-Server/routes/SeedsensorData.js
Normal file
0
Web-Server/routes/SeedsensorData.js
Normal file
11
Web-Server/routes/seed_route.js
Normal file
11
Web-Server/routes/seed_route.js
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
const router = require('express').Router();
|
||||
|
||||
//location route
|
||||
router.use('/seed', require('./SeedLocationAndSensor'));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = router;
|
Reference in New Issue
Block a user