// routes/admin/post.js
import { Router, verify } from "../../deps.ts"
const router = Router()
import post from '../../controllers/admin/post.js'
router.get('/', async (req, res) => {
if(await req.session.get("user") === (await verify(req.myjwt, req.mykey)).user){
post.getItem(req, res)
}else{
res.redirect('/login')
}
})
router.post('/', async (req, res) => {
if(await req.session.get("user") === (await verify(req.myjwt, req.mykey)).user){
post.postItem(req, res)
}else{
res.redirect('/login')
}
})
router.get('/edit/:id', async (req, res) => {
if(await req.session.get("user") === (await verify(req.myjwt, req.mykey)).user){
post.getItem(req, res)
}else{
res.redirect('/login')
}
})
router.post('/edit/:id', async (req, res) => {
if(await req.session.get("user") === (await verify(req.myjwt, req.mykey)).user){
post.editItem(req, res)
}else{
res.redirect('/login')
}
})
router.get('/delete/:id', async (req, res) => {
if(await req.session.get("user") === (await verify(req.myjwt, req.mykey)).user){
post.deleteItem(req, res)
}else{
res.redirect('/login')
}
})
export default router
// controllers/admin/post.js
import config from '../../config.js'
import post from '../../views/admin/post.jsx'
import postdb from '../../models/postdb.ts'
class Post{
async getItem(req, res){
this.config = await config()
this.config.pageTitle = 'ទំព័រការផ្សាយ'
this.config.route = '/admin/post'
this.config.type = 'post'
this.config.count = await postdb.count(req)
const {item, items} = await postdb.getItem(req, this.config.adminItemLimit)
if(item){
await req.session.set('post-userid', item.userid)
}
this.config.item = item
this.config.items = items
const html = await post(this.config)
res.send(html)
}
async postItem(req, res){
const user_role = await req.session.get('user-role')
if(user_role in {'Admin':1,'Editor':1,'Author':1}){
await postdb.insertPost(req)
}
res.redirect('/admin/post')
}
async editItem(req, res){
const user_role = await req.session.get('user-role')
if(user_role in {'Admin':1,'Editor':1,'Author':1}){
const user_id = await req.session.get('user-id')
const post_userid = await req.session.get('post-userid')
if((user_role === 'Admin') || (user_id === post_userid)){
await postdb.editPost(req)
}
}
res.redirect('/admin/post')
}
async deleteItem(req, res){
await postdb.deletePost(req)
res.redirect('/admin/post')
}
}
export default new Post()
// models/postdb.js
interface PostSchema {
_id: ObjectId;
id: string;
title: string;
content: string;
categories: string[];
thumb: string;
postdate: string;
video: string;
userid: string;
}
class Postdb{
async count(req, query={}){
const posts = req.mydb.collection<PostSchema>("posts")
return await posts.countDocuments(query)
}
async insertPost(req){
const id = Date.now() + Math.round(Math.random() * 1E9).toString()
if(req.body.categories.includes(',')){
var categories: string[] = req.body.categories.split(',')
}else{
var categories: string[] = [req.body.categories]
}
const user_id = await req.session.get('user-id')
let newPost = {
id: id,
title: req.body.title,
content: req.body.content,
categories: categories,
thumb: req.body.thumb,
postdate: req.body.datetime,
video: req.body.video,
userid: user_id,
}
const posts = req.mydb.collection<PostSchema>("posts")
await posts.insertOne(newPost)
}
async getItem(req, amount, query={}){
const posts = req.mydb.collection<PostSchema>("posts")
let item = null
if(req.params.id){
item = await posts.findOne({id: req.params.id})
}
const items = await posts.find(query).sort({date:-1,_id:-1}).limit(amount).toArray()
return {item:item, items:items}
}
async editPost(req){
if(req.body.categories.includes(',')){
var categories: string[] = req.body.categories.split(',')
}else{
var categories: string[] = [req.body.categories]
}
let editPost = {$set:{
title: req.body.title,
content: req.body.content,
categories: categories,
thumb: req.body.thumb,
postdate: req.body.datetime,
video: req.body.video,
}}
const posts = req.mydb.collection<PostSchema>("posts")
await posts.updateOne({id: req.params.id}, editPost)
}
async deletePost(req){
const posts = req.mydb.collection<PostSchema>("posts")
if(req.params.id){
var item = await posts.findOne({id: req.params.id})
}
const user_id = await req.session.get('user-id')
const user_role = await req.session.get('user-role')
if(user_role in {'Admin':1,'Editor':1,'Author':1}){
if((user_role === 'Admin') || (user_id === item.userid)){
await posts.deleteOne({id: req.params.id})
}
}
}
}
export default new Postdb
GitHub: https://github.com/Sokhavuth/khmerweb-deno
Deno Deploy: https://khmerweb-blog.deno.dev/admin/post