Adding «UPDATE» User Functionality
// controllers/dashboard/author.js
class Author{
constructor(){
this.deepcopy = require('deepcopy');
this.vdict = require('../../config');
this.utility = require('../../utility');
this.usersdb = require('../../models/usersdb');
this.emailCheck = require('email-check');
this.bcrypt = require('bcryptjs');
}
getAuthor(req, res){
const self = this;
if(req.session.user){
const data = this.deepcopy(this.vdict);
data.site_title = 'ទំព័រអ្នកនិពន្ធ';
data.date = this.utility.setDate();
this.usersdb.selectUser(this.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.count = count;
if(req.params.authorId){
self.usersdb.selectUser(self.vdict.dashboardLimit, function(author){
data.edited = author;
res.render('dashboard/author', data);
}, req.params.authorId);
}else
res.render('dashboard/author', data);
});
});
}else{
res.redirect('/admin/login');
}
}
postAuthor(req, res){
const self = this;
const data = this.deepcopy(this.vdict);
data.site_title = 'ទំព័រអ្នកនិពន្ធ';
data.date = this.utility.setDate();
if(req.session.user.role == 'Admin'){
this.usersdb.checkEmail(req, function(user){
if(user){
self.usersdb.selectUser(self.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.count = count;
data.message = 'Email នេះមានគេប្រើប្រាស់ហើយ';
res.render('dashboard/author', data);
});
});
}else{
self.emailCheck(req.body.email)
.then(function (result) {
if(result){
self.usersdb.insertUser(req, function(user, err){
if(!err){
self.usersdb.selectUser(self.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.count = count;
data.message = `អ្នកនិពន្ធ ${user.username} ត្រូវបានចុះបញ្ជីរួចហើយ`;
res.render('dashboard/author', data);
});
});
}else{
self.usersdb.selectUser(self.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.message = err;
data.count = count;
res.render('dashboard/author', data);
});
});
}
});
}
})
.catch(function (err) {
self.usersdb.selectUser(self.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.count = count;
data.message = 'Email នេះមិនត្រឹមត្រូវទេ';
res.render('dashboard/author', data);
});
});
});
}
});
}else{
this.usersdb.selectUser(self.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.count = count;
data.message = 'មានតែ Administrator ទេ ដែលអាចចុះបញ្ជីអ្នកនិពន្ធបាន';
res.render('dashboard/author', data);
});
});
}
}
updateAuthor(req, res){
const self = this;
const data = this.deepcopy(this.vdict);
data.site_title = 'ទំព័រអ្នកនិពន្ធ';
data.date = this.utility.setDate();
this.usersdb.checkEmail(req, function(user){
if(user.userid !== req.params.authorId){
self.usersdb.selectUser(self.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.count = count;
data.message = 'Email នេះមានគេប្រើប្រាស់ហើយ';
res.render('dashboard/author', data);
});
});
}else{
self.emailCheck(req.body.email)
.then(function (result) {
if(result){
self.usersdb.updateUser(req, function(user){
data.author = user;
data.message = `ទិន្នន័យអ្នកនិពន្ធ ${user.username} ត្រូវបានកែតំរូវ`;
self.usersdb.selectUser(self.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.count = count;
res.render('dashboard/author', data);
});
});
});
}
})
.catch(function (err) {
self.usersdb.selectUser(self.vdict.dashboardLimit, function(authors){
data.authors = authors;
data.thumbs = self.utility.getThumbUrl(authors, 'author');
self.usersdb.countUser(function(count){
data.count = count;
data.message = 'Email នេះមិនត្រឹមត្រូវទេ';
res.render('dashboard/author', data);
});
});
});
}
});
}
}//end class
module.exports = new Author();
// models/usersdb.js
class Usersdb{
constructor(){
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const usersSchema = new mongoose.Schema({
username: {type: String, required: true},
userid: {type: String, required: true},
password: {type: String, required: true},
email: {type: String, required: true},
role: {type: String, required: true},
info: {type: String, required: true},
date: {type: Date, required: true}
});
const users = mongoose.model('users', usersSchema);
this.users = users;
this.bcrypt = bcrypt;
users.findOne(function (err, user){
if (err) return console.error(err);
if(!user){
const hash = bcrypt.hashSync('password', 12);
const id = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2);
const root = new users({userid:id, username:'root', password:hash, email:'root@multimedia.com', role:'Admin', info:'test', date: new Date()});
root.save(function (err, root){
if (err) return console.error(err);
});
}
});
}
checkUser(req, callback){
this.users.findOne({email:req.body.email}, function (err, user){
if (err) return console.error(err);
return callback(user);
});
}
checkEmail(req, callback){
this.users.findOne({email:req.body.email}, function (err, user){
if (err) return console.error(err);
return callback(user);
});
}
insertUser(req, callback){
const hash = this.bcrypt.hashSync(req.body.password, 12);
const id = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2);
const user = new (this.users)({userid:id, username:req.body.username, password:hash, email:req.body.email, role:req.body.role, info:req.body.info, date: new Date(req.body.date)});
user.save(function (err, user){
if (err) return callback(false, err);
return callback(user, false)
});
}
selectUser(amount=5, callback, id=false){
if(id){
this.users.findOne({userid: id}, function(err, user){
if (err) return console.error(err);
return callback(user);
});
}else{
this.users.find().sort({date: -1, _id: -1}).limit(amount).then(users => {
return callback(users);
});
}
}
countUser(callback){
this.users.countDocuments({}, function(err, users){
if(err) return console.log(err);
return callback(users);
})
}
updateUser(req, callback){
this.users.findOne({userid:req.params.authorId}, function (err, user){
if (err) return console.error(err);
user.username = req.body.username;
user.email = req.body.email;
user.role = req.body.role;
user.info = req.body.info;
user.date = new Date(req.body.date);
user.save(function (err, user){
if (err) return console.error(err);
return callback(user);
});
});
}
}//end class
module.exports = new Usersdb();
GitHub: "https://github.com/Sokhavuth/multimedia
Heroku: https://khmerweb-multimedia.herokuapp.com/

Comments
Post a Comment