//routes/backend/category.js
import express from 'express'
const categoryRouter = express.Router()
import category from '../../controllers/backend/category.js'
categoryRouter.get('/', async function(req,res){
await category.getPage(req,res)
})
categoryRouter.post('/', async function(req,res){
await category.createItem(req,res)
})
categoryRouter.get('/edit/:id', async function(req,res){
await category.getPage(req,res)
})
categoryRouter.post('/edit/:id', async function(req,res){
await category.updateItem(req,res)
})
categoryRouter.get('/delete/:id', async function(req,res){
await category.deleteItem(req,res)
})
export default categoryRouter
//controllers/backend/category.js
import category from '../../models/category.js'
class Category{
async getPage(req,res){
if(req.session.user){
req.settings.pageTitle = 'ទំព័រជំពូក'
req.settings.message = ''
req.settings.route = '/admin/category'
req.settings.items = await category.getItem(req,req.settings.dItemLimit)
req.settings.count = await category.countItem(req)
if(req.params.id){
req.settings.item = await category.getItem(req,req.settings.dItemLimit,req.params.id)
}
res.render('base', {data:req.settings})
}else{
res.redirect('/admin/login')
}
}
async createItem(req,res){
if(req.session.user){
if(req.session.user.role == 'Admin'){
category.createItem(req)
}
res.redirect('/admin/category')
}else{
res.redirect('/admin/login')
}
}
async updateItem(req,res){
if(req.session.user){
if(req.session.user.role == 'Admin'){
category.updateItem(req)
}
res.redirect('/admin/category')
}else{
res.redirect('/admin/login')
}
}
async deleteItem(req,res){
if(req.session.user){
if(req.session.user.role == 'Admin'){
category.deleteItem(req)
}
res.redirect('/admin/category')
}else{
res.redirect('/admin/login')
}
}
}
export default new Category()
//models/category.js
class Category{
async createItem(req){
const id = Date.now() + Math.round(Math.random() * 1E9).toString()
let myCategory = {
id: id,
title: req.body.label,
thumb: req.body.thumb,
date: req.body.datetime
}
req.mydb.collection("categories").insertOne(myCategory)
}
async countItem(req){
return await req.mydb.collection('categories').countDocuments()
}
async getItem(req,amount=10,id=false,page=0){
if(id){
return await req.mydb.collection("categories").findOne({id:id})
}else if(page){
return await req.mydb.collection("categories").find().skip(amount*page).sort({date:-1,_id:-1}).limit(amount).toArray()
}else if(amount === 'all'){
return await req.mydb.collection("categories").find({}, {title:1,_id:0}).sort({title:1}).toArray()
}else{
return await req.mydb.collection("categories").find().sort({date:-1,_id:-1}).limit(amount).toArray()
}
}
async updateItem(req){
const myquery = {id:req.params.id}
let newvalue = {$set: {
title: req.body.label,
thumb: req.body.thumb,
date: req.body.datetime
}}
await req.mydb.collection("categories").updateOne(myquery,newvalue)
}
async deleteItem(req){
const myquery = {id:req.params.id}
await req.mydb.collection("categories").deleteOne(myquery)
}
}
export default new Category()