consolidate all repos to one for archive
This commit is contained in:
198
projektna_naloga/web_server/controllers/dailyDataController.js
Normal file
198
projektna_naloga/web_server/controllers/dailyDataController.js
Normal file
@@ -0,0 +1,198 @@
|
||||
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();
|
||||
});
|
||||
}
|
||||
};
|
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();
|
||||
});
|
||||
}
|
||||
};
|
@@ -0,0 +1,38 @@
|
||||
let Locations = require('../data/locations.json');
|
||||
/**
|
||||
* locationController.js
|
||||
*
|
||||
* @description :: Server-side logic for managing locations.
|
||||
*/
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* locationController.list()
|
||||
*/
|
||||
list: function (req, res) {
|
||||
return res.json(Locations);
|
||||
},
|
||||
|
||||
csv: function (req, res) {
|
||||
let csvData = "";
|
||||
for (let i = 0; i < Locations.length; i++) {
|
||||
csvData += Locations[i].location_id + "," + Locations[i].name + "," + Locations[i].cord_N + "," + Locations[i].cord_E + "," + Locations[i].img_urls[0] + "\n";
|
||||
}
|
||||
|
||||
res.setHeader('Content-disposition', 'attachment; filename=locations.csv');
|
||||
res.set('Content-Type', 'text/csv');
|
||||
|
||||
res.status(200).send(csvData);
|
||||
},
|
||||
|
||||
/**
|
||||
* locationController.show()
|
||||
*/
|
||||
show: function (req, res) {
|
||||
const id = req.params.id;
|
||||
|
||||
const find = Locations.find(value => value.location_id == id)
|
||||
|
||||
return res.json(find);
|
||||
},
|
||||
};
|
@@ -0,0 +1,130 @@
|
||||
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();
|
||||
});
|
||||
}
|
||||
};
|
237
projektna_naloga/web_server/controllers/userController.js
Normal file
237
projektna_naloga/web_server/controllers/userController.js
Normal file
@@ -0,0 +1,237 @@
|
||||
const UserModel = require('../models/userModel.js');
|
||||
/**
|
||||
* userController.js
|
||||
*
|
||||
* @description :: Server-side logic for managing users.
|
||||
*/
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* userController.list()
|
||||
*/
|
||||
list: function (req, res) {
|
||||
UserModel.find(function (err, users) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting user.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(users);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* userController.show()
|
||||
*/
|
||||
show: function (req, res) {
|
||||
const id = req.params.id;
|
||||
|
||||
UserModel.findOne({ _id: id }, function (err, user) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting user.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
message: 'No such user'
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(user);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* userController.create()
|
||||
*/
|
||||
create: function (req, res) {
|
||||
const user = new UserModel({
|
||||
username: req.body.username,
|
||||
password: req.body.password,
|
||||
email: req.body.email,
|
||||
locations: []
|
||||
});
|
||||
|
||||
user.save(function (err, user) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when creating user',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(201).json(user);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* userController.update()
|
||||
*/
|
||||
update: function (req, res) {
|
||||
const id = req.params.id;
|
||||
|
||||
UserModel.findOne({ _id: id }, function (err, user) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting user',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
message: 'No such user'
|
||||
});
|
||||
}
|
||||
|
||||
user.username = req.body.username ? req.body.username : user.username;
|
||||
user.password = req.body.password ? req.body.password : user.password;
|
||||
user.email = req.body.email ? req.body.email : user.email;
|
||||
|
||||
|
||||
user.save(function (err, user) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when updating user.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(user);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* userController.remove()
|
||||
*/
|
||||
remove: function (req, res) {
|
||||
const id = req.params.id;
|
||||
|
||||
UserModel.findByIdAndRemove(id, function (err, user) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when deleting the user.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(204).json();
|
||||
});
|
||||
},
|
||||
|
||||
showRegister: function (req, res) {
|
||||
res.render('user/register');
|
||||
},
|
||||
|
||||
showLogin: function (req, res) {
|
||||
res.render('user/login');
|
||||
},
|
||||
|
||||
login: function (req, res, next) {
|
||||
UserModel.authenticate(req.body.username, req.body.password, function (err, user) {
|
||||
if (err || !user) {
|
||||
const err = new Error('Wrong username or paassword');
|
||||
err.status = 401;
|
||||
return next(err);
|
||||
}
|
||||
req.session.userId = user._id;
|
||||
|
||||
return res.json(user);
|
||||
});
|
||||
},
|
||||
|
||||
logout: function (req, res, next) {
|
||||
if (req.session) {
|
||||
req.session.destroy(function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
} else {
|
||||
//return res.redirect('/');
|
||||
return res.status(201).json({});
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
profile: function (req, res, next) {
|
||||
UserModel.findById(req.session.userId)
|
||||
.exec(function (error, user) {
|
||||
if (error) {
|
||||
return next(error);
|
||||
} else {
|
||||
if (user === null) {
|
||||
const err = new Error('Not authorized, go back!');
|
||||
err.status = 400;
|
||||
return next(err);
|
||||
} else {
|
||||
//return res.render('user/profile', user);
|
||||
return res.json(user);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
addLocation: function (req, res) {
|
||||
const locationID = req.params.id;
|
||||
|
||||
UserModel.findOne({ _id: req.session.userId }, function (err, user) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting user',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
message: 'No such user'
|
||||
});
|
||||
}
|
||||
|
||||
if (user.locations.includes(locationID)) {
|
||||
return res.status(400).json({
|
||||
message: 'Location already added'
|
||||
});
|
||||
}
|
||||
|
||||
user.locations.push(locationID);
|
||||
|
||||
user.save(function (err, user) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when updating user.',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(user);
|
||||
});
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
checkLocation: function (req, res) {
|
||||
UserModel.findOne({ _id: req.session.userId }, function (err, user) {
|
||||
if (err) {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting user',
|
||||
error: err
|
||||
});
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
message: 'No such user'
|
||||
});
|
||||
}
|
||||
|
||||
return res.json(user.locations);
|
||||
});
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user