Before we can update a job item, we need to pull  it from MongoDB database and display it on an editable page. Also, a route to pull editable job item, related controller, and model need to be defined.

 

//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')
    }
})

export default jobRoute

 

// controller/admin/job.js
import config from '../../config.js'
import categories from '../../model/category.js'
import job 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 job.getItem(req,this.config.maxPosts)
        this.config.count = await job.count(req)
        res.render('base',{data: this.config})
    }

    async postItem(req,res){
        await job.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 job.getItem(req,this.config.maxPosts)
        this.config.count = await job.count(req)
        this.config.item = await job.getSingleItem(req)
        
        res.render('base',{data: this.config})
    }
}

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})
    }
}

export default new Job()

 

 

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

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