var express = require('express')
var router = express.Router()
var settings = require('../../settings')
router.get('/', async function(req, res, next) {
settings.dLogo = 'ទំព័រសៀវភៅ'
if(req.session.user){
const today = new Date()
const date = today.toLocaleDateString('fr-CA')
const time = today.toLocaleTimeString('it-IT')
settings.datetime = date + 'T' + time
const count = await require('../../controllers/books/count')()
settings.message = `ចំនួនសៀវភៅសរុបៈ ${count}`
const read = await require('../../controllers/books/read')
if(req.session.user.role === 'Admin'){
settings.items = await read(settings.dItemLimit)
}else{
settings.items = await read(settings.dItemLimit, null, null, req.session.user.userid)
}
settings.route = 'book'
res.render('users/book', settings)
}else{
res.redirect('/users')
}
})
router.post('/', async function(req, res, next) {
if(req.session.user){
await require('../../controllers/books/create')(req)
res.redirect('/users/book')
}else{
res.redirect('/users')
}
})
router.get('/edit/:id', async function(req, res, next) {
if(req.session.user){
settings.dLogo = 'ទំព័រកែប្រែសៀវភៅ'
const book = await require('../../controllers/books/read')(false, req.params.id)
if((req.session.user.role === 'Admin') || (req.session.user.userid === book.author)){
const date = book.date.toLocaleDateString('fr-CA')
const time = book.date.toLocaleTimeString('it-IT')
settings.datetime = date + 'T' + time
settings.book = book
settings.route = 'book'
res.render('users/edit_book', settings)
}else{
res.redirect('/users/book')
}
}else{
res.redirect('/users')
}
})
router.post('/edit/:id', async function(req, res, next){
if(req.session.user){
await require('../../controllers/books/update')(req)
res.redirect('/users/book/edit/' + req.params.id)
}else{
res.redirect('/users')
}
})
router.get('/delete/:id', async function(req, res, next){
if(req.session.user){
const book = await require('../../controllers/books/read')(false, req.params.id)
if((req.session.user.role === 'Admin') || (req.session.user.userid === book.author)){
await require('../../controllers/books/delete')(req)
}
res.redirect('/users/book')
}else{
res.redirect('/users')
}
})
router.post('/paginate', async function(req, res, next){
if(req.session.user){
const read = await require('../../controllers/books/read')
if(req.session.user.role === 'Admin'){
settings.items = await read(settings.dItemLimit, null, req.body.page)
}else{
settings.items = await read(settings.dItemLimit, null, req.body.page, req.session.user.userid)
}
res.json({items: settings.items})
}else{
res.redirect('/users')
}
})
module.exports = router
const schema = require('./schema')
async function getBook(amount=10, id=false, page=0, userid=null){
const bookSchema = await schema()
if(userid){
var querry = {author: userid}
}else{
var querry = {}
}
if(id){
return await bookSchema.findOne({id: id})
}else if(page){
return await bookSchema.find(querry).skip(amount * page).sort({date: -1, _id: -1}).limit(amount)
}else{
return await bookSchema.find(querry).sort({date: -1, _id: -1}).limit(amount)
}
}
module.exports = getBook
GitHub: https://github.com/Sokhavuth/tv
Vercel: https://tv-neon.vercel.app
Responsive: http://www.responsinator.com