//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)
}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)
}else{
res.redirect('/admin/login')
}
})
category.get('/delete/:id',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/category/delete.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
category.post('/paginate',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/category/paginate.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
export default category
//public/scripts/admin/paginate.js
let page = 0
function paginate(route){
$('.paginate img').attr('src', '/images/loading.gif')
page += 1
$.post(`${route}/paginate/`,{page:page},function(data, status){
appendItem(data.items,route)
})
}
function appendItem(items, route){
let html = ''
if(items){
for(let item of items){
html += `<li>`
html += `<div class='thumb'>`
html += `<a href="${route}/${item.id}"><img src="${item.thumb}"/></a>`
if(item.video){
if(item.video !== ''){
html += `<img class="play-icon" src="/images/play-icon"/>`
}
}
html += `</div>`
html += `<div class="title">`
html += `<a href="${route}/${item.id}">${item.title}</a>`
html += `<div>${new Date(item.date).toLocaleDateString()}</div>`
html += `</div>`
html += `<div class="edit">`
html += `<a href="${route}/edit/${item.id}"><img src="/images/edit.png"/></a>`
html += `<a href="${route}/delete/${item.id}"><img src="/images/delete.png"/></a>`
html += `</div>`
html += `</li>`
}
}
$('.list').append(html)
$('.paginate img').attr('src', '/images/load-more.png')
}
//controllers/admin/category/paginate.js
import settings from '../../../settings.js'
import read from '../../../models/category/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/category/read.js
export default async (req,amount=10,id=false)=>{
if(id){
return await req.mydb.collection("categories").findOne({id:id})
}else if(req.body.page){
const page = req.body.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()
}
}