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

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

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

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

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

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

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

export default routeUser

 

//controllers/admin/user/paginate.js
import settings from '../../../settings.js'
import read from '../../../models/user/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/user/read.js

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

 

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

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

 

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

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