consolidate all repos to one for archive
This commit is contained in:
254
projektna_naloga/web_server/controllers/hourlyDataController.js
Normal file
254
projektna_naloga/web_server/controllers/hourlyDataController.js
Normal file
@@ -0,0 +1,254 @@
|
||||
const HourlydataModel = require('../models/hourlyDataModel.js');
|
||||
const MapData = require('../data/mapData.json');
|
||||
const HourlyData = require('../data/hourlyData.json');
|
||||
/**
|
||||
* hourlyDataController.js
|
||||
*
|
||||
* @description :: Server-side logic for managing hourlyDatas.
|
||||
*/
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* hourlyDataController.list()
|
||||
*/
|
||||
list: function (req, res) {
|
||||
HourlydataModel.find(function (err, hourlyDatas) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting hourlyData.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(hourlyDatas);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* hourlyDataController.show()
|
||||
*/
|
||||
// DOTO: change this to get last 24 hours for a location
|
||||
show: function (req, res) {
|
||||
const location = req.params.location;
|
||||
|
||||
HourlydataModel.find({ location_id: location }, function (err, hourlyData) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting hourlyData.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!hourlyData) {
|
||||
return res.status(404).json({
|
||||
message: 'No such hourlyData'
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(hourlyData);
|
||||
});
|
||||
},
|
||||
//location id max 248
|
||||
showLocationTest: function (req, res) {
|
||||
let location = req.params.location;
|
||||
location = Number.parseInt(location);
|
||||
|
||||
if (isNaN(location)) return res.json([]);
|
||||
|
||||
if (location > 248) location = 248;
|
||||
else if (location < 0) location = 0;
|
||||
|
||||
return res.json(HourlyData[location]);
|
||||
},
|
||||
|
||||
showLocation: function (req, res) {
|
||||
const location = req.params.location;
|
||||
const year = req.params.year;
|
||||
const month = req.params.month;
|
||||
const day = req.params.day;
|
||||
|
||||
HourlydataModel.find({
|
||||
location_id: location,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day
|
||||
}).sort({ hour: 1 }).exec(function (err, hourlyData) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting hourlyData.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!hourlyData) {
|
||||
return res.status(404).json({
|
||||
message: 'No such hourlyData'
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(hourlyData);
|
||||
});
|
||||
},
|
||||
|
||||
showHourTest: function (req, res) {
|
||||
return res.json(MapData);
|
||||
},
|
||||
|
||||
showHourCsvTest: function (req, res) {
|
||||
let csv = "";
|
||||
MapData.forEach(row =>
|
||||
csv += row.location_id + "," + row.car_count + "\n"
|
||||
);
|
||||
res.setHeader('Content-disposition', 'attachment; filename=hourlyData.csv');
|
||||
res.set('Content-Type', 'text/csv');
|
||||
return res.send(csv);
|
||||
},
|
||||
|
||||
showHour: function (req, res) {
|
||||
const year = req.params.year;
|
||||
const month = req.params.month;
|
||||
const day = req.params.day;
|
||||
const hour = req.params.hour;
|
||||
|
||||
HourlydataModel.find({
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour
|
||||
}).sort({ location_id: 1 }).exec(function (err, hourlyData) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting hourlyData.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!hourlyData) {
|
||||
return res.status(404).json({
|
||||
message: 'No such hourlyData'
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(hourlyData);
|
||||
});
|
||||
},
|
||||
|
||||
showHourCsv: function (req, res) {
|
||||
const year = req.params.year;
|
||||
const month = req.params.month;
|
||||
const day = req.params.day;
|
||||
const hour = req.params.hour;
|
||||
|
||||
HourlydataModel.find({
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour
|
||||
}).sort({ location_id: 1 }).exec(function (err, hourlyData) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting hourlyData.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!hourlyData) {
|
||||
return res.status(404).json({
|
||||
message: 'No such hourlyData'
|
||||
});
|
||||
}
|
||||
|
||||
let csv = "";
|
||||
hourlyData.forEach(function (row) {
|
||||
csv += row.location_id + "," + row.car_count + "\n";
|
||||
});
|
||||
res.setHeader('Content-disposition', 'attachment; filename=hourlyData.csv');
|
||||
res.set('Content-Type', 'text/csv');
|
||||
return res.send(csv);
|
||||
});
|
||||
},
|
||||
/**
|
||||
* hourlyDataController.create()
|
||||
*/
|
||||
create: function (req, res) {
|
||||
const hourlyData = new HourlydataModel({
|
||||
location_id: req.body.location_id,
|
||||
date: req.body.date,
|
||||
car_count: req.body.car_count,
|
||||
hour: req.body.hour,
|
||||
day: req.body.day,
|
||||
month: req.body.month,
|
||||
year: req.body.year
|
||||
});
|
||||
|
||||
hourlyData.save(function (err, hourlyData) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when creating hourlyData',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(201).json(hourlyData);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* hourlyDataController.update()
|
||||
*/
|
||||
update: function (req, res) {
|
||||
const id = req.params.id;
|
||||
|
||||
HourlydataModel.findOne({ _id: id }, function (err, hourlyData) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting hourlyData',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!hourlyData) {
|
||||
return res.status(404).json({
|
||||
message: 'No such hourlyData'
|
||||
});
|
||||
}
|
||||
|
||||
hourlyData.location_id = req.body.location_id ? req.body.location_id : hourlyData.location_id;
|
||||
hourlyData.date = req.body.date ? req.body.date : hourlyData.date;
|
||||
hourlyData.car_count = req.body.car_count ? req.body.car_count : hourlyData.car_count;
|
||||
hourlyData.hour = req.body.hour ? req.body.hour : hourlyData.hour;
|
||||
hourlyData.day = req.body.day ? req.body.day : hourlyData.day;
|
||||
hourlyData.month = req.body.month ? req.body.month : hourlyData.month;
|
||||
hourlyData.year = req.body.year ? req.body.year : hourlyData.year;
|
||||
|
||||
hourlyData.save(function (err, hourlyData) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when updating hourlyData.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(hourlyData);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* hourlyDataController.remove()
|
||||
*/
|
||||
remove: function (req, res) {
|
||||
const id = req.params.id;
|
||||
|
||||
HourlydataModel.findByIdAndRemove(id, function (err, hourlyData) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when deleting the hourlyData.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(204).json();
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user