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

export default post

 

//controllers/admin/post/delete.js
import settings from '../../../settings.js'
import read from '../../../models/post/read.js'
import mydelete from '../../../models/post/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')||(req.session.user.userid === item.userid)){
        await mydelete(req)
    }

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

 

//models/post/delete.js

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

 

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

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