more work!

This commit is contained in:
newtbot 2023-12-31 02:30:49 +08:00
parent 233ebafdcb
commit 234906ada5
18 changed files with 394 additions and 238 deletions

View File

@ -1,45 +0,0 @@
'use strict';
const { Sequelize, DataTypes } = require('sequelize');
const { sequelize } = require("../nySql.js")
const adminUserModel = sequelize.define('adminusers', {
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
username: {
type: DataTypes.STRING,
allowNull: true,
length: 50
},
email: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
password: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
lastLogin: {
type: DataTypes.timestamps,
allowNull: true,
},
jobTitle: {
type: DataTypes.STRING,
allowNull: true,
length: 255
}
},{
timestamps: false, // Disable automatic timestamps
});
module.exports = { adminUserModel }

View File

@ -3,7 +3,7 @@ const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
sequelize.sync();
//sequelize.sync();
const api_log_Model = sequelize.define("api-logs",{
// Model attributes are defined here
id: {

View File

@ -2,7 +2,7 @@
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
sequelize.sync();
//sequelize.sync();
const locationModel = sequelize.define(
"location",
{

View File

@ -0,0 +1,54 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { locationModel } = require("./locationModel");
const { sensorModel } = require("./sensorModel");
//sequelize.sync();
const sensorDataModel = sequelize.define("sensorData",
{
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
},
id_sensor: {
type: DataTypes.INTEGER,
allowNull: false,
length: 100,
//FK
references: {
model: sensorModel,
key: 'id'
}
},
id_location: {
type: DataTypes.INTEGER,
allowNull: false,
length: 100,
//FK
references: {
model: locationModel,
key: 'id'
}
},
Sensordata: {
type: DataTypes.JSON,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
timestamps: true,
}
);
module.exports = { sensorDataModel };

View File

@ -0,0 +1,54 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { locationModel } = require("./locationModel");
//sequelize.sync();
const sensorModel = sequelize.define("sensors",
{
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
},
sensortype: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
},
added_by: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
},
description: {
type: DataTypes.STRING,
allowNull: true,
length: 100,
},
location: {
type: DataTypes.INTEGER,
allowNull: true,
length: 100,
//one to many relationship
references: {
model: locationModel,
key: 'id'
}
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
timestamps: true,
}
);
module.exports = { sensorModel };

View File

@ -1,40 +0,0 @@
'use strict';
const { Sequelize, DataTypes } = require('sequelize');
const { sequelize } = require("../mySql.js");
const userModel = sequelize.define('users', {
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
username: {
type: DataTypes.STRING,
allowNull: true,
length: 50
},
email: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
password: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
lastLogin: {
type: DataTypes.timestamps,
allowNull: true,
}
},{
timestamps: false, // Disable automatic timestamps
});
module.exports = { userModel }

View File

@ -1,20 +1,11 @@
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");
async function getLocation() {
try {
sequelize.sync();
const location = await locationModel.findAll({
attributes: [
"id",
"name",
"added_by",
"description",
"createdAt",
"updatedAt",
],
});
const location = await locationModel.findAll();
return location;
} catch (error) {
console.error(error);
@ -24,7 +15,6 @@ async function getLocation() {
async function addLocation(name, added_by, description) {
try {
sequelize.sync();
const location = await locationModel.create({
name: name,
added_by: added_by,
@ -38,7 +28,6 @@ async function addLocation(name, added_by, description) {
async function updateLocation(id, name, added_by, description) {
try {
sequelize.sync();
//update by id
const location = await locationModel.update(
{
@ -60,7 +49,6 @@ async function updateLocation(id, name, added_by, description) {
async function deleteLocation(id) {
try {
sequelize.sync();
//delete by id
const location = await locationModel.destroy({
where: {
@ -75,8 +63,6 @@ async function deleteLocation(id) {
async function getLocationById(id) {
try {
sequelize.sync();
//delete by id
const location = await locationModel.findAll({
where: {
id: id,
@ -89,9 +75,85 @@ async function getLocationById(id) {
}
}
async function getSensor() {
try {
const sensor = await sensorModel.findAll();
return sensor;
} catch (error) {
console.error(error);
return null;
}
}
async function addSensor(sensortype, added_by, description, location) {
try {
const sensor = await sensorModel.create({
sensortype: sensortype,
added_by: added_by,
description: description,
location: location,
});
} catch (error) {
console.error(error);
return null;
}
}
async function updateSensor(id, sensortype, added_by, description, location) {
try {
//update by id
const sensor = await sensorModel.update(
{
sensortype: sensortype,
added_by: added_by,
description: description,
location: location,
},
{
where: {
id: id,
},
}
);
} catch (error) {
console.error(error);
return null;
}
}
async function deleteSensor(id) {
try {
//delete by id
const sensor = await sensorModel.destroy({
where: {
id: id,
},
});
} catch (error) {
console.error(error);
return null;
}
}
async function getSensorById(id) {
try {
const sensor = await sensorModel.findAll({
where: {
id: id,
},
});
return sensor;
} catch (error) {
console.error(error);
return null;
}
}
async function getallData() {
try {
sequelize.sync();
const allData = await IoTModel.findAll({
attributes: [
"id",
@ -118,7 +180,6 @@ async function getallData() {
async function getLatestData() {
try {
sequelize.sync();
const latestData = await IoTModel.findAll({
limit: 1,
order: [["createdAt", "DESC"]],
@ -138,4 +199,9 @@ module.exports = {
updateLocation,
deleteLocation,
getLocationById,
getSensor,
addSensor,
updateSensor,
deleteSensor,
getSensorById,
};

View File

@ -3,32 +3,55 @@
2) enforce best practice for api routes
*/
const express = require("express");
const helmet = require('helmet')
const helmet = require("helmet");
const app = express();
app.use(helmet())
app.use(helmet());
const port = 80;
//disable x-powered-by header for security reasons
app.disable('x-powered-by')
app.disable("x-powered-by");
app.use(express.json());
app.set('json spaces', 2);
app.set("json spaces", 2);
const { APIlogger } = require('../middleware/ApiLogger.js');
const { json } = require("body-parser");
//const { APIlogger } = require('../middleware/ApiLogger.js');
//middleware logic
//app.use('/api/v1', require('../middleware/ApiKey.js'));
app.use('/api/v0', APIlogger );
//app.use('/api/v0', require('../middleware/ApiKey.js'));
//app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
//route logic
app.use('/api/v0', require('../routes/api_route.js'));
//route logic
app.use("/api/v0", require("../routes/api_route.js"));
// Catch 404 and forward to error handler. If none of the above routes are
// used, this is what will be called.
app.use(function (req, res, next) {
var err = new Error("Not Found");
err.message = "Page not found";
err.status = 404;
next(err);
});
// Error handler. This is where `next()` will go on error
app.use(function (err, req, res, next) {
console.error(err.status || res.status, err.name, req.method, req.url);
if (![404].includes(err.status || res.status)) {
console.error(err.message);
console.error(err.stack);
console.error("=========================================");
}
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, () => {
console.log(`app listening on port ${port}`);
});
module.exports = { app };

View File

@ -0,0 +1,71 @@
const { sequelize } = require("../../Database/mySql.js");
const { locationModel } = require("../../Database/model/locationModel.js");
const {
addLocation,
getLocation,
getLocationById,
updateLocation,
deleteLocation,
} = require("../functions/APIDatabase.js");
const express = require("express");
const router = express.Router();
//get location
router.get("/", async (req, res, next) => {
try {
const location = await getLocation();
res.json(location);
} catch (error) {
console.error(error);
next(error);
}
});
//add location
router.post("/new", async (req, res, next) => {
try {
const { name, added_by, description } = req.body;
await addLocation(name, added_by, description);
} catch (error) {
console.error(error);
next(error);
}
});
//update location
router.put("/update", async (req, res, next) => {
try {
const { id, name, added_by, description } = req.body;
await updateLocation(id, name, added_by, description);
} catch (error) {
console.error(error);
next(error);
}
});
//delete location
router.delete("/delete", async (req, res, next) => {
try {
const { id } = req.body;
await deleteLocation(id);
} catch (error) {
console.error(error);
next(error);
}
});
//get location by id
router.get("/:id", async (req, res, next) => {
try {
//get params
const { id } = req.params;
const location = await getLocationById(id);
res.json(location);
} catch (error) {
console.error(error);
next(error);
}
});
module.exports = router;

View File

@ -0,0 +1,64 @@
const { sequelize } = require("../../Database/mySql.js");
const { sensorModel } = require("../../Database/model/sensorModel.js");
const {
getSensor,
addSensor,
updateSensor,
deleteSensor,
getSensorById
} = require("../functions/APIDatabase.js");
const express = require("express");
const router = express.Router();
router.get("/", async (req, res, next) => {
try {
const sensor = await getSensor();
res.json(sensor);
} catch (error) {
console.error(error);
next(error);
}
});
router.post("/new", async (req, res, next) => {
try {
const { sensortype, added_by, description, location } = req.body;
await addSensor(sensortype, added_by, description, location);
} catch (error) {
console.error(error);
next(error);
}
});
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);
} catch (error) {
console.error(error);
next(error);
}
});
router.delete("/delete", async (req, res, next) => {
try {
const { id } = req.body;
await deleteSensor(id);
} catch (error) {
console.error(error);
next(error);
}
});
router.get("/:id", async (req, res, next) => {
try {
const sensor = await getSensorById(req.params.id);
res.json(sensor);
} catch (error) {
console.error(error);
next(error);
}
});
module.exports = router;

View File

@ -1,19 +0,0 @@
const { sequelize } = require("../../Database/mySql.js");
const { locatioModel } = require("../../Database/model/locationModel.js");
const { addLocation } = require("../functions/APIDatabase.js");
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
try {
const {name , added_by , description } = req.body;
await addLocation(name, added_by, description);
} catch (error) {
console.error(error);
}
});
// Export the router
module.exports = router;

View File

@ -18,11 +18,10 @@ module.exports = router;
const router = require('express').Router();
//location route
router.use('/location', require('./getLocation'));
router.use('/add-location', require('./addLocation'));
router.use('/update-location', require('./updateLocation'));
router.use('/delete-location', require('./deleteLocation'));
router.use('/', require('./getLocationId'));
router.use('/location', require('./Location'));
//sensor route
router.use('/sensor', require('./Sensor'))
@ -31,4 +30,7 @@ router.use('/test' , require('./test'));
router.use('/latest-data', require('./latest-data'));
router.use('/:month', require('./monthlyData'));
module.exports = router;

View File

@ -1,19 +0,0 @@
const { sequelize } = require("../../Database/mySql.js");
const { locatioModel } = require("../../Database/model/locationModel.js");
const { deleteLocation } = require("../functions/APIDatabase.js");
const express = require('express');
const router = express.Router();
router.delete('/', async (req, res) => {
try {
const {id} = req.body;
await deleteLocation(id);
} catch (error) {
console.error(error);
}
});
// Export the router
module.exports = router;

View File

@ -1,18 +0,0 @@
const { sequelize } = require("../../Database/mySql.js");
const { locatioModel } = require("../../Database/model/locationModel.js");
const { getLocation } = require("../functions/APIDatabase.js");
const express = require('express');
const router = express.Router();
router.get('/', async (req, res) => {
try {
const location = await getLocation();
res.json(location);
} catch (error) {
console.error(error);
}
});
// Export the router
module.exports = router;

View File

@ -1,20 +0,0 @@
const { sequelize } = require("../../Database/mySql.js");
const { locatioModel } = require("../../Database/model/locationModel.js");
const { getLocationById } = require("../functions/APIDatabase.js");
const express = require('express');
const router = express.Router();
router.get('/:id', async (req, res) => {
try {
//get params
const { id } = req.params;
const location = await getLocationById(id);
res.json(location);
} catch (error) {
console.error(error);
}
});
// Export the router
module.exports = router;

View File

@ -5,23 +5,6 @@ const { getallData } = require("../functions/APIDatabase.js");
const express = require('express');
const router = express.Router();
/*
async function getallData() {
try {
sequelize.sync();
const allData = await IoTModel.findAll({
attributes: ['id', 'psiData', 'humidityData', 'o3Data', 'no2Data', 'so2Data', 'coData', 'temperatureData', 'windspeedData', 'currentTime', 'regionData' , 'createdAt' , 'updatedAt'],
});
return allData;
}
catch(error) {
console.error(error);
return null;
}
}
*/
router.get('/', async (req, res) => {
try {
const data = await getallData();

View File

@ -1,19 +0,0 @@
const { sequelize } = require("../../Database/mySql.js");
const { locatioModel } = require("../../Database/model/locationModel.js");
const { updateLocation } = require("../functions/APIDatabase.js");
const express = require('express');
const router = express.Router();
router.put('/', async (req, res) => {
try {
const {id , name , added_by , description } = req.body;
await updateLocation(id , name, added_by , description);
} catch (error) {
console.error(error);
}
});
// Export the router
module.exports = router;

27
api.MD
View File

@ -1,14 +1,33 @@
//location
//get all
curl localhost/api/v0/location
//get id
curl localhost/api/v0/1
http://localhost/api/v0/location/3
//post
curl localhost/api/v0/add-location -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"}'
//put
curl localhost/api/v0/update-location -X PUT -H "Content-Type: application/json" -d '{"id": "1" , "name": "test", "added_by": "noot", "description": "test123"}'
curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json" -d '{"id": "2" , "name": "test", "added_by": "noot", "description": "test12344444444"}'
//delete
curl localhost/api/v0/delete-location -X DELETE -H "Content-Type: application/json" -d '{"id": "1" }'
curl localhost/api/v0/location/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "6" }'
//sensor
//GET all
curl localhost/api/v0/sensor
//get id
curl http://localhost/api/v0/sensor/3
//POST
curl localhost/api/v0/sensor/new -H "Content-Type: application/json" -X POST -d '{"sensortype": "test", "added_by": "noot" , "description": "test" , "location": "2"}'
//put
curl localhost/api/v0/sensor/update -H "Content-Type: application/json" -X PUT -d '{"id": "2" ,"sensortype": "test", "added_by": "noot" , "description": "test123333333" , "location": "3" }'
//delete
curl localhost/api/v0/sensor/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "2" }'
//sensor data