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

export default routeUser

 

//controllers/admin/user/delete.js
import settings from '../../../settings.js'
import read from '../../../models/user/read.js'
import mydelete from '../../../models/user/delete.js'

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

export default async (req,res)=>{
    let item = await read(req,mySettings.dItemLimit,req.params.id)
    if(req.session.user.role === 'Admin'){
        await mydelete(req)
    }

    res.redirect('/admin/user')
}

 

//models/user/delete.js

export default async (req)=>{
    const myquery = {id:req.params.id}
 
    await req.mydb.collection("users").deleteOne(myquery)
}

 

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

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