diff --git a/Database/model/locationModel.js b/Database/model/locationModel.js new file mode 100644 index 0000000..4997602 --- /dev/null +++ b/Database/model/locationModel.js @@ -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 }; diff --git a/Web-Server/functions/APIDatabase.js b/Web-Server/functions/APIDatabase.js index 0e582f8..f5f5242 100644 --- a/Web-Server/functions/APIDatabase.js +++ b/Web-Server/functions/APIDatabase.js @@ -1,39 +1,141 @@ -const { sequelize } = require("../../Database/mySql.js"); -const { IoTModel } = require("../../Database/model/IoTModel.js"); +const { sequelize } = require("../../Database/mySql.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() { - 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; - - } - } + 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; + } +} +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() { - try { - sequelize.sync(); - const latestData = await IoTModel.findAll({ - limit: 1, - order: [['createdAt', 'DESC']] - }); - return latestData; - } - catch (error) { - console.error(error); - return null; - } - } - - - - -module.exports = { getallData , getLatestData }; \ No newline at end of file +module.exports = { + getallData, + getLatestData, + getLocation, + addLocation, + updateLocation, + deleteLocation, + getLocationById, +}; diff --git a/Web-Server/modules/express.js b/Web-Server/modules/express.js index 4124fdd..bb91c2c 100644 --- a/Web-Server/modules/express.js +++ b/Web-Server/modules/express.js @@ -12,14 +12,18 @@ const port = 80; //disable x-powered-by header for security reasons app.disable('x-powered-by') +app.use(express.json()); +app.set('json spaces', 2); + const { APIlogger } = require('../middleware/ApiLogger.js'); +const { json } = require("body-parser"); //middleware logic //app.use('/api/v1', require('../middleware/ApiKey.js')); -app.use('/api/', APIlogger ); +app.use('/api/v0', APIlogger ); //route logic -app.use('/api/', require('../routes/api_route.js')); +app.use('/api/v0', require('../routes/api_route.js')); app.listen(port, () => { console.log(`app listening on port ${port}`); diff --git a/Web-Server/routes/addLocation.js b/Web-Server/routes/addLocation.js new file mode 100644 index 0000000..12b1170 --- /dev/null +++ b/Web-Server/routes/addLocation.js @@ -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; \ No newline at end of file diff --git a/Web-Server/routes/api_route.js b/Web-Server/routes/api_route.js index 2bcdb22..2289204 100644 --- a/Web-Server/routes/api_route.js +++ b/Web-Server/routes/api_route.js @@ -17,6 +17,16 @@ module.exports = router; 'use strict'; 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('/latest-data', require('./latest-data')); router.use('/:month', require('./monthlyData')); diff --git a/Web-Server/routes/deleteLocation.js b/Web-Server/routes/deleteLocation.js new file mode 100644 index 0000000..b68a3af --- /dev/null +++ b/Web-Server/routes/deleteLocation.js @@ -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; \ No newline at end of file diff --git a/Web-Server/routes/getLocation.js b/Web-Server/routes/getLocation.js new file mode 100644 index 0000000..38ae348 --- /dev/null +++ b/Web-Server/routes/getLocation.js @@ -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; \ No newline at end of file diff --git a/Web-Server/routes/getLocationId.js b/Web-Server/routes/getLocationId.js new file mode 100644 index 0000000..88d6730 --- /dev/null +++ b/Web-Server/routes/getLocationId.js @@ -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; \ No newline at end of file diff --git a/Web-Server/routes/updateLocation.js b/Web-Server/routes/updateLocation.js new file mode 100644 index 0000000..ff3829a --- /dev/null +++ b/Web-Server/routes/updateLocation.js @@ -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; \ No newline at end of file diff --git a/api.MD b/api.MD new file mode 100644 index 0000000..3bd6f87 --- /dev/null +++ b/api.MD @@ -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" }'