238 lines
5.3 KiB
JavaScript
238 lines
5.3 KiB
JavaScript
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);
|
|
});
|
|
}
|
|
};
|