location route

NO validation
no middleware to auth
This commit is contained in:
newtbot 2023-12-30 03:59:04 +08:00
parent 0aefebae75
commit 233ebafdcb
10 changed files with 305 additions and 36 deletions

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

View File

@ -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 };
module.exports = {
getallData,
getLatestData,
getLocation,
addLocation,
updateLocation,
deleteLocation,
getLocationById,
};

View File

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

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

View File

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

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

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

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

View 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
View 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" }'