//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')
}
})
bookRoute.get('/edit/:id',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('/edit/:id',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/book/update.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
bookRoute.get('/delete/:id',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/book/delete.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
bookRoute.post('/paginate',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/book/paginate.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
export default bookRoute
//controllers/admin/book/paginate.js
import settings from '../../../settings.js'
import read from '../../../models/book/read.js'
let mySettings = JSON.parse(JSON.stringify(settings))
export default async (req,res)=>{
mySettings.items = await read(req,mySettings.dItemLimit)
res.json(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()
}
}