//routes/admin/book.js
import express from 'express'
const bookRoute = express.Router()
bookRoute.get('/',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/book/read.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
bookRoute.post('/',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/book/create.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
export default bookRoute
//controllers/admin/book/read.js
import settings from '../../../settings.js'
import read from '../../../models/book/read.js'
import readCategory from '../../../models/category/read.js'
import count from '../../../models/count.js'
let mySettings = JSON.parse(JSON.stringify(settings))
export default async (req,res)=>{
mySettings.pageTitle = 'ទំព័រសៀវភៅ'
mySettings.route = '/admin/book'
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,'books')
mySettings.categories = await readCategory(req,'all')
res.render('base',{data:mySettings})
}
//models/post/read.js
export default async (req,amount=10,id=false)=>{
if(id){
return await req.mydb.collection("books").findOne({id:id})
}else if(req.body.page){
const page = req.body.page
return await req.mydb.collection("books")
.find().skip(amount*page).sort({date:-1,_id:-1}).limit(amount).toArray()
}else{
return await req.mydb.collection("books")
.find().sort({date:-1,_id:-1}).limit(amount).toArray()
}
}