After being created on MongoDB database, we can modify settings data by pulling it from the database and list it on a form.
// 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')
}
}
export default await new Setting()
// model/setting.js
class Setting{
async postItem(req){
const settings = {
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()
}
}
export default new Setting()