location route
NO validation no middleware to auth
This commit is contained in:
parent
0aefebae75
commit
233ebafdcb
44
Database/model/locationModel.js
Normal file
44
Database/model/locationModel.js
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
"use strict";
|
||||||
|
const { Sequelize, DataTypes } = require("sequelize");
|
||||||
|
const { sequelize } = require("../mySQL");
|
||||||
|
|
||||||
|
sequelize.sync();
|
||||||
|
const locationModel = sequelize.define(
|
||||||
|
"location",
|
||||||
|
{
|
||||||
|
id: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: true,
|
||||||
|
primaryKey: true,
|
||||||
|
autoIncrement: true,
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
length: 10,
|
||||||
|
},
|
||||||
|
added_by: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
length: 10,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: true,
|
||||||
|
length: 100,
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
type: DataTypes.DATE,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
updatedAt: {
|
||||||
|
type: DataTypes.DATE,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
module.exports = { locationModel };
|
@ -1,39 +1,141 @@
|
|||||||
const { sequelize } = require("../../Database/mySql.js");
|
const { sequelize } = require("../../Database/mySql.js");
|
||||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||||
|
const { locationModel } = require("../../Database/model/locationModel.js");
|
||||||
|
|
||||||
|
async function getLocation() {
|
||||||
|
try {
|
||||||
|
sequelize.sync();
|
||||||
|
const location = await locationModel.findAll({
|
||||||
|
attributes: [
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
"added_by",
|
||||||
|
"description",
|
||||||
|
"createdAt",
|
||||||
|
"updatedAt",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
return location;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function addLocation(name, added_by, description) {
|
||||||
|
try {
|
||||||
|
sequelize.sync();
|
||||||
|
const location = await locationModel.create({
|
||||||
|
name: name,
|
||||||
|
added_by: added_by,
|
||||||
|
description: description,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateLocation(id, name, added_by, description) {
|
||||||
|
try {
|
||||||
|
sequelize.sync();
|
||||||
|
//update by id
|
||||||
|
const location = await locationModel.update(
|
||||||
|
{
|
||||||
|
name: name,
|
||||||
|
added_by: added_by,
|
||||||
|
description: description,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteLocation(id) {
|
||||||
|
try {
|
||||||
|
sequelize.sync();
|
||||||
|
//delete by id
|
||||||
|
const location = await locationModel.destroy({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getLocationById(id) {
|
||||||
|
try {
|
||||||
|
sequelize.sync();
|
||||||
|
//delete by id
|
||||||
|
const location = await locationModel.findAll({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return location;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function getallData() {
|
async function getallData() {
|
||||||
try {
|
try {
|
||||||
sequelize.sync();
|
sequelize.sync();
|
||||||
const allData = await IoTModel.findAll({
|
const allData = await IoTModel.findAll({
|
||||||
attributes: ['id', 'psiData', 'humidityData', 'o3Data', 'no2Data', 'so2Data', 'coData', 'temperatureData', 'windspeedData', 'currentTime', 'regionData' , 'createdAt' , 'updatedAt'],
|
attributes: [
|
||||||
});
|
"id",
|
||||||
return allData;
|
"psiData",
|
||||||
}
|
"humidityData",
|
||||||
catch(error) {
|
"o3Data",
|
||||||
console.error(error);
|
"no2Data",
|
||||||
return null;
|
"so2Data",
|
||||||
|
"coData",
|
||||||
}
|
"temperatureData",
|
||||||
}
|
"windspeedData",
|
||||||
|
"currentTime",
|
||||||
|
"regionData",
|
||||||
|
"createdAt",
|
||||||
|
"updatedAt",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
return allData;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getLatestData() {
|
||||||
|
try {
|
||||||
|
sequelize.sync();
|
||||||
|
const latestData = await IoTModel.findAll({
|
||||||
|
limit: 1,
|
||||||
|
order: [["createdAt", "DESC"]],
|
||||||
|
});
|
||||||
|
return latestData;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function getLatestData() {
|
module.exports = {
|
||||||
try {
|
getallData,
|
||||||
sequelize.sync();
|
getLatestData,
|
||||||
const latestData = await IoTModel.findAll({
|
getLocation,
|
||||||
limit: 1,
|
addLocation,
|
||||||
order: [['createdAt', 'DESC']]
|
updateLocation,
|
||||||
});
|
deleteLocation,
|
||||||
return latestData;
|
getLocationById,
|
||||||
}
|
};
|
||||||
catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = { getallData , getLatestData };
|
|
||||||
|
@ -12,14 +12,18 @@ const port = 80;
|
|||||||
//disable x-powered-by header for security reasons
|
//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);
|
||||||
|
|
||||||
const { APIlogger } = require('../middleware/ApiLogger.js');
|
const { APIlogger } = require('../middleware/ApiLogger.js');
|
||||||
|
const { json } = require("body-parser");
|
||||||
|
|
||||||
//middleware logic
|
//middleware logic
|
||||||
//app.use('/api/v1', require('../middleware/ApiKey.js'));
|
//app.use('/api/v1', require('../middleware/ApiKey.js'));
|
||||||
app.use('/api/', APIlogger );
|
app.use('/api/v0', APIlogger );
|
||||||
|
|
||||||
//route logic
|
//route logic
|
||||||
app.use('/api/', require('../routes/api_route.js'));
|
app.use('/api/v0', require('../routes/api_route.js'));
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`app listening on port ${port}`);
|
console.log(`app listening on port ${port}`);
|
||||||
|
19
Web-Server/routes/addLocation.js
Normal file
19
Web-Server/routes/addLocation.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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;
|
@ -17,6 +17,16 @@ module.exports = router;
|
|||||||
'use strict';
|
'use strict';
|
||||||
const router = require('express').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('/test' , require('./test'));
|
router.use('/test' , require('./test'));
|
||||||
router.use('/latest-data', require('./latest-data'));
|
router.use('/latest-data', require('./latest-data'));
|
||||||
router.use('/:month', require('./monthlyData'));
|
router.use('/:month', require('./monthlyData'));
|
||||||
|
19
Web-Server/routes/deleteLocation.js
Normal file
19
Web-Server/routes/deleteLocation.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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;
|
18
Web-Server/routes/getLocation.js
Normal file
18
Web-Server/routes/getLocation.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
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;
|
20
Web-Server/routes/getLocationId.js
Normal file
20
Web-Server/routes/getLocationId.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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;
|
19
Web-Server/routes/updateLocation.js
Normal file
19
Web-Server/routes/updateLocation.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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;
|
14
api.MD
Normal file
14
api.MD
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
//get all
|
||||||
|
curl localhost/api/v0/location
|
||||||
|
|
||||||
|
//get id
|
||||||
|
curl localhost/api/v0/1
|
||||||
|
|
||||||
|
//post
|
||||||
|
curl localhost/api/v0/add-location -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"}'
|
||||||
|
|
||||||
|
//delete
|
||||||
|
curl localhost/api/v0/delete-location -X DELETE -H "Content-Type: application/json" -d '{"id": "1" }'
|
Loading…
x
Reference in New Issue
Block a user