Adding User Pagination Using Ajax Call
// 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');
}
async getAuthor(req, res){
const self = this;
const data = this.deepcopy(this.vdict);
data.site_title = 'ទំព័រអ្នកនិពន្ធ';
data.date = this.utility.setDate();
data.authors = await this.usersdb.selectUser(this.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
if(req.params.authorId){
data.edited = await self.usersdb.selectUser(self.vdict.dashboardLimit, req.params.authorId);
res.render('dashboard/author', data);
}else
res.render('dashboard/author', data);
}
async 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'){
const user = await this.usersdb.checkEmail(req);
if(user){
data.authors = await self.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
data.message = 'Email នេះមានគេប្រើប្រាស់ហើយ';
res.render('dashboard/author', data);
}else{
self.emailCheck(req.body.email)
.then(async function (result) {
if(result){
const user = await self.usersdb.insertUser(req);
data.authors = await self.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
data.message = `អ្នកនិពន្ធ ${user.username} ត្រូវបានចុះបញ្ជីរួចហើយ`;
res.render('dashboard/author', data);
}
}).catch(async function (err) {
data.authors = await self.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
data.message = 'Email នេះមិនត្រឹមត្រូវទេ';
res.render('dashboard/author', data);
});
}
}else{
data.authors = await this.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
data.message = 'មានតែ Administrator ទេ ដែលអាចចុះបញ្ជីអ្នកនិពន្ធបាន';
res.render('dashboard/author', data);
}
}
async updateAuthor(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") || (req.session.user.userid === req.params.authorId)){
const user = await this.usersdb.checkEmail(req);
if(user && (req.params.authorId != user.userid)){
data.authors = await self.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
data.message = 'Email នេះមានគេប្រើប្រាស់ហើយ';
res.render('dashboard/author', data);
}else{
self.emailCheck(req.body.email)
.then(async function (result) {
if(result){
if((req.session.user.role === "Admin") || (req.session.user.userid === user.userid)){
data.author = await self.usersdb.updateUser(req);
data.message = `ទិន្នន័យអ្នកនិពន្ធ ${data.author.username} ត្រូវបានកែតំរូវ`;
data.authors = await self.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
res.render('dashboard/author', data);
}
}
})
.catch(async function (err) {
data.authors = await self.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
data.message = 'Email នេះមិនត្រឹមត្រូវទេ';
res.render('dashboard/author', data);
});
}
}else{
data.authors = await this.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
data.message = 'មានតែ Administrator ឬសមីខ្លូនទេ ដែលអាចដូរទិន្នន័យអ្នកនិពន្ធបាន';
res.render('dashboard/author', data);
}
}
async deleteAuthor(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"){
const user = await self.usersdb.deleteUser(req);
data.authors = await this.usersdb.selectUser(self.vdict.dashboardLimit);
data.thumbs = self.utility.getThumbUrl(data.authors, 'author');
data.count = await self.usersdb.countUser();
data.message = `អ្នកនិពន្ធឈ្មោះ ${user.username} ត្រូវបានលុបចេញពីបញ្ជី`;
res.render('dashboard/author', data);
}
}
async loadAuthor(req, res){
const self = this;
const authors = await this.usersdb.selectUser(self.vdict.dashboardLimit, false, req.params.page);
const thumbs = self.utility.getThumbUrl(authors, 'author');
res.json({authors:authors, thumbs:thumbs});
}
}//end class
module.exports = new Author();
GitHub: "https://github.com/Sokhavuth/multimedia
Heroku: https://khmerweb-multimedia.herokuapp.com/

Comments
Post a Comment