199 lines
4.6 KiB
JavaScript

const DailydataModel = require('../models/dailyDataModel.js');
const DailyData = require('../data/dailyData.json');
/**
* dailyDataController.js
*
* @description :: Server-side logic for managing dailyDatas.
*/
module.exports = {
/**
* dailyDataController.list()
*/
list: function (req, res) {
DailydataModel.find(function (err, dailyDatas) {
if (err) {
return res.status(500).json({
message: 'Error when getting dailyData.',
error: err
});
}
return res.json(dailyDatas);
});
},
/**
* dailyDataController.show()
*/
show: function (req, res) {
const id = req.params.id;
DailydataModel.find({ location_id: id }, function (err, dailyData) {
if (err) {
return res.status(500).json({
message: 'Error when getting dailyData.',
error: err
});
}
if (!dailyData) {
return res.status(404).json({
message: 'No such dailyData'
});
}
return res.json(dailyData);
});
},
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(DailyData[location]);
},
showLocation: function (req, res) {
const year = req.params.year;
const month = req.params.month;
const location = req.params.location;
DailydataModel.find({
year: year,
month: month,
location_id: location
}).exec(function (err, dailyData) {
if (err) {
return res.status(500).json({
message: 'Error when getting dailyData.',
error: err
});
}
if (!dailyData) {
return res.status(404).json({
message: 'No such dailyData'
});
}
return res.json(dailyData);
});
},
showDay: function (req, res) {
const year = req.params.year;
const month = req.params.month;
const day = req.params.day;
DailydataModel.find({
year: year,
month: month,
day: day
}).exec(function (err, dailyData) {
if (err) {
return res.status(500).json({
message: 'Error when getting dailyData.',
error: err
});
}
if (!dailyData) {
return res.status(404).json({
message: 'No such dailyData'
});
}
return res.json(dailyData);
});
},
/**
* dailyDataController.create()
*/
create: function (req, res) {
const dailyData = new DailydataModel({
location_id: req.body.location_id,
date: req.body.date,
car_count: req.body.car_count,
year: req.body.year,
month: req.body.month,
day: req.body.day
});
dailyData.save(function (err, dailyData) {
if (err) {
return res.status(500).json({
message: 'Error when creating dailyData',
error: err
});
}
return res.status(201).json(dailyData);
});
},
/**
* dailyDataController.update()
*/
update: function (req, res) {
const id = req.params.id;
DailydataModel.findOne({ _id: id }, function (err, dailyData) {
if (err) {
return res.status(500).json({
message: 'Error when getting dailyData',
error: err
});
}
if (!dailyData) {
return res.status(404).json({
message: 'No such dailyData'
});
}
dailyData.location_id = req.body.location_id ? req.body.location_id : dailyData.location_id;
dailyData.date = req.body.date ? req.body.date : dailyData.date;
dailyData.car_count = req.body.car_count ? req.body.car_count : dailyData.car_count;
dailyData.year = req.body.year ? req.body.year : dailyData.year;
dailyData.month = req.body.month ? req.body.month : dailyData.month;
dailyData.day = req.body.day ? req.body.day : dailyData.day;
dailyData.save(function (err, dailyData) {
if (err) {
return res.status(500).json({
message: 'Error when updating dailyData.',
error: err
});
}
return res.json(dailyData);
});
});
},
/**
* dailyDataController.remove()
*/
remove: function (req, res) {
const id = req.params.id;
DailydataModel.findByIdAndRemove(id, function (err, dailyData) {
if (err) {
return res.status(500).json({
message: 'Error when deleting the dailyData.',
error: err
});
}
return res.status(204).json();
});
}
};