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

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

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

export default bookRoute

 

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

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

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

 

//models/book/create.js

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

    let mybook = {
        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,
        bookTitle: req.body.bookTitle,
        bookCover: req.body.bookCover,
        bookChapter:  req.body.bookChapter
    }
 
    await req.mydb.collection("books").insertOne(mybook)
}

 

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

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