//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')
}
})
export default bookRoute
//controllers/admin/book/delete.js
import settings from '../../../settings.js'
import read from '../../../models/book/read.js'
import mydelete from '../../../models/book/delete.js'
let mySettings = JSON.parse(JSON.stringify(settings))
export default async (req,res)=>{
let item = await read(req,mySettings.dItemLimit,req.params.id)
if((req.session.user.role === 'Admin')||(req.session.user.userid === item.userid)){
await mydelete(req)
}
res.redirect('/admin/book')
}
//models/book/delete.js
export default async (req)=>{
const myquery = {id:req.params.id}
await req.mydb.collection("books").deleteOne(myquery)
}