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

export default post

 

//controllers/admin/post/create.js
import create from '../../../models/post/create.js'

export default async (req,res)=>{
    await create(req)

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

 

//models/post/create.js

export default async (req)=>{
    const id = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2)

    let mypost = {
        id: id,
        title: req.body.title,
        content: req.body.content,
        thumb: req.body.thumb,
        video: req.body.entries,
        category: req.body.category,
        date: new Date(req.body.datetime),
        author: req.session.user.userid
    }
 
    await req.mydb.collection("posts").insertOne(mypost)
}

 

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

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