seedroute and vlidate

This commit is contained in:
newtbot
2024-01-03 03:51:15 +08:00
parent 088302fa5b
commit 90754c1792
10 changed files with 113 additions and 70 deletions

View File

@ -6,29 +6,20 @@ const { sensorDataModel } = require("../../Database/model/sensorDataModel.js");
async function getLocation() {
try {
const location = await locationModel.findAll();
return location;
} catch (error) {
console.error(error);
}
}
async function addLocation(name, added_by, description) {
try {
const location = await locationModel.create({
name: name,
added_by: added_by,
description: description,
});
} catch (error) {
console.error(error);
}
}
async function updateLocation(id, name, added_by, description) {
try {
//update by id
const location = await locationModel.update(
{
name: name,
@ -41,35 +32,24 @@ async function updateLocation(id, name, added_by, description) {
},
}
);
} catch (error) {
console.error(error);
}
}
async function deleteLocation(id) {
try {
//delete by id
const location = await locationModel.destroy({
where: {
id: id,
},
});
} catch (error) {
console.error(error);
}
}
async function getLocationById(id) {
try {
const location = await locationModel.findAll({
where: {
id: id,
},
});
return location;
} catch (error) {
console.error(error);
}
}
async function getSensor() {

View File

View File

@ -15,15 +15,18 @@ 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 ( 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"));
//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
// used, this is what will be called.
app.use(function (req, res, next) {
@ -34,29 +37,33 @@ app.use(function (req, res, next) {
});
// Error handler. This is where `next()` will go on error
app.use(function (err, req, res, next) {
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("=========================================");
if(![ 404].includes(err.status || res.status)){
console.error(err.message);
console.error(err.stack);
console.error('=========================================');
}
//validation error
if (err.name === "SequelizeValidationError") {
err.status = 400;
err.message = "Validation 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
}
}
}
else if (err.name === "SequelizeUniqueConstraintError")
{
console.log("this is my custom error!" + err);
}
res.status(err.status || 500);
console.log(keyErrors);
res.json({
name: err.name,
message: err.message,
name: err.name,
message: err.message,
keyErrors,
});
});
});
app.listen(port, () => {
console.log(`app listening on port ${port}`);
});

View File

@ -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);

View 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;

View File

View File

@ -0,0 +1,11 @@
'use strict';
const router = require('express').Router();
//location route
router.use('/seed', require('./SeedLocationAndSensor'));
module.exports = router;