backend validation wip
This commit is contained in:
@ -2,6 +2,8 @@ const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { locationModel } = require("../../Database/model/locationModel.js");
|
||||
const { sensorModel } = require("../../Database/model/sensorModel.js");
|
||||
const { sensorDataModel } = require("../../Database/model/sensorDataModel.js");
|
||||
|
||||
|
||||
async function getLocation() {
|
||||
try {
|
||||
@ -9,7 +11,6 @@ async function getLocation() {
|
||||
return location;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,7 +23,6 @@ async function addLocation(name, added_by, description) {
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,7 +43,6 @@ async function updateLocation(id, name, added_by, description) {
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,7 +56,6 @@ async function deleteLocation(id) {
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,7 +69,6 @@ async function getLocationById(id) {
|
||||
return location;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,31 +78,31 @@ async function getSensor() {
|
||||
return sensor;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function addSensor(sensortype, added_by, description, location) {
|
||||
async function addSensor(sensorname, added_by, mac_address , description, location) {
|
||||
try {
|
||||
const sensor = await sensorModel.create({
|
||||
sensortype: sensortype,
|
||||
sensorname: sensorname,
|
||||
added_by: added_by,
|
||||
mac_address: mac_address,
|
||||
description: description,
|
||||
location: location,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function updateSensor(id, sensortype, added_by, description, location) {
|
||||
async function updateSensor(id, sensorname, added_by, mac_address ,description, location) {
|
||||
try {
|
||||
//update by id
|
||||
const sensor = await sensorModel.update(
|
||||
{
|
||||
sensortype: sensortype,
|
||||
sensorname: sensorname,
|
||||
added_by: added_by,
|
||||
mac_address: mac_address,
|
||||
description: description,
|
||||
location: location,
|
||||
|
||||
@ -118,7 +115,6 @@ async function updateSensor(id, sensortype, added_by, description, location) {
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@ -133,7 +129,6 @@ async function deleteSensor(id) {
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -147,34 +142,88 @@ async function getSensorById(id) {
|
||||
return sensor;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function getSensorData() {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.findAll();
|
||||
return sensorData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function addSensorData(id , id_sensor , id_location , sensordata){
|
||||
try{
|
||||
console.log(typeof sensordata);
|
||||
console.log(sensordata);
|
||||
if (!sensordata){
|
||||
console.log("Sensor Data is null");
|
||||
}
|
||||
const sensorData = await sensorDataModel.create({
|
||||
id: id,
|
||||
id_sensor: id_sensor,
|
||||
id_location: id_location,
|
||||
sensordata: sensordata,
|
||||
});
|
||||
}catch(error){
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async function updateSensorData(id, id_sensor, id_location, sensordata) {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.update(
|
||||
{
|
||||
id_sensor: id_sensor,
|
||||
id_location: id_location,
|
||||
sensordata: sensordata,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async function deleteSensorData(id) {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.destroy({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getSensorDataById(id) {
|
||||
try {
|
||||
const sensorData = await sensorDataModel.findAll({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
return sensorData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getallData() {
|
||||
try {
|
||||
const allData = await IoTModel.findAll({
|
||||
attributes: [
|
||||
"id",
|
||||
"psiData",
|
||||
"humidityData",
|
||||
"o3Data",
|
||||
"no2Data",
|
||||
"so2Data",
|
||||
"coData",
|
||||
"temperatureData",
|
||||
"windspeedData",
|
||||
"currentTime",
|
||||
"regionData",
|
||||
"createdAt",
|
||||
"updatedAt",
|
||||
],
|
||||
});
|
||||
const allData = await IoTModel.findAll();
|
||||
return allData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -187,7 +236,6 @@ async function getLatestData() {
|
||||
return latestData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -204,4 +252,9 @@ module.exports = {
|
||||
updateSensor,
|
||||
deleteSensor,
|
||||
getSensorById,
|
||||
getSensorData,
|
||||
addSensorData,
|
||||
updateSensorData,
|
||||
deleteSensorData,
|
||||
getSensorDataById,
|
||||
};
|
||||
|
@ -22,4 +22,11 @@ function validateData(data) {
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = { validateData };
|
||||
const dateRegex = /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/;
|
||||
|
||||
function isValidDateString(){
|
||||
return dateRegex.test(value);
|
||||
}
|
||||
|
||||
|
||||
module.exports = { validateData , isValidDateString };
|
||||
|
@ -15,11 +15,11 @@ app.disable("x-powered-by");
|
||||
app.use(express.json());
|
||||
app.set("json spaces", 2);
|
||||
|
||||
//const { APIlogger } = require('../middleware/ApiLogger.js');
|
||||
const { APIlogger } = require('../middleware/ApiLogger.js');
|
||||
|
||||
//middleware logic
|
||||
//middleware logic ( called by next() )
|
||||
//app.use('/api/v0', require('../middleware/ApiKey.js'));
|
||||
//app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
|
||||
app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
|
||||
|
||||
//route logic
|
||||
app.use("/api/v0", require("../routes/api_route.js"));
|
||||
@ -41,13 +41,20 @@ app.use(function (err, req, res, next) {
|
||||
console.error(err.stack);
|
||||
console.error("=========================================");
|
||||
}
|
||||
//validation error
|
||||
if (err.name === "SequelizeValidationError") {
|
||||
err.status = 400;
|
||||
err.message = "Validation Error";
|
||||
}
|
||||
else if (err.name === "SequelizeUniqueConstraintError")
|
||||
{
|
||||
console.log("this is my custom error!" + err);
|
||||
}
|
||||
|
||||
res.status(err.status || 500);
|
||||
res.json({
|
||||
name: err.name,
|
||||
message: err.message,
|
||||
runner: err.runner && err.runner.name,
|
||||
duration: err.duration,
|
||||
});
|
||||
});
|
||||
app.listen(port, () => {
|
||||
|
@ -23,8 +23,8 @@ router.get("/", async (req, res, next) => {
|
||||
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
const { sensortype, added_by, description, location } = req.body;
|
||||
await addSensor(sensortype, added_by, description, location);
|
||||
const { sensorname, added_by, mac_address , description, location } = req.body;
|
||||
await addSensor(sensorname, added_by, mac_address ,description, location);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
@ -33,8 +33,8 @@ router.post("/new", async (req, res, next) => {
|
||||
|
||||
router.put("/update", async (req, res, next) => {
|
||||
try {
|
||||
const { id, sensortype, added_by, description, location } = req.body;
|
||||
await updateSensor(id, sensortype, added_by, description, location);
|
||||
const { id, sensorname, added_by, mac_address ,description, location } = req.body;
|
||||
await updateSensor(id, sensorname, added_by, mac_address , description, location);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
|
68
Web-Server/routes/SensorData.js
Normal file
68
Web-Server/routes/SensorData.js
Normal file
@ -0,0 +1,68 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { sensorDataModel } = require("../../Database/model/sensorDataModel.js");
|
||||
const {
|
||||
getSensorData,
|
||||
addSensorData,
|
||||
updateSensorData,
|
||||
deleteSensorData,
|
||||
getSensorDataById,
|
||||
|
||||
} = require("../functions/APIDatabase.js");
|
||||
|
||||
const express = require("express");
|
||||
const { json } = require("body-parser");
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", async (req, res, next) => {
|
||||
try {
|
||||
const sensor = await getSensorData();
|
||||
res.json(sensor);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/new", async (req, res, next) => {
|
||||
try {
|
||||
//JSON.parse(d) /* d is the parameter of the method 'add()' */
|
||||
|
||||
const { id, id_sensor, id_location, sensordata } = req.body;
|
||||
await addSensorData(id , id_sensor , id_location , sensordata);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.put("/update", async (req, res, next) => {
|
||||
try {
|
||||
const { id , id_sensor , id_location , sensordata } = req.body;
|
||||
await updateSensorData( id , id_sensor , id_location , sensordata );
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.delete("/delete", async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.body;
|
||||
await deleteSensorData(id);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/:id", async (req, res, next) => {
|
||||
try {
|
||||
const sensor = await getSensorDataById(req.params.id);
|
||||
res.json(sensor);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
@ -23,12 +23,14 @@ router.use('/location', require('./Location'));
|
||||
//sensor route
|
||||
router.use('/sensor', require('./Sensor'))
|
||||
|
||||
//sensor data route
|
||||
router.use('/sensor-data', require('./SensorData'));
|
||||
|
||||
|
||||
|
||||
|
||||
router.use('/test' , require('./test'));
|
||||
router.use('/latest-data', require('./latest-data'));
|
||||
router.use('/:month', require('./monthlyData'));
|
||||
|
||||
|
||||
|
||||
|
@ -1,21 +0,0 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { getallData } = require("../functions/APIDatabase.js");
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
//get month from url
|
||||
console.log(req.params.month);
|
||||
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
|
||||
// Export the router
|
||||
module.exports = router;
|
Reference in New Issue
Block a user