//routes/admin/post.js
import express from 'express'
const post = express.Router()
post.get('/',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/post/read.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
post.post('/',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/post/create.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
post.get('/edit/:id',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/post/read.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
post.post('/edit/:id',async function(req,res){
if(req.session.user){
const module = await import('../../controllers/admin/post/update.js')
module.default(req,res)
}else{
res.redirect('/admin/login')
}
})
export default post
//controllers/admin/post/read.js
import settings from '../../../settings.js'
import read from '../../../models/post/read.js'
import readCategory from '../../../models/category/read.js'
import count from '../../../models/count.js'
let mySettings = JSON.parse(JSON.stringify(settings))
export default async (req,res)=>{
mySettings.pageTitle = 'ទំព័រការផ្សាយ'
mySettings.route = '/admin/post'
if(req.params.id){
mySettings.item = await read(req,mySettings.dItemLimit,req.params.id)
const date = mySettings.item.date.toLocaleDateString('fr-CA')
const time = mySettings.item.date.toLocaleTimeString('it-IT')
mySettings.item.date = date + 'T' + time
}
mySettings.items = await read(req,mySettings.dItemLimit)
mySettings.count = await count(req,'posts')
mySettings.categories = await readCategory(req,'all')
res.render('base',{data:mySettings})
}
<!--views/admin/index.ejs-->
<link href="/styles/admin/index.css" rel="stylesheet">
<script src="/scripts/admin/paginate.js"></script>
<section class="Header">
<div class="wrapper region">
<div class="logo"><%= data.pageTitle %></div>
<form method="post" action="/search">
<select name="type">
<option>ការផ្សាយ</option>
<option>ទំព័រស្តាទិក</option>
<option>អ្នកប្រើប្រាស់</option>
</select>
<input type="text" name="q" placeholder="Search" required>
<input type="submit" value="ស្វែងរក">
</form>
<div class="logout">
<a href="/">ទំព័រមុខ</a> | <a href="/admin/login/logout">ចេញក្រៅ</a>
</div>
</div>
</section>
<section class="Body">
<div class="wrapper region">
<div class="sidebar">
<a href='/admin/post'><img src='/images/movie.png' /></a>
<a href='/admin/post'>ការផ្សាយ</a>
<a href='/admin/category'><img src='/images/category.png' /></a>
<a href='/admin/category'>ជំពូក</a>
<a href='/admin/book'><img src='/images/books.png' /></a>
<a href='/admin/book'>សៀវភៅ</a>
<a href='/admin/upload'><img src='/images/upload.png' /></a>
<a href='/admin/upload'>Upload</a>
<a href='/admin/user'><img src='/images/users.png' /></a>
<a href='/admin/user'>អ្នកប្រើប្រាស់</a>
<a href='/admin/setting'><img src='/images/setting.png' /></a>
<a href='/admin/setting'>Setting</a>
</div>
<div class="Content">
<% if(data.route.includes('/category')){ %>
<%- include('category.ejs') %>
<% }else if(data.route.includes('/post')){ %>
<%- include('post.ejs') %>
<% } %>
</div>
</div>
<div class="Listing region">
<div class="info">សរុបទាំងអស់មានចំនួនៈ <%= data.count %></div>
<ul class="list">
<% if(data.items){ %>
<% for(let item of data.items) { %>
<li>
<div class='thumb'>
<a href='<%= data.route %>/<%= item.id %>'><img src='<%= item.thumb %>'/>
<% if((item.video)&&(item.video !== '[]')){ %>
<img class="play-icon" src="/images/play.png"/>
<% } %>
</a>
</div>
<div class="title">
<a href="<%= data.route %>/<%= item.id %>"><%= item.title %></a>
<div><%= item.date.toLocaleDateString() %></div>
</div>
<div class="edit">
<a href="<%= data.route %>/edit/<%= item.id %>"><img src="/images/edit.png"/></a>
<a href="<%= data.route %>/delete/<%= item.id %>"><img src="/images/delete.png"/></a>
</div>
</li>
<% } %>
<% } %>
</ul>
<div class="paginate region">
<img onclick="paginate('<%= data.route %>')" src="/images/load-more.png"/>
</div>
</div>
</section>
<!--views/admin/post.ejs-->
<link rel='stylesheet' href='/styles/admin/post.css' />
<script src="/scripts/ckeditor/ckeditor.js"></script>
<script src="/scripts/admin/video.js"></script>
<section class='Main'>
<div class='content'>
<% if(data.item){ %>
<form action='/admin/post/edit/<%= data.item.id %>' method='post' >
<input type='text' name='title' value='<%= data.item.title %>' placeholder='ចំណងជើង' required />
<textarea name="content" id="editor" ><%= data.item.content %></textarea>
<div class='wrapper'>
<select name='category' class='category' >
<% for(let v in data.categories){ %>
<option><%= data.categories[v].title %></option>
<% } %>
</select>
<script>$(".category").val("<%= data.item.category %>").change()</script>
<input type='text' name='thumb' value='<%= data.item.thumb %>' required placeholder="តំណរភ្ជាប់រូបតំណាង" />
<input type='datetime-local' value='<%= data.item.date %>' name='datetime' required />
<input type='submit' value='ចុះផ្សាយ' />
</div>
<input name='entries' value='<%- data.item.video %>' type='hidden' />
</form>
<% }else{ %>
<form action='/admin/post' method='post' >
<input type='text' name='title' placeholder='ចំណងជើង' required />
<textarea name="content" id="editor" ></textarea>
<div class='wrapper'>
<select name='category'>
<% for(let v in data.categories){ %>
<option><%= data.categories[v].title %></option>
<% } %>
</select>
<input type='text' name='thumb' required placeholder="តំណរភ្ជាប់រូបតំណាង" />
<input type='datetime-local' value='' name='datetime' required />
<input type='submit' value='ចុះផ្សាយ' />
</div>
<input name='entries' value='' type='hidden' />
</form>
<% } %>
<div class='form'>
<select name='type'>
<option>YouTube</option>
<option>YouTubePlaylist</option>
<option>Facebook</option>
<option>OK</option>
<option>Dailymotion</option>
<option>Vimeo</option>
</select>
<input name='id' type='text' placeholder="អត្តសញ្ញាណវីដេអូ" required />
<select name='ending'>
<option>ចប់</option>
<option>មិនទាន់ចប់</option>
<option>~ ចប់</option>
</select>
<input onclick='genJson()' type="button" value="បញ្ចូលវីដេអូ" />
</div>
<table class='viddata'></table>
<% if(data.item){ %>
<script>
var entries = JSON.parse('<%- data.item.video %>')
</script>
<% }else{ %>
<script>
var entries = ''
</script>
<% } %>
<% delete data.item %>
<script>
if(entries.length > 0){
let html = ``
for(let v in entries){
episode += 1
html += `<tr>`
html += `<td title="Delete" onClick="deleteRow(event)" class="episode">${episode}</td>`
html += `<td class="td${episode}">${entries[v].type}</td>`
html += `<td class="td${episode}">${entries[v].id}</td>`
html += `<td class="td${episode}">${entries[v].ending}</td>`
html += `</tr>`
}
if($('.viddata').html() === ''){
$('.viddata').append('<tr>')
$('.viddata').append('<th>ភាគ/លុប</th>')
$('.viddata').append('<th>ប្រភេទ</th>')
$('.viddata').append('<th>អត្តសញ្ញាណ</th>')
$('.viddata').append('<th>ចប់ឬនៅ?</th>')
$('.viddata').append('</tr>')
}
$('.viddata').append(`${html}`)
}
</script>
<script src="/scripts/ckeditor/config.js"></script>
</div>
</section>
//controllers/admin/post/update.js
import settings from '../../../settings.js'
import read from '../../../models/post/read.js'
import update from '../../../models/post/update.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 update(req)
}
res.redirect('/admin/post')
}
//models/post/update.js
export default async (req)=>{
const myquery = {id:req.params.id}
let newvalue = {$set: {
title: req.body.title,
content: req.body.content,
thumb: req.body.thumb,
video: req.body.entries,
category: req.body.category,
date: new Date(req.body.datetime),
}}
await req.mydb.collection("posts").updateOne(myquery,newvalue)
}