// routes/admin/user.js
import express from 'express'
const userRouter = express.Router()
import user from '../../controllers/admin/user.js'

userRouter.get('/',async (req,res)=>{
    if(req.session.user){
        user.getItem(req,res)
    }else{
        res.redirect('/login')
    }
})

userRouter.post('/',async (req,res)=>{
    if(req.session.user){
        user.postItem(req,res)
    }else{
        res.redirect('/login')
    }
})

userRouter.get('/edit/:id',async (req,res)=>{
    if(req.session.user){
        user.editItem(req,res)
    }else{
        res.redirect('/login')
    }
})

userRouter.post('/edit/:id',async (req,res)=>{
    if(req.session.user){
        user.updateItem(req,res)
    }else{
        res.redirect('/login')
    }
})

userRouter.get('/delete/:id',async (req,res)=>{
    if(req.session.user){
        user.deleteItem(req,res)
    }else{
        res.redirect('/login')
    }
})

userRouter.post('/paginate',async (req,res)=>{
    if(req.session.user){
        user.paginateItem(req,res)
    }else{
        res.redirect('/login')
    }
})

export default userRouter

 

// controllers/admin/user.js
import config from "../../config.js"
import userdb from '../../models/user.js'

class User{
    async getItem(req,res){
        this.config = await config()
        this.config.pageTitle = 'ទំព័រ​អ្នក​ប្រើប្រាស់'
        this.config.user = req.session.user
        this.config.route = '/admin/user'

        if(this.config.user.role in {'Admin':1,'Editor':1}){
            this.config.type = 'user'

            this.config.count = await userdb.count(req)
            this.config.items = await userdb.getItem(req,this.config.maxPosts)

        }else if(this.config.user.role === 'Author'){
            this.config.item =  await userdb.editItem(req,this.config.user.id)
        }

        res.render('base',{data:this.config})
    }

    async postItem(req,res){
        await userdb.postItem(req)
        res.redirect('/admin/user')
    }

    async editItem(req,res){
        this.config = await config()
        this.config.pageTitle = 'ទំព័រ​អ្នក​ប្រើប្រាស់'
        this.config.user = req.session.user
        this.config.route = '/admin/user'

        if(this.config.user.role in {'Admin':1,'Editor':1}){
            this.config.type = 'user'

            this.config.count = await userdb.count(req)
            this.config.items = await userdb.getItem(req,this.config.maxPosts)
            this.config.item =  await userdb.editItem(req,req.params.id)

        }else if(this.config.user.role === 'Author'){
            this.config.item =  await userdb.editItem(req,req.params.id)
        }

        res.render('base',{data:this.config})
    }

    async updateItem(req,res){
        if(req.session.user.role in {'Admin':1,'Editor':1}){
            await userdb.updateItem(req)
        }else if(req.session.user.id === req.params.id){
            await userdb.updateAuthor(req)
        }

        res.redirect('/admin/user')
    }

    async deleteItem(req,res){
        if(req.session.user.role in {'Admin':1,'Editor':1}){
            await userdb.deleteItem(req)
        }else if(req.session.user.id === req.params.id){
            await userdb.deleteItem(req)
        }

        res.redirect('/admin/user')
    }

    async paginateItem(req,res){
        this.config = await config()
        this.config.route = '/admin/user'
        this.config.type = 'user'

        this.config.items = await userdb.paginateItem(req,this.config.maxPosts)

        res.json(this.config)
    }

}

export default new User()

 

// models/user.js
// npm install bcryptjs
import bcrypt from 'bcryptjs'

class User{
    async checkUser(req){
        const query = {email:req.body.email}
        return await req.mydb.collection("users").findOne(query)
    }

    async count(req){
        return await req.mydb.collection('users').countDocuments()
    }

    async postItem(req){
        const id = Date.now() + Math.round(Math.random() * 1E9).toString()
        const hashPassword = bcrypt.hashSync(req.body.password, 12)

        let newUser = {
            id: id, 
            title: req.body.title,
            content: req.body.content,
            thumb: req.body.thumb,
            postdate: req.body.datetime,
            role: req.body.category,
            email: req.body.email,
            password: hashPassword,
        }
 
        await req.mydb.collection("users").insertOne(newUser)
    }

    async getItem(req,amount){
        return await req.mydb.collection("users").find().sort({date:-1,_id:-1}).limit(amount).toArray()
    }

    async editItem(req,id){
        return await req.mydb.collection('users').findOne({id:id})
    }

    async updateItem(req){
        let myquery = {id: req.params.id}

        const user = await req.mydb.collection('users').findOne({id:req.params.id})

        if(req.body.password === user.password){
            var hashPassword = req.body.password
        }else{
            var hashPassword = bcrypt.hashSync(req.body.password, 12)
        }

        let newvalue = {$set: {
            title: req.body.title,
            content: req.body.content,
            thumb: req.body.thumb,
            postdate: req.body.datetime,
            role: req.body.category,
            email: req.body.email,
            password: hashPassword
        }}
 
        await req.mydb.collection("users").updateOne(myquery,newvalue)
    }

    async updateAuthor(req){
        let myquery = {id: req.params.id}

        if(req.body.password === req.session.user.password){
            var hashPassword = req.body.password
        }else{
            var hashPassword = bcrypt.hashSync(req.body.password, 12)
        }

        let newvalue = {$set: {
            title: req.body.title,
            content: req.body.content,
            thumb: req.body.thumb,
            postdate: req.body.datetime,
            email: req.body.email,
            password: hashPassword
        }}
 
        await req.mydb.collection("users").updateOne(myquery,newvalue)
    }

    async deleteItem(req){
        await req.mydb.collection("users").deleteOne({id:req.params.id})
    }

    async paginateItem(req,amount){
        const page = req.body.page
        return await req.mydb.collection("users").find().skip(amount*page).sort({postdate:-1,_id:-1}).limit(amount).toArray()
    }
}

export default new User()

 

Heroku: https://khmerweb-sale.herokuapp.com/login