//routes/admin/category.js
import express from 'express'
const category = express.Router()
category.get('/',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/category/read.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
category.post('/', async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/category/create.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
category.get('/edit/:id',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/category/read.js')
module.default(req,res,req.params.id)
}else{
res.redirect('/admin/login')
}
})
category.post('/edit/:id',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/category/update.js')
module.default(req,res,req.params.id)
}else{
res.redirect('/admin/login')
}
})
export default category
//controllers/admin/category/read.js
import settings from '../../../settings.js'
import read from '../../../models/category/read.js'
import count from '../../../models/count.js'
export default async (req,res)=>{
let mySettings = JSON.parse(JSON.stringify(settings))
mySettings.pageTitle = 'ទំព័រជំពូក'
mySettings.route = '/admin/category'
if(req.params.id){
mySettings.item = await read(req,mySettings.dItemLimit,req.params.id)
const date = mySettings.item.date.toLocaleDateString('fr-CA')
const time = mySettings.item.date.toLocaleTimeString('it-IT')
mySettings.item.date = date + 'T' + time
}
mySettings.items = await read(req,mySettings.dItemLimit)
mySettings.count = await count(req,'categories')
res.render('base',{data:mySettings})
}
//models/category/read.js
export default async (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()
}
}
<!--views/admin/category.ejs-->
<link href="/styles/admin/category.css" rel="stylesheet"></link>
<div class='content'>
<% if('item' in data){ %>
<form action='/admin/category/edit/<%= data.item.id %>' method='post' >
<a>ឈ្មោះជំពូកៈ</a><input type='text' value="<%= data.item.title %>" name='label' required />
<a>រូបសញ្ញាៈ</a><input type='text' value="<%= data.item.thumb %>" name='thumb' required />
<a>ពេលបង្កើតៈ</a><input type='datetime-local' value='<%= data.item.date %>' name="datetime" required />
<a></a><input type='submit' value='បញ្ជូនទិន្នន័យ' />
</form>
<% }else{ %>
<form action='/admin/category' method='post' >
<a>ឈ្មោះជំពូកៈ</a><input type='text' name='label' required />
<a>រូបសញ្ញាៈ</a><input type='text' name='thumb' required />
<a>ពេលបង្កើតៈ</a><input type='datetime-local' value='' name="datetime" required />
<a></a><input type='submit' value='បញ្ជូនទិន្នន័យ' />
</form>
<% } %>
</div>