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

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

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

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

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

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

export default category

 

//controllers/admin/category/delete.js
import mydelete from '../../../models/category/delete.js'

export default async (req,res)=>{
    if(req.session.user.role === 'Admin'){
        await mydelete(req)
    }

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

 

//models/category/delete.js

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

 

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

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