The same thing as updating a job item, we need to create a route, a controller, and a model, all will be linked together to delete a job item in MongoDB database.

 

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

jobRoute.get('/delete/:id',async (req,res,next)=>{
    if(req.session.user){
        job.deleteItem(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')
    }

    async deleteItem(req,res){
        await jobDB.deleteItem(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)
    }

    async deleteItem(req){
        const myquery = {id:req.params.id}
        await req.mydb.collection("jobs").deleteOne(myquery)
    }
}

export default new Job()

 

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

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