school/projektna_naloga/web_server/controllers/rawCameraDataController.js

131 lines
3.4 KiB
JavaScript

const RawcameradataModel = require('../models/rawCameraDataModel.js');
/**
* rawCameraDataController.js
*
* @description :: Server-side logic for managing rawCameraDatas.
*/
module.exports = {
/**
* rawCameraDataController.list()
*/
list: function (req, res) {
RawcameradataModel.find(function (err, rawCameraDatas) {
if (err) {
return res.status(500).json({
message: 'Error when getting rawCameraData.',
error: err
});
}
return res.json(rawCameraDatas);
});
},
/**
* rawCameraDataController.show()
*/
show: function (req, res) {
const id = req.params.id;
RawcameradataModel.findOne({ _id: id }, function (err, rawCameraData) {
if (err) {
return res.status(500).json({
message: 'Error when getting rawCameraData.',
error: err
});
}
if (!rawCameraData) {
return res.status(404).json({
message: 'No such rawCameraData'
});
}
return res.json(rawCameraData);
});
},
/**
* rawCameraDataController.create()
*/
create: function (req, res) {
const date1 = new Date(req.body.date);
const rawCameraData = new RawcameradataModel({
location_id: req.body.location_id,
camera_id: req.body.camera_id,
date: date1,
car_count: req.body.car_count,
hour: req.body.hour
});
rawCameraData.save(function (err, rawCameraData) {
if (err) {
return res.status(500).json({
message: 'Error when creating rawCameraData',
error: err
});
}
return res.status(201).json(rawCameraData);
});
},
/**
* rawCameraDataController.update()
*/
update: function (req, res) {
const id = req.params.id;
RawcameradataModel.findOne({ _id: id }, function (err, rawCameraData) {
if (err) {
return res.status(500).json({
message: 'Error when getting rawCameraData',
error: err
});
}
if (!rawCameraData) {
return res.status(404).json({
message: 'No such rawCameraData'
});
}
rawCameraData.location_id = req.body.location_id ? req.body.location_id : rawCameraData.location_id;
rawCameraData.camera_id = req.body.camera_id ? req.body.camera_id : rawCameraData.camera_id;
rawCameraData.date = req.body.date ? req.body.date : rawCameraData.date;
rawCameraData.car_count = req.body.car_count ? req.body.car_count : rawCameraData.car_count;
rawCameraData.hour = req.body.hour ? req.body.hour : rawCameraData.hour;
rawCameraData.save(function (err, rawCameraData) {
if (err) {
return res.status(500).json({
message: 'Error when updating rawCameraData.',
error: err
});
}
return res.json(rawCameraData);
});
});
},
/**
* rawCameraDataController.remove()
*/
remove: function (req, res) {
const id = req.params.id;
RawcameradataModel.deleteMany({ hour: id }, function (err, rawCameraData) {
if (err) {
return res.status(500).json({
message: 'Error when deleting the rawCameraData.',
error: err
});
}
return res.status(204).json();
});
}
};