To update a job item after editing it, we need to define a route with parameter leading to a controller that in turn linking to a model as below:

 

//route/admin/job.js
import express from 'express'
const jobRoute = express.Router()
import job from '../../controller/admin/job.js'

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

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

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

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

export default jobRoute

 

// controller/admin/job.js
import config from '../../config.js'
import categories from '../../model/category.js'
import jobDB from '../../model/job.js'

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

    async getItem(req,res){
        this.config.pageTitle = 'ទំព័រ​ការងារ'
        this.config.route = '/admin/job'
        this.config.type = 'job'

        this.config.categories = await categories.getAllItem(req)
        this.config.items = await jobDB.getItem(req,this.config.maxPosts)
        this.config.count = await jobDB.count(req)
        res.render('base',{data: this.config})
    }

    async postItem(req,res){
        await jobDB.insertItem(req)

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

    async editItem(req,res){
        this.config.pageTitle = 'ទំព័រ​កែប្រែការងារ'
        this.config.route = '/admin/job'
        this.config.type = 'job'

        this.config.categories = await categories.getAllItem(req)
        this.config.items = await jobDB.getItem(req,this.config.maxPosts)
        this.config.count = await jobDB.count(req)
        this.config.item = await jobDB.getSingleItem(req)

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

    async updateItem(req,res){
        await jobDB.updateItem(req)

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

}

export default await new Job()

 

// model/job.js

class Job{
    async count(req){
        return await req.mydb.collection('jobs').countDocuments()
    }

    async insertItem(req){
        const id = Date.now() + Math.round(Math.random() * 1E9).toString()
 
        let newJob = {
            id: id, 
            title: req.body.title,
            content: req.body.content,
            categories: req.body.categories,
            payable: req.body.payable,
            email: req.body.email,
            website: req.body.website,
            thumb: req.body.thumb,
            location: req.body.location,
            postdate: new Date(),
            closedate: req.body.closedate,
            author: req.session.user.id
        }
 
        await req.mydb.collection("jobs").insertOne(newJob)
    }

    async getItem(req,amount){
        return await req.mydb.collection('jobs').find().sort({data: -1,_id: -1}).limit(amount).toArray()
    }

    async getSingleItem(req){
        return await req.mydb.collection('jobs').findOne({id: req.params.id})
    }

    async updateItem(req){
        const myquery = {id:req.params.id}
        let newvalue = {$set: {
            title: req.body.title,
            content: req.body.content,
            categories: req.body.categories,
            payable: req.body.payable,
            email: req.body.email,
            website: req.body.website,
            thumb: req.body.thumb,
            location: req.body.location,
            closedate: req.body.closedate,
        }}
 
        await req.mydb.collection("jobs").updateOne(myquery,newvalue)

    }
}

export default new Job()

 

<!--view/admin/job.ejs-->
<link rel="stylesheet" href="/styles/admin/job.css" />
<script src="/scripts/ckeditor/ckeditor.js"></script>
<script src="/scripts/addCategory.js"></script>

<section class="Job">
    <% if(data.item){ %>
    <form action="/admin/job/edit/<%= data.item.id %>" method="post">
        <input type="text" name="title" value="<%= data.item.title %>" required placeholder="ចំណង​ជើង" />
        <textarea name="content" id="editor"><%= data.item.content %></textarea>
        <input type="text" name="categories" value="<%= data.item.categories %>" required placeholder="ជំពូក" />
        <div class="wrapper">
            <select id="category" onChange="getCategory()">
                <option>ជ្រើសរើស​ជំពូក</option>
                <% for(let v in data.categories){ %>
                    <option><%= data.categories[v].title %></option>
                <% } %>
            </select>
            <input type="text" value="<%= data.item.payable %>" name="payable" required placeholder="ប្រាក់​ឈ្នួល" />
            <input type="email" value="<%= data.item.email %>" name="email" required placeholder="Email" />
            <input type="text" value="<%= data.item.website %>" name="website" placeholder="គេហទំព័រ" />
            <input type="text" value="<%= data.item.thumb %>" name="thumb" required placeholder="តំណរភ្ជាប់​រូប​តំណាង"/>
            <input type="text" value="<%= data.item.location %>" name="location" required placeholder="ទីកន្លែង" />
            <input type="datetime-local" value="<%= data.item.closedate %>" name="closedate" required />
            <input type="submit" value="​​​​​​​​​​​​​ចុះ​ផ្សាយ" />
        </div>
    </form>
    <% delete data.item %>
    <% }else{ %>
        <form action="/admin/job" method="post">
            <input type="text" name="title" required placeholder="ចំណង​ជើង" />
            <textarea name="content" id="editor"></textarea>
            <input type="text" name="categories" required placeholder="ជំពូក" />
            <div class="wrapper">
                <select id="category" onChange="getCategory()">
                    <option>ជ្រើសរើស​ជំពូក</option>
                    <% for(let v in data.categories){ %>
                        <option><%= data.categories[v].title %></option>
                    <% } %>
                </select>
                <input type="text" name="payable" required placeholder="ប្រាក់​ឈ្នួល" />
                <input type="email" name="email" required placeholder="Email" />
                <input type="text" name="website" placeholder="គេហទំព័រ" />
                <input type="text" name="thumb" required placeholder="តំណរភ្ជាប់​រូប​តំណាង"/>
                <input type="text" name="location" required placeholder="ទីកន្លែង" />
                <input type="datetime-local" name="closedate" 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