Updating settings data requires us to define a new route with a parameter connecting to a controller that in turn connects to a model.

 

// route/admin/setting.js
import express from 'express'
const settingRoute = express.Router()
import setting from "../../controller/admin/setting.js"

settingRoute.get('/',async (req,res)=>{
    if(req.session.user){
        setting.getItem(req,res)
    }else{
        res.redirect('/login')
    }
})

settingRoute.post('/',async (req,res)=>{
    if(req.session.user){
        setting.postItem(req,res)
    }else{
        res.redirect('/login')
    }
})

settingRoute.post('/edit/:id',async (req,res)=>{
    if(req.session.user){
        setting.updateItem(req,res)
    }else{
        res.redirect('/login')
    }
})

export default settingRoute

 

// controller/admin/setting.js
import config from '../../config.js'
import settingDB from "../../model/setting.js"

class Setting{
    constructor(){
        (async ()=>{
            this.config = await config()
        })()
    }

    async getItem(req,res){
        this.config.pageTitle = 'ទំព័រ Setting'
        this.config.route = '/admin/setting'

        this.config.item = await settingDB.getItem(req)

        res.render('base',{data:this.config})
    }

    async postItem(req,res){
        if(req.session.user.role === 'Admin'){
            settingDB.postItem(req)
        }
        
        res.redirect('/admin/setting')
    }

    async updateItem(req,res){
        if(req.session.user.role === 'Admin'){
            settingDB.updateItem(req)
        }

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

export default await new Setting()

 

// model/setting.js

class Setting{
    async postItem(req){
        const id = Date.now() + Math.round(Math.random() * 1E9).toString()

        const settings = {
            id: id,
            siteTitle: req.body.siteTitle,
            description: req.body.description,
            maxPosts: req.body.maxPosts,
            indexPostLimit: req.body.indexPostLimit,
            categoryPostLimit: req.body.categoryPostLimit,
        }

        await req.mydb.collection('settings').insertOne(settings)
    }

    async getItem(req){
        return await req.mydb.collection('settings').findOne()
    }

    async updateItem(req){
        const myquery = {id: req.params.id}
        
        const setting = {$set: {
            siteTitle: req.body.siteTitle,
            description: req.body.description,
            maxPosts: req.body.maxPosts,
            indexPostLimit: req.body.indexPostLimit,
            categoryPostLimit: req.body.categoryPostLimit
        }}

        await req.mydb.collection('settings').updateOne(myquery,setting)
    }
}

export default new Setting()

 

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

    <div class='Setting'>
        <% 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.maxPosts %>" 
                name='maxPosts' 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='maxPosts' 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/khmerweb-job

Heroku: https://khmerweb-job.herokuapp.com