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

export default category

 

//controllers/admin/category/create.js
import settings from '../../../settings.js'
import create from '../../../models/category/create.js'

export default async (req,res)=>{
    settings.pageTitle = 'ទំព័រ​ជំពូក'
    settings.route = '/admin/category'
    if(req.session.user.role === 'Admin'){
        create(req)
    }

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

 

//models/category/create.js

export default async (req)=>{
    const id = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2)
 
    let myCategory = {
        id: id, 
        label: req.body.label,
        thumb: req.body.thumb,
        date: new Date(req.body.datetime)
    }
 
    req.mydb.collection("categories").insertOne(myCategory)
}

 

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

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