As we do with category items, to open the next page of job items, we make an Ajax post call to a route that will call a controller that will call a model to pull a number of job items from MongoDB database.

 

// static/scripts/paginate.js

let page = 0

function paginate(route){
    $('.paginate img').attr('src', '/images/loading.gif')
    page += 1
    
    $.post(`${route}/paginate/`,{page:page},function(data, status){
        appendItem(data.items,route,data)
    })
}

function appendItem(items, route,data){
    let html = ''
    
    if(items){
        for(let item of items){
            html += `<li>`
                html += `<div class='thumb'>`
                    html += `<a href="/${data.type}/${item.id}"><img src="${item.thumb}"/></a>`
                html += `</div>`
                html += `<div class="title">`
                    html += `<a href="/${data.type}/${item.id}">${item.title}</a>`
                    html += `<div>${new Date(item.date).toLocaleDateString('it-IT')}</div>`
                html += `</div>`
                html += `<div class="edit">`
                    html += `<a href="${route}/edit/${item.id}"><img src="/images/edit.png"/></a>`
                    html += `<a href="${route}/delete/${item.id}"><img src="/images/delete.png"/></a>`
                html += `</div>` 
            html += `</li>`
        }
    }
    $('.list').append(html)

    if(route === '/admin/user'){
        $('.Listing .list li').css({'grid-template-columns':'13% auto 25%'})
        $('.Listing .list li .thumb').css({'padding-top':'100%'})
        $('.Listing .list li .thumb img').css({'border-radius':'50%'})
    }

    $('.paginate img').attr('src', '/images/load-more.png')
} 

 

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

jobRoute.post('/paginate',async (req,res,next)=>{
    if(req.session.user){
        job.paginateItem(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')
    }

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

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

        res.json(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})
    }

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

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

export default new Job()

 

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

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