After creating users, we can modify them at will by pulling from MongoDB database. Fist of all, we need to define a route with user id as parameter that connect to a controller and a model as below:

 

// route/admin/user.js
import express from 'express'
const userRoute = express.Router()
import user from '../../controller/admin/user.js'

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

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

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

export default userRoute

 

// controller/admin/user.js
import config from '../../config.js'
import userDB from '../../model/user.js'

class User{
    constructor(){
        (async ()=>{
            this.config = await config()
        })()
    }

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

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

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

    async postItem(req,res){
        userDB.postItem(req,res)

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

    async getEditItem(req,res){
        this.config.pageTitle = 'ទំព័រកែប្រែអ្នកប្រើប្រាស់'
        this.config.route = '/admin/user'
        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.getSingle(req)
        console.log(this.config.item)
        res.render('base',{data:this.config})
    }
}

export default await new User()

 

// model/user.js
import bcrypt from 'bcryptjs'

class User{
    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 getSingle(req){
        return await req.mydb.collection('users').findOne({id:req.params.id})
    }
}

export default new User()

 

<!--views/admin/post.ejs-->
<link rel='stylesheet' href='/styles/admin/user.css' />
<script src="/scripts/ckeditor/ckeditor.js"></script>

<section class="User">
<% if(data.item){ %>
<form action='/admin/user/edit/<%= data.item.id %>' method='post' >
    <input type='text' name='title' value="<%= data.item.title %>" placeholder='ឈ្មោះ​អ្នក​ប្រើប្រាស់' required />
    <textarea name="content" id="editor" ><%= data.item.content %></textarea>
    <div class='wrapper'>
        <input type='text' name='thumb' value="<%= data.item.thumb %>" required placeholder="តំណរ​ភ្ជាប់​រូប​តំណាង" />
        <input type='datetime-local' value='<%= data.item.postdate %>' name='datetime' required />
        <select name='category' class="category">
            <option>Author</option>
            <option>Admin</option>
        </select>
        <script>$(".category").val("<%= data.item.role %>").change()</script>
        <input type="email" name="email" value="<%= data.item.email %>" required placeholder="Email" />
        <input type="password" name="password" value="<%= data.item.password %>" required />
        <input type='submit' value='ចុះ​ផ្សាយ' />
    </div>
</form>
<% delete data.item %>
<% }else{ %>
<form action='/admin/user' method='post' >
    <input type='text' name='title' placeholder='ឈ្មោះ​អ្នក​ប្រើប្រាស់' required />
    <textarea name="content" id="editor" ></textarea>
    <div class='wrapper'>
        <input type='text' name='thumb' required placeholder="តំណរ​ភ្ជាប់​រូប​តំណាង" />
        <input type='datetime-local' value='' name='datetime' required />
        <select name='category'>
            <option>Author</option>
            <option>Admin</option>
        </select>
        <input type="email" name="email" required placeholder="Email" />
        <input type="password" name="password" required />
        <input type='submit' value='ចុះ​ផ្សាយ' />
    </div>
</form>
<% } %>
</section>
<script src="/scripts/ckeditor/config.js"></script>

 

GitHub: https://github.com/Sokhavuth/khmerweb-job

Heroku: https://khmerweb-job.herokuapp.com