//routes/admin/post.js
import express from 'express'
const post = express.Router()

post.get('/',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/post/read.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

post.post('/',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/post/create.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

post.get('/edit/:id',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/post/read.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

post.post('/edit/:id',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/post/update.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

post.get('/delete/:id',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/post/delete.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

post.post('/paginate',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/post/paginate.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

export default post

 

//public/scripts/admin/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)
    })
}

function appendItem(items, route){
    let html = ''
    
    if(items){
        for(let item of items){
            html += `<li>`
                html += `<div class='thumb'>`
                    html += `<a href="${route}/${item.id}"><img src="${item.thumb}"/></a>`
                    if((item.video)&&(item.video !== '[]')){
                        html += `<img class="play-icon" src="/images/play.png"/>`
                    }
                html += `</div>`
                html += `<div class="title">`
                    html += `<a href="${route}/${item.id}">${item.title}</a>`
                    html += `<div>${new Date(item.date).toLocaleDateString()}</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)
    $('.paginate img').attr('src', '/images/load-more.png')
}

 

//controllers/admin/post/paginate.js
import settings from '../../../settings.js'
import read from '../../../models/post/read.js'

let mySettings = JSON.parse(JSON.stringify(settings))

export default async (req,res)=>{
    mySettings.items = await read(req,mySettings.dItemLimit)
    res.json(mySettings)
}

 

//models/post/read.js

export default async (req,amount=10,id=false)=>{
    if(id){
        return await req.mydb.collection("posts").findOne({id:id})
    }else if(req.body.page){
        const page = req.body.page
        return await req.mydb.collection("posts")
            .find().skip(amount*page).sort({date:-1,_id:-1}).limit(amount).toArray()
    }else{
        return await req.mydb.collection("posts")
            .find().sort({date:-1,_id:-1}).limit(amount).toArray()
    }
}

 

GitHub: https://github.com/Sokhavuth/Blog_

Vercel: https://khmerweb-blog.vercel.app/