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

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

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

export default settingRoute

 

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

export default async (req,res)=>{

    if(req.session.user.role === 'Admin'){
       await create(req)
    }

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

 

//models/setting/create.js

export default async (req)=>{
    const id = Date.now() + Math.round(Math.random() * 1E9).toString()

    let mypost = {
        id: id,
        siteTitle: req.body.siteTitle,
        description: req.body.description,
        dItemLimit: req.body.dItemLimit,
        indexPostLimit: req.body.indexPostLimit,
        categoryPostLimit: req.body.categoryPostLimit
    }
 
    await req.mydb.collection("settings").insertOne(mypost)
}

 

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

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