seedroute and vlidate
This commit is contained in:
parent
088302fa5b
commit
90754c1792
@ -15,7 +15,7 @@ const api_log_Model = sequelize.define(
|
|||||||
allowNull: true,
|
allowNull: true,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
validator: {
|
validate: {
|
||||||
isNumeric: true,
|
isNumeric: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -23,7 +23,7 @@ const api_log_Model = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 45,
|
length: 45,
|
||||||
validator: {
|
validate: {
|
||||||
isIP: true,
|
isIP: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -31,7 +31,7 @@ const api_log_Model = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 20,
|
length: 20,
|
||||||
validator: {
|
validate: {
|
||||||
//validate time: new Date().toUTCString(),
|
//validate time: new Date().toUTCString(),
|
||||||
isValidDateString(value) {
|
isValidDateString(value) {
|
||||||
if (!isValidDateString(value)) {
|
if (!isValidDateString(value)) {
|
||||||
@ -46,7 +46,7 @@ const api_log_Model = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validator: {
|
validate: {
|
||||||
isIn: [["GET", "POST", "PUT", "DELETE"]],
|
isIn: [["GET", "POST", "PUT", "DELETE"]],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -54,18 +54,12 @@ const api_log_Model = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 45,
|
length: 45,
|
||||||
validator: {
|
|
||||||
//http://localhost/api/test
|
|
||||||
//remember to add domain name to the list
|
|
||||||
isIn: [["localhost"]],
|
|
||||||
//isFqdn: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
statusCode: {
|
statusCode: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validator: {
|
validate: {
|
||||||
isNumeric: true,
|
isNumeric: true,
|
||||||
len: [1, 3],
|
len: [1, 3],
|
||||||
},
|
},
|
||||||
@ -74,7 +68,7 @@ const api_log_Model = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validator: {
|
validate: {
|
||||||
isNumeric: true,
|
isNumeric: true,
|
||||||
len: [1, 100],
|
len: [1, 100],
|
||||||
},
|
},
|
||||||
@ -83,7 +77,7 @@ const api_log_Model = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 45,
|
length: 45,
|
||||||
validator: {
|
validate: {
|
||||||
isString(value) {
|
isString(value) {
|
||||||
if (typeof value !== "string") {
|
if (typeof value !== "string") {
|
||||||
throw new Error("Referrer must be a string");
|
throw new Error("Referrer must be a string");
|
||||||
@ -97,7 +91,7 @@ const api_log_Model = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 100,
|
length: 100,
|
||||||
validator: {
|
validate: {
|
||||||
isString(value) {
|
isString(value) {
|
||||||
if (typeof value !== "string") {
|
if (typeof value !== "string") {
|
||||||
throw new Error("UserAgent must be a string");
|
throw new Error("UserAgent must be a string");
|
||||||
|
@ -11,7 +11,7 @@ const locationModel = sequelize.define(
|
|||||||
allowNull: true,
|
allowNull: true,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
validator: {
|
validate: {
|
||||||
isNumeric: true,
|
isNumeric: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -19,7 +19,7 @@ const locationModel = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validator: {
|
validate: {
|
||||||
notEmpty: { msg: "Name cannot be empty" },
|
notEmpty: { msg: "Name cannot be empty" },
|
||||||
len: [1, 20],
|
len: [1, 20],
|
||||||
/*
|
/*
|
||||||
@ -27,7 +27,8 @@ const locationModel = sequelize.define(
|
|||||||
"hello world" (contains a space)
|
"hello world" (contains a space)
|
||||||
"hello@123" (contains a symbol)
|
"hello@123" (contains a symbol)
|
||||||
"" (empty string)
|
"" (empty string)
|
||||||
|
is: /^[a-z]+$/i, // matches this RegExp
|
||||||
|
is: ["^[a-z]+$",'i'],
|
||||||
*/
|
*/
|
||||||
is: ["^[a-z0-9]+$", "i"]
|
is: ["^[a-z0-9]+$", "i"]
|
||||||
},
|
},
|
||||||
@ -36,16 +37,9 @@ const locationModel = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validator: {
|
validate: {
|
||||||
notEmpty: { msg: "Added by cannot be empty" },
|
notEmpty: { msg: "Added by cannot be empty" },
|
||||||
len: [1, 20],
|
len: [1, 20],
|
||||||
/*
|
|
||||||
//will not validate this and fail it
|
|
||||||
"hello world" (contains a space)
|
|
||||||
"hello@123" (contains a symbol)
|
|
||||||
"" (empty string)
|
|
||||||
|
|
||||||
*/
|
|
||||||
is: ["^[a-z0-9]+$", "i"]
|
is: ["^[a-z0-9]+$", "i"]
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -53,17 +47,16 @@ const locationModel = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
length: 100,
|
length: 100,
|
||||||
validator: {
|
validate: {
|
||||||
notEmpty: { msg: "Description cannot be empty" },
|
notEmpty: { msg: "Description cannot be empty" },
|
||||||
len: [1, 100],
|
len: [1, 100],
|
||||||
/*
|
/*
|
||||||
//will not validate this and fail it
|
//will not validate this and fail it
|
||||||
"hello world" (contains a space)
|
|
||||||
"hello@123" (contains a symbol)
|
"hello@123" (contains a symbol)
|
||||||
"" (empty string)
|
"" (empty string)
|
||||||
|
|
||||||
*/
|
*/
|
||||||
is: ["^[a-z0-9]+$", "i"]
|
is: ["^[a-zA-Z0-9 ]+$", "i"]
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
createdAt: {
|
createdAt: {
|
||||||
|
@ -6,29 +6,20 @@ const { sensorDataModel } = require("../../Database/model/sensorDataModel.js");
|
|||||||
|
|
||||||
|
|
||||||
async function getLocation() {
|
async function getLocation() {
|
||||||
try {
|
|
||||||
const location = await locationModel.findAll();
|
const location = await locationModel.findAll();
|
||||||
return location;
|
return location;
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addLocation(name, added_by, description) {
|
async function addLocation(name, added_by, description) {
|
||||||
try {
|
|
||||||
const location = await locationModel.create({
|
const location = await locationModel.create({
|
||||||
name: name,
|
name: name,
|
||||||
added_by: added_by,
|
added_by: added_by,
|
||||||
description: description,
|
description: description,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateLocation(id, name, added_by, description) {
|
async function updateLocation(id, name, added_by, description) {
|
||||||
try {
|
|
||||||
//update by id
|
|
||||||
const location = await locationModel.update(
|
const location = await locationModel.update(
|
||||||
{
|
{
|
||||||
name: name,
|
name: name,
|
||||||
@ -41,35 +32,24 @@ async function updateLocation(id, name, added_by, description) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteLocation(id) {
|
async function deleteLocation(id) {
|
||||||
try {
|
|
||||||
//delete by id
|
//delete by id
|
||||||
const location = await locationModel.destroy({
|
const location = await locationModel.destroy({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getLocationById(id) {
|
async function getLocationById(id) {
|
||||||
try {
|
|
||||||
const location = await locationModel.findAll({
|
const location = await locationModel.findAll({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return location;
|
return location;
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getSensor() {
|
async function getSensor() {
|
||||||
|
0
Web-Server/functions/SeedCRUD.js
Normal file
0
Web-Server/functions/SeedCRUD.js
Normal file
@ -15,15 +15,18 @@ app.disable("x-powered-by");
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.set("json spaces", 2);
|
app.set("json spaces", 2);
|
||||||
|
|
||||||
const { APIlogger } = require('../middleware/ApiLogger.js');
|
//const { APIlogger } = require('../middleware/ApiLogger.js');
|
||||||
|
|
||||||
//middleware logic ( called by next() )
|
//middleware logic ( called by next() )
|
||||||
//app.use('/api/v0', require('../middleware/ApiKey.js'));
|
//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
|
//route logic
|
||||||
app.use("/api/v0", require("../routes/api_route.js"));
|
app.use("/api/v0", require("../routes/api_route.js"));
|
||||||
|
|
||||||
|
//seed logic
|
||||||
|
app.use("/api/seed/v0", require("../routes/seed_route.js"));
|
||||||
|
|
||||||
// Catch 404 and forward to error handler. If none of the above routes are
|
// Catch 404 and forward to error handler. If none of the above routes are
|
||||||
// used, this is what will be called.
|
// used, this is what will be called.
|
||||||
app.use(function (req, res, next) {
|
app.use(function (req, res, next) {
|
||||||
@ -39,22 +42,26 @@ app.use(function (err, req, res, next) {
|
|||||||
if(![ 404].includes(err.status || res.status)){
|
if(![ 404].includes(err.status || res.status)){
|
||||||
console.error(err.message);
|
console.error(err.message);
|
||||||
console.error(err.stack);
|
console.error(err.stack);
|
||||||
console.error("=========================================");
|
console.error('=========================================');
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(err.name + " validation error");
|
||||||
|
// Parse key error for Sequilzw
|
||||||
|
let keyErrors = {}
|
||||||
|
if(['SequelizeValidationError'].includes(err.name) && err.errors){
|
||||||
|
for(let item of err.errors){
|
||||||
|
if(item.path){
|
||||||
|
keyErrors[item.path] = item.message
|
||||||
}
|
}
|
||||||
//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.status(err.status || 500);
|
||||||
|
console.log(keyErrors);
|
||||||
res.json({
|
res.json({
|
||||||
name: err.name,
|
name: err.name,
|
||||||
message: err.message,
|
message: err.message,
|
||||||
|
keyErrors,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
@ -27,6 +27,7 @@ router.post("/new", async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { name, added_by, description } = req.body;
|
const { name, added_by, description } = req.body;
|
||||||
await addLocation(name, added_by, description);
|
await addLocation(name, added_by, description);
|
||||||
|
res.sendStatus(200)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -38,6 +39,7 @@ router.put("/update", async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { id, name, added_by, description } = req.body;
|
const { id, name, added_by, description } = req.body;
|
||||||
await updateLocation(id, name, added_by, description);
|
await updateLocation(id, name, added_by, description);
|
||||||
|
res.status(200).json({ message: "Location " + id + " updated" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -49,6 +51,7 @@ router.delete("/delete", async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { id } = req.body;
|
const { id } = req.body;
|
||||||
await deleteLocation(id);
|
await deleteLocation(id);
|
||||||
|
res.status(200).json({ message: "Location " + id + " deleted" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(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;
|
13
api.MD
13
api.MD
@ -8,12 +8,22 @@ curl http://localhost/api/v0/location/3
|
|||||||
//post
|
//post
|
||||||
curl localhost/api/v0/location/new -H "Content-Type: application/json" -X POST -d '{"name": "test", "added_by": "noot" , "description": "test"}'
|
curl localhost/api/v0/location/new -H "Content-Type: application/json" -X POST -d '{"name": "test", "added_by": "noot" , "description": "test"}'
|
||||||
|
|
||||||
|
status: 200
|
||||||
|
|
||||||
//put
|
//put
|
||||||
curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json" -d '{"id": "2" , "name": "test", "added_by": "noot", "description": "test12344444444"}'
|
curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json" -d '{"id": "2" , "name": "test", "added_by": "noot", "description": "test12344444444"}'
|
||||||
|
|
||||||
|
status: 200
|
||||||
|
"message": "Location 13 updated"
|
||||||
|
|
||||||
//delete
|
//delete
|
||||||
curl localhost/api/v0/location/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "6" }'
|
curl localhost/api/v0/location/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "6" }'
|
||||||
|
|
||||||
|
status: 200
|
||||||
|
{
|
||||||
|
"message": "Location 13 deleted"
|
||||||
|
}
|
||||||
|
|
||||||
//sensor
|
//sensor
|
||||||
//GET all
|
//GET all
|
||||||
curl localhost/api/v0/sensor
|
curl localhost/api/v0/sensor
|
||||||
@ -68,3 +78,6 @@ curl localhost/api/v0/sensor-data/update -H "Content-Type: application/json" -X
|
|||||||
//delete
|
//delete
|
||||||
curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "3" }'
|
curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "3" }'
|
||||||
|
|
||||||
|
//seed
|
||||||
|
|
||||||
|
curl localhost/api/seed/v0/seed/new -H "Content-Type: application/json" -X POST -d '{"mockLocation": ["test", "test123"]}'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user