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

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

export default settingRoute

 

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

export default async (req,res)=>{
    if(req.session.user.role === 'Admin'){
        await update(req)
    }
    
    res.redirect('/admin/post')
}

 

//models/setting/update.js

export default async (req)=>{
    const myquery = {id:req.params.id}
    let newvalue = {$set: {
        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").updateOne(myquery,newvalue)
}

 

<!--views/admin/setting.ejs-->
<link href="/styles/admin/category.css" rel="stylesheet"></link>

    <div class='content'>
        <% if(data.item){ %>
        <form action='/admin/setting/edit/<%= data.item.id %>' method='post' >
            <a>ឈ្មោះគេហទំព័រ</a><input type='text' value="<%= data.item.siteTitle %>" 
                name='siteTitle' required />
            <a>អំពី​គេហទំព័រ</a><input type='text' value="<%= data.item.description %>" 
                name='description' required />
            <a>ទិន្នន័យ​ក្នុង​</a><input type='text' value="<%= data.item.dItemLimit %>" 
                name='dItemLimit' required /> 
            <a>ទិន្នន័យ​មុខ</a><input type='text' value="<%= data.item.indexPostLimit %>" 
                name='indexPostLimit' required /> 
            <a>ទិន្នន័យ​ប្រភេទ​</a><input type='text' value="<%= data.item.categoryPostLimit %>" 
                name='categoryPostLimit' required /> 
            <a></a><input type='submit' value='បញ្ជូនទិន្នន័យ' />
        </form>
        <% delete data.item %>
        <% }else{ %>
        <form action='/admin/setting' method='post' >
            <a>ឈ្មោះគេហទំព័រ</a><input type='text' value="" name='siteTitle' required />
            <a>អំពី​គេហទំព័រ</a><input type='text' value="" name='description' required />
            <a>ទិន្នន័យ​ក្នុង​</a><input type='text' value="" name='dItemLimit' required /> 
            <a>ទិន្នន័យ​មុខ​</a><input type='text' value="" name='indexPostLimit' required /> 
            <a>ទិន្នន័យ​ប្រភេទ​</a><input type='text' value="" name='categoryPostLimit' required /> 
            <a></a><input type='submit' value='បញ្ជូនទិន្នន័យ' />
        </form>
        <% } %>
    </div>

 

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

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